Discuss about PHP Multi-language support

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by ducvux10, Sep 29, 2014.

  1. ducvux10

    ducvux10 New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    I am new to web development and I want to create a small site, but I need it has multi-language support that I can easy to manage later.
    So I create my first template (yeah, i think it still doesn't break your terms of service).
    This template is simple, but i don't know that it can work with big data or not?
    If anyone has any suggestion, please help to design a greater template, thanks.

    //require.php
    PHP:
    <?php
    ini_set
    "short_open_tag");//Try if can
    $_FORCE_LANGUAGE true//If restriction

    /*******************************************************************/
    class Language{
        private 
    $table = array();

        public function 
    __construct($data_file null){
            if (!
    $data_file) return;
            
    //Load all word from xml file, implement later :)

            //Test some word
            
    $this->setKeyValue("del_confirm""Are you sure want to delete this file?");
            
    $this->setKeyValue("error""Sorry, some error here!");
        }
        public function 
    setKeyValue($key$value){
            
    $this->table[$key] = $value;
        }

        public function 
    getKeyValue($key){
            if (!
    array_key_exists($key$this->table)) return $key//return null ???
            
    return $this->table[$key];
        }
    }

    $lang_code "en";
    if (!
    $_FORCE_LANGUAGE) {
        if (isset(
    $_COOKIE["preferedLang"]))
            
    $lang_code $_COOKIE["preferedLang"];
        
    //Other condition to choose language here...$_SERVER['HTTP_ACCEPT_LANGUAGE']...
    }
    $lg = new Language($lang_code ".xml");

    //Short of GET key value from $lg
    function g($key){
        global 
    $lg;
        return 
    $lg->getKeyValue($key);
    }
    //index.php
    PHP:
    <?php
    require("require.php");//Do this for all php file on server ?
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Test Multi-Language</title>
    </head>
    <body>
    <h3><?=g("site_heading")?></h3>

    <p><?=g("del_confirm")?></p>

    <div><?=g("error")?></div>

    </body>
    </html>
     
  2. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy Points:
    63
    This is nowhere near "big data" (which starts at "lots of gigabytes" and goes up from there), but you can easily run into problems anyway. It all depends on how you're planning to work with the XML you're talking about.
    Basically, it boils down to this simple axiom: XML is a horrible, horrible container for data. It has two, and only two, redeeming features that make it useful at all for anything. The first is trivial: it's human-readable (unless it's pathological, such as a bunch of base64-encoded binary data enclosed in CDATA sections). The second is that it's transportable, since the data descriptors can tell the receiving system what kind of data the data values represent, and that makes it easy for data created on one system type to be used on a different system type. But the down sides are huge. First, it's verbose, which means that for every character of data, there will be a relatively large count of characters that are about the data (metadata). Second, if you want to do anything more than simple linear parsing (that is, if you want to look anything up without continually re-reading the text file from the beginning), the XML file must be converted into a tree of data, and that tree can take up a surprisingly large amount of memory space, many times greater than the size of the file.
    On Free Hosting, memory is at a premium. You are allowed to use 64MB maximum for all concurrent processes (so "your part of the computer" only has 64MB of RAM, and you can't borrow space from anyone else). And PHP won't allow you to share space across processes (so if you have four readers all trying to read the Spanish version of your page, you need to load and make trees for four copies of the Spanish-language XML). So you can run out of memory in a hurry if you use XML.
    You have other options. You can use static page parts wherever they make sense. Yes, it means more files to maintain (although you can create a page generator to automate maintenance for you), but the server has to do very, very little work to include (or require) static content. And you have several options for lookups that are better than XML: MySQL, SQLlite, and JSON files (which are "lighter" than XML, already in "nearly a tree" form, and trivial to parse if they are simple key-value pairs, but are not nearly as efficient as "proper" database operations).
    Oh -- forget about short tags or ASP-style tags. The short-echo tag <?=xxx?> is supported everywhere even when short tags are turned off, and any other short tags you may try to use can leak out of the system when short tags are off, and then they become SGML bugs in the users' browsers if nothing bad happens at the server.
     
    • Like Like x 1

Share This Page