Translate multiple languages ​​in php

I am working on a small parser that needs to accept input in multiple languages. I will have maybe 50 lines to serve as keywords / anchors when parsing the input. In PHP, what would be the best way to organize these translated keywords?

One possible solution that comes to my mind is to use an associative array. Like this:

$lang = array('us' => array('totalDebt' => 'Total Debt', 'color' => 'Color'),
              'gb' => array('totalDebt' => 'Total Debt', 'color' => 'Colour'))

      

which I could get using the following:

$langCode = 'en';
$debtPos = strpos($lang[$langCode]['totalDebt']);

      

Are there any better, proven methods for dealing with a bunch of short strings translated into a bunch of languages?

0


source to share


5 answers


For a complete translation solution, you can find a solution like gettext .

Your solution is good enough (fast, cheap in resources) for small dictionaries. I didn't understand what you were trying to do using the strpos () function.



Do not forget to use some kind of fallback if the term you want to translate does not exist in that language, usually the backup is in English.

+2


source


Usually people use l10n (or something similar) in their application because it allows additional languages ​​to be added by adding a localization file, which means programmers don't need to change the code. But if it's a simple script, then full on localization would be overkill.



0


source


I've seen your solution used in other OS projects, however the array is $lang

usually created in different files. For example:

<?php // lang.us.php
$LANG['us'] = array(
                    'totalDebt' => 'Total Debt',
                    'color' => 'Color',
                   );

      

etc. for lang.gb.php, lang.de.php, etc.

0


source


As Peter said, you have the right idea, but separate your languages ​​from different files. This means that PHP doesn't have to store an array of every word in every single language. Since you are only going to download one language at a time, you can also skip the nesting level $lang['us']

.

You can even "extend" languages:

<?php // lang.en-gb.php
$lang = array(
    'color' => "Colour",
    'totalDebt' => "Total Debt",
    ...
);
?>

<?php // lang.en-us.php
include('lang.en-gb.php');

$lang['color'] = "Color";
// don't need to redefine "totalDebt"
?>

      

You might even consider creating a base class that handles all of this for you: determine the locale, select the correct file, handle the "error" if the term is not defined in a particular language, etc.

0


source


You probably don't need "totaldebt" => "Total Debt" as this will end up obfuscating your code and will be slower than just storing "Total Debt" as a hash key. The gettext method is simply to wrap the strings in a function.

but not:

echo "Color";

      

You do:

echo t("Color");

      

the t () function will look at the globally defined language and make a replacement if possible. You may need to use gettext at some point and it will be compatible.

0


source







All Articles