2

This is more of an analytical question.

I need to know how best to make a multilingual system, a.k.a. a system where the user can change the language. The language will be stored in a cookie or a database.
I've worked in the past with different files for each language, for example:

nl.php

$lang['hi'] = 'Hoi';
$lang['howareyou'] = 'Hoe gaat het?';

en.php

$lang['hi'] = 'Hi'];
$lang['howareyou'] = 'How are you?';

index.php

include($language . '.php');

As you can see, this system is both inefficient and insecure. Is there a better way to do it? I can think of a few ways to do this this instant, but I don't really know which one would be good.

Can anyone help me with this? Please don't just say "Do it like this!", also tell me why it is a good way to do it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
RobinJ
  • 5,022
  • 7
  • 32
  • 61

2 Answers2

4

Well, if you don't need to provide ability to change localization texts via web interface, you can just do it like this:

include/locales.php

<?php
    if (!isset($locales)) {
        $locales = array(
            "en" => array(
                "hi" => "Hi",
                "howareyou" => "How are you?"
            ),
            "nl" => array(
                "hi" => "Hoi",
                "howareyou" => "Hoe gaat het?"
            )
        );
    }
?>

index.php

include("include/locales.php");
if (!isset($locales[$language])) $locale = $locales[$deflang]; // $deflang could be "en"
else $locale = $locales[$deflang];

echo $locale["hi"]." ".$locale["howareyou"];

This is the fastest approach, because parsing single include file with hash is very fast operation. If you need to provide ability to modify localization strings via web interface, then you will need to store localization strings in DB and read em from there each time you show a page... This approach is way more slow.

mephisto123
  • 1,400
  • 13
  • 38
  • ... That's the point. I *need* to be able to change the language easilly via the web interface. – RobinJ Nov 26 '11 at 21:19
  • What's the problem with changing the language via web interface in this approach? I am using your $language variable as identifier of language passed from cookies or wherever you like. I meant you can not change language _strings_, not the language user wants to see page in. – mephisto123 Nov 26 '11 at 21:21
  • But this way seems inefficient somehow :\ Isn't there a way that I can, for example, just make the whole system in dutch/english and that the multilangual system translates everything on the go from strings in another file? – RobinJ Nov 26 '11 at 21:47
  • It will be slower. And more complex. – mephisto123 Nov 26 '11 at 21:52
  • And how would I use this for strings like "Welcome, $user!"? Because obviously I'd need to include the locales file at the top of the script, but the variables user may change after the locales file is included... – RobinJ Nov 27 '11 at 16:08
  • Erm, if user changes locale via web interface, you need to reload the page using new locale... – mephisto123 Nov 27 '11 at 16:39
  • ... This is what I mean: http://codepad.viper-7.com/R0HAWZ (It won't run the code now (502 Bad Gateway), but you get me point?) – RobinJ Nov 27 '11 at 18:05
  • Yes, I get the point. You can put something like %username% into locale string and later replace it with correct name of the user. That is the way I do it in my projects. – mephisto123 Nov 27 '11 at 18:16
  • Ok, I had hoped there was an easier way. Thanks. – RobinJ Nov 28 '11 at 16:59
0

First of all you don't need to have more language in a file. Keep each language in distinct file.

  1. It's cleaner
  2. You can make a script to compare the keys defined. If a file is missing a key, you can alert people to repair this situation.

Don't forget you may keep and other setting in language files like : date format, number formatting,etc/

catalinux
  • 1,462
  • 14
  • 26