SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist ckchin's Avatar
    Join Date
    Mar 2002
    Location
    msia
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recommended Technique to buid multi-language PHP website?

    Hi,

    Could anyone share the good practices to build multi-language PHP website?

    Let say, my index.php has 2 languages, when user select english (with his membership settings, for example) the welcome message is in english, else it is in second language like japanese, chinese, etc.

    Any recommended techniques?

    Can I include, a file, say called language.php, and in this file, I have array of messages in multiple languages, like english, japanses, etc:

    $welcome = array ("english" => "welcome", "chinese" =>"xxx xxx xxx");
    $goodday = array ( .....)

    Hence, I can use this array to populate different languages based on user/member's preference ....

    But, is this a good practices? Burden to the server?

    Thanks in advance.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    Madison, WI
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    personally, rather than arrays, i would have a different language file for each language with all the of phrases defined as constants, like:
    PHP Code:
    // english.lan

    define('WELCOME','Welcome to our web site!');
    define('GOODBYE','Thanks for having visited us!');

    // spanish.lan

    define('WELCOME','Bienvenido a nuestro sitio web.');
    define('GOODBYE','Gracias por habernos visitado.'); 
    then just use a switch to include the appropriate file in each page based on the selection of your users

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Mexico
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good places to pick up best practices are in software like mambo where all the terms including menu items and structural information is definned so the definition can vary and you can easily build modules to update the site language.
    A good example is mambo at www.mamboserver.org
    Alexandro Colorado

  4. #4
    SitePoint Addict shrikie's Avatar
    Join Date
    Dec 2002
    Location
    Hyperion
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at smarty template system, that could really help speed things up

  5. #5
    Obsessive designer Infizi's Avatar
    Join Date
    May 2004
    Location
    North Pole
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one that i did, is based on jinkas idea,

    at the top of my page i have my include, this included a link as the one below
    PHP Code:
    require_once $full_path."/languages/".$lang.".lang"
    Then, in my site i used variables to pass everything out, same variables defined in each script so you dont need lots of switches.

    One other way is to use a databse, same idea,, like
    Lang ID, Page_title, text1,link1
    en Hello .. ..
    fr Bonjour!

    Either way will work

  6. #6
    Obsessive designer Infizi's Avatar
    Join Date
    May 2004
    Location
    North Pole
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (by the way, that was where i have a map at load, and my links include a magic id "&lang=en", or fr etc

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ckchin
    Hi,

    Could anyone share the good practices to build multi-language PHP website?
    The best practice is to separate logic and presentation. Something like "welcome message" belongs to the presentation and I see no reason why it should be kept in php files (i.e. "logic").

    Make different templates for each language with your template engine of choice (or bare php) and use what appropriate:

    PHP Code:
    $lang "en";
    if(
             isset(
    $_REQUEST['lang']) && 
             
    in_array($_REQUEST['lang'], $SUPPORTED_LANGS))
       
    $lang $_REQUEST['lang'];

    include 
    "templates/$lang/welcome.php"
    In addition, this allows you to use different layouts, images etc for different languages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •