How to make a template multilanguage?

Discussion in 'PHP' started by clades, Feb 6, 2011.

  1. #1
    Is there any efficient way to make a template multilingual?

    For example:

    
    <div>
         <span class="red">This is red text</span><br>
         <div class="mar5">
             Another 
         </div>
    </div>
    
    Code (markup):
    I could make it like this (adding tokens for replacement) but then it would lose the text and would be hard for other person to customize The template due to lack of text references

    
    <div>
         <span class="red">{text_55}</span><br>
         <div class="mar5">
             {text_56}
         </div>
    </div>
    
    Code (markup):
    Any other ways?
     
    clades, Feb 6, 2011 IP
  2. moads

    moads Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    fopen a connection to google translate and parse out the result as a variable.
     
    moads, Feb 6, 2011 IP
  3. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you need to use the google api to translate the page
     
    srisen2, Feb 6, 2011 IP
  4. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #4
    The following can be done like that or from a database. It really depends on how much text you'll be translating and how frequently you'll be doing it. This presumes a default of english:
    
    session_start();
    if(!$_SESSION['lang']){
        $_SESSION['lang'] = 'en';
    }
    
    $translate_title = array(
        'en' => 'Page Title',
        'fr' => 'Titre Page',
        'no' => 'Sidetittel'
    );
    
    $translate_body = array(
        'en' => 'Your text here...',
        'fr' => 'Votre texte ici...',
        'no' => 'Din tekst her...'
    );
    
    echo "<h1>".$translate_title[$_SESSION['lang']]."</h1>
    <p>".$translate_body[$_SESSION['lang']]."</p>";
    PHP:
    if bokmål was selected, the following would echo:
    <h1>Sidetittel</h1>
    <p>Din tekst her...</p>
    Code (markup):
    In regards to using Google's API; why be reliant on them? I think it's a fantastic service but why be reliant on them? If it isn't much text at all, which it sounds like (as it's a template), the developer'd be mad to repeatedly grab the same translation from The G. Also, remember that Google's translations can change form and length. This could break layout and buttons. If you have it pre-translated, you can confirm that spacing works ahead of time. :)
     
    Alastair Gilfillan, Feb 6, 2011 IP
  5. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yes that is one method but to pre translate each line of text seems much more burdensome then to simple use googles api.

    At least with the api you get a increases standard of quality as google updates its database where as with a straight translation you will have more code and will have to manually update everything that many times if you wish to change anything on your site
     
    srisen2, Feb 7, 2011 IP
  6. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #6
    It's certainly more effort to start with but it means you aren't connecting to Google for every single word of a template on every single page load across the entire site, unless you're storing each translation with JavaScript for the session which requires even more work! You're either wasting your server's or the browsers' bandwidth and slowing down every page on your site.

    For this application, I can't agree about updated translations: 'text' is always going to be 'tekst' in norwegian. There's more chance of human extinction than out-dated translates for such minimal text as a template. If it were large sections of text like entire articles, I'd be behind you 100% that using an API at least for periodic, stored translations.

    On the topic of periodic translations; if my translation array example was put into a database, the API could be implemented as a cron job so that each translation can be updated automatically, preiodically and so the user doesn't have to make a manual array such as mine but simply:
    
    $words_to_translate_in_template = mysql_query("SELECT * FROM words WHERE lang='en'");
    while($word = mysql_fetch_assoc($words_to_translate_in_template)){
        $template_languages = array('da', 'no', 'sv');
        foreach($template_languages as $language){
            $translated_word = google_translate_api($language, $word);
            mysql_query("INSERT INTO words ('en_word','lang','translated_word') VALUES('$word', '$language', '$translated_word')");
        }
    }
    
    PHP:
    Put in the effort now or have a slow site needing to query Google for its menu names. If there's only a very small proportion of non-native users then by all means go with live translations because they'll rather slow translations than none at all.
     
    Alastair Gilfillan, Feb 7, 2011 IP
  7. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I agree for small sections of text and a limited number of languages this approach may be a better solution. I know there are some translators that are worse than other which is why i would bring up a better version of a translation being available in the future as the current one could be lacking proper grammer for instance in some cases.
     
    srisen2, Feb 8, 2011 IP
  8. clau

    clau Guest

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can also have a language file where you store all the text that can be translated.

    For example you can have the files like /language/en.php, /language/fr.php and so on and in each you can have
    
    <?
    define("STRING1","This is the first string");
    define("STRING2","This is the second string");
    .
    .
    .
    ?>
    
    PHP:
    In the template file you can then simply call for the string you want with

    <? echo STRING1; ?>
    PHP:
     
    clau, Feb 8, 2011 IP