[TUTORIAL] Multi-Language WebSites

Discussion in 'Tutorials' started by DarkDragonLord, Nov 9, 2007.

  1. DarkDragonLord

    DarkDragonLord New Member

    Messages:
    782
    Likes Received:
    0
    Trophy Points:
    0
    Greetings guys!

    Finally i could manage to find a great script that supports Multi-language websites WITHOUT databases :D
    As always, no english tutorial could help me and never worked. So i've searched in my home language and gotcha, i found :p

    Very nice for small / mid website but might be a little hard to manage in Big websites.

    It uses only 1 + 1*x files (where X is the number of languages you want) then, 2 languages are 3 files.

    Credits goes to user Cerrito (MRS) from " iMasters Fóruns " ( http://forum.imasters.com.br/index.php?showtopic=124045 ) that created the code
    And zé_violeiro ( http://forum.imasters.com.br/index.php?showtopic=224115 ) that changed code to make it work with images instead of a Drop Down Menu.


    I've translated most of the comments xD

    Ok, Lets start!

    First, create a file named setlanguage.php
    PHP:
    <?php
    /*** BY MRS 05/06/2004 ****\
    * *** *
    *  this file check if the user choosed a language. If true, it changes and saves a cookie for that setting  *
    * with a 1month duration, so next time it comes, already is in the language he previously set.  * 
    * if false, it check for a language cookie, if dont have, it get the browsers idiom and set as it *
    * +++ *
    /******************************************************** */

     
    $texto['portuguese']          = "Portugues";
     
    $texto['english']          = "English";
     
    $texto['spanish']          = "Espanol";

    // here you add the languages.

    ob_start();
    $default "en"// default will be english...

    if( isset( $_GET['lang'] ) )
    {
    require 
    $_GET['lang'] . ".php";
    setcookie("saveLang"$_GET['lang'], time()+3600*24*30'/'); // cookie that will expire in 1 month
    }
    else
    if( isset(
    $_COOKIE["saveLang"]) )
    {
    require 
    $_COOKIE["saveLang"] . ".php";
    }
    else
    if (!isset(
    $_SERVER['HTTP_ACCEPT_LANGUAGE']))
    {
    require 
    $default .".php";
    }
    else
    {
    $language $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    $language substr($language,0,2); // splitting languages....
    if( ( $language == "pt") or ( $language == "es") or ( $language == "en") ) // checking if language is valid .. here you can change or add the languages
    {
    require 
    $language".php";
    }
    else
    {
    require 
    $default ".php";
    }
    }
    ob_end_flush();
    ?>

    Now, to the language files!
    en.php or es.php or pt.php or anything .php.. Just remember of making sure to add it at setlanguage.php where check the language if valid.

    pt.php
    PHP:
    <?
        
    $texto['titulo']               =   "Informações do sistema";
        
    $texto['usada']                =   "Usado";
        
    $texto['livre']                =   "Livre";
        
    $texto['espacoTotal']          =   "Tamanho";
        
    $texto['dataHora']             =   "Data/Hora";
        
    $texto['nomeDaMaquina']        =   "None do servidor";
        
    $texto['dominio']              =   "Dominio";
        
    $texto['host']                 =   "Host";
        
    $texto['uptime']               =   "Tempo de vida";
        
    $texto['numeroProcessos']      =   "Numero de Processos";
        
    $texto['sistemaOperacional']   =   "Sistema operacional";
        
    $texto['vKernel']              =   "Versão do Kernel";
        
    $texto['usuarios']             =   "Usuários";
    ?>
    And en.php
    PHP:
    <?php
    $texto
    ['titulo']               = "System Information";
    $texto['usada']                = "Usage";
    $texto['livre']                = "Free";
    $texto['espacoTotal']          = "Space total";
    $texto['dataHora']             = "Date/time";
    $texto['nomeDaMaquina']        = "Computer Name";
    $texto['dominio']              = "Domain";
    $texto['host']                 = "Host address";
    $texto['uptime']               = "Server time life";
    $texto['numeroProcessos']      = "Processors number";
    $texto['sistemaOperacional']   = "Operational System";
    $texto['vKernel']              = "Kernel Version";
    $texto['usuarios']             = "Users";

    ?>

    explanation:
    $texto['VARIABLE_YOU_WANT'] = "The meaning of the variable";

    a better example:
    You want to have all your Menu links to be language changeable.
    so you make:

    PHP:
    FOR EN.PHP

    <?php
    $texto
    ['home']               = "Home";
    $texto['downloads']                = "Downloads";
    $texto['support']                = "Support";
    $texto['contact']          = "Contact";
    ?>

    FOR PT.PHP

    <?php
     $texto
    ['home']               = "Página Inicial";
     
    $texto['downloads']                = "Downloads";
     
    $texto['support']                = "Suporte";
     
    $texto['contact']          = "Contato";
    ?>
    simple isn't it? always make the variable inside [' '] same in every languages

    Ok, next step.


    In the index.php (or the page you want) add at beggining:

    PHP:
    <?php
    include "setlanguage.php";
    ?>
    For the Change Language Links / Images is this:
    HTML:
    <a href="<?php $_SERVER['PHP_SELF'] ?>?lang=pt"><?php echo $texto['portuguese']; ?></a>
    
    <a href="<?php $_SERVER['PHP_SELF'] ?>?lang=en"><?php echo $texto['english']; ?></a>
    
    the $_SERVER is to make the php get itself, this way you dont need to write the php file name all time (so if you want to rename it later, you'll have to change in the links) and the php and echo is used with those arrays you set up at beggining of setlanguage.php. If you want a image instead of a name, just put

    HTML:
    <a href="<?php $_SERVER['PHP_SELF'] ?>?lang=en"><img src="IMAGE" border="0"></a>
    With all set up, now lets add our variables at where you want.



    a common menu:
    HTML:
    <a href="<?php $_SERVER['PHP_SELF'] ?>?id=home">HOME</a> <br />
    <a href="<?php $_SERVER['PHP_SELF'] ?>?id=downloads">DOWNLOADS</a> <br />
    <a href="<?php $_SERVER['PHP_SELF'] ?>?id=support">SUPPORT</a> <br />
    <a href="<?php $_SERVER['PHP_SELF'] ?>?id=contact">CONTACT</a> <br />
    
    Now, the new menu that will change the names if changed language:
    HTML:
    <a href="<?php $_SERVER['PHP_SELF'] ?>?id=home"><?php echo $texto['home']; ?></a> <br />
    <a href="<?php $_SERVER['PHP_SELF'] ?>?id=downloads"><?php echo $texto['downloads']; ?></a> <br />
    <a href="<?php $_SERVER['PHP_SELF'] ?>?id=support"><?php echo $texto['support']; ?></a> <br />
    <a href="<?php $_SERVER['PHP_SELF'] ?>?id=contact"><?php echo $texto['contact']; ?></a> <br />
    
    Its done!
    When you change the languages, the menu automatically changes the names ^^


    I think this could be made with all texts too but you will have to write each text twice, one in english one in the other language then add the <?php echo $texto['VARIABLE']; ?>


    I hope this help most of people to make a Multi-language website.

    I know, i could charge everyone to make it work for you but i think its better this way. (but i still can do for you if you are lazy enough for some credits :D)


    see you ^^



    If you think this tutorial post was helpful, click in the reputation button in the beginning of this post, in the right side ( [​IMG] ).
    And if you think it worth something i translated and shared it with you, i would be grateful if you donate some credits ^^
     
    Last edited: Nov 9, 2007
  2. Slothie

    Slothie New Member

    Messages:
    1,430
    Likes Received:
    0
    Trophy Points:
    0
    I prefer using getText for localization in most of my apps but yeah, nice tutorial.
     
  3. ikarus

    ikarus New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
  4. eminemix

    eminemix Member

    Messages:
    350
    Likes Received:
    0
    Trophy Points:
    16
  5. DarkDragonLord

    DarkDragonLord New Member

    Messages:
    782
    Likes Received:
    0
    Trophy Points:
    0
    Tomorrow i'll make an Add-on tutorial of this one showing how use this variables for images, and how make them detect the browser's language using part of the setlanguage.php code.
     
  6. cool_dude_99

    cool_dude_99 New Member

    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
  7. LHVWB

    LHVWB New Member

    Messages:
    1,308
    Likes Received:
    0
    Trophy Points:
    0
    Nice system.
    I use the inbuilt system with SFM to do my language support,it automatically allows you to select languages, and I have a language bar for the user to change the language. It has a very similar system where you can change and add text to the $txt array in different files for different languages.

    The easiest option for multi-language support is probably to integrate your site with a user system that already has the language, but you system's nice because it doesn't need the user system.
     
  8. tittat

    tittat Active Member

    Messages:
    2,479
    Likes Received:
    1
    Trophy Points:
    38
    any way nice tutorial.But i didn't tried to setup script now.will do later.*******repped*******
     
    Last edited: Mar 29, 2008
  9. ThePaintGuru

    ThePaintGuru New Member

    Messages:
    208
    Likes Received:
    0
    Trophy Points:
    0
    This is a very helpful (and timely) tutorial for me, I'm just in the process of coding a new website in English and Spanish. Will have to work on my translation skills, though :).
     
  10. LHVWB

    LHVWB New Member

    Messages:
    1,308
    Likes Received:
    0
    Trophy Points:
    0
  11. DarkDragonLord

    DarkDragonLord New Member

    Messages:
    782
    Likes Received:
    0
    Trophy Points:
    0
    Just an update:

    Here is a code using the 'checking browser language' code to make links with the lang, so when our visitor change the page, the lang keeps on :)

    As example, my Portfolio link.
    Code:
    <a href="http://raphaelddl.com/?ddl=Portfolio<?php if( isset( $_GET['lang'] ) ){echo "&amp;lang=" . $_GET['lang'];}else{if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){echo "&amp;lang=en";}else{$idioma = $_SERVER['HTTP_ACCEPT_LANGUAGE']; $idioma = substr($idioma,0,2); if( ( $idioma == "pt") or ( $idioma == "en") ) {echo "&amp;lang=" . $idioma;}else{echo "&amp;lang=en";}}} ?>"><?php echo $texto['Portfolio']; ?></a>
    
    so, ?ddl=portfolio - If the lang already at url, print &lang={variable of URL} if not, check the browser language, check if have any of the ones available and print &lang={the browser lang}. Else, print &lang= en (english - 'default' one).
     
  12. kkenny

    kkenny Active Member

    Messages:
    1,950
    Likes Received:
    0
    Trophy Points:
    36
    Very Nice.
    I might need this sometime soon. :]
     

Share This Page