views:

246

answers:

5

I am working on a small parser that should accept input in a number of languages. I am going to have maybe 50 strings that will serve as keywords/anchors in 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 then access 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?

A: 

Generally people use l10n (or something similar) in their application because it allows for additional languages by adding a localization file which means the programmers do not have to change code. But if it is a simple script, then full on localization would be overkill.

Seamus
+2  A: 

For a complete translation solution, you can look on a solution like gettext.

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

Don't forget to use some kind of fallback if the term you want to translate doesn't exists in the language, usually the fallback is to the English.

A: 

I have seen your solution used in other OS projects, however the $lang array is usually constructed in different files. For example:

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

and so on for lang.gb.php, lang.de.php, etc.

too much php
A: 

As Peter said, you've got the right idea, but separate your languages into different files. It means that PHP won't have to store the array of every single word in every single language. Because you only are going to be loading one language at a time, you can skip the $lang['us'] level of nesting too.

You could 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 basic class which handles all this for you: detect the locale, choose the correct file, have "error" handling, if a term isn't defined in a particular language, etc.

nickf
A: 

You probably don't want 'totaldebt' => 'Total Debt' as that will ultimately obfuscate your code, and is slower than just storing 'Total Debt' as the hash key. The gettext way of doing things is to just wrap the strings in a function.

so rather than:

echo "Color";

You do:

echo t("Color");

the t() function will look at the globally defined language, and make the substitution if possible. At some point, you may want to use gettext and doing it this way will be compatible.

John C