views:

60

answers:

4

Hello

I'm creating an open-source cms and was just wondering that which is the best way to add localizations? I already decided to have them in files similar to lang.en.php. I would assume arrays, but in which form?

$lang['xyz'] = "Text goes here!";
$lang['Text goes here!'] = "Translated text!";

Or should I create my custom parser and add localizations to a file, like this:

"Text goes here!" = "Translated text!";

And then just parse it.

What would you suggest? I tried to search but no results for me.

Martti Laine

A: 

I know the Gettext library for Desktop applications does something similar to your custom parser. Gettext has a module in PHP, but I'm not sure if it's installed in most PHP installations by default.

Basically, you would write every string with it with a function name tr("How are you?"). Then create a function to translate it:

include('lang.es.php');
function tr($txt) {
   global $tr;
   if(array_key_exists($txt,$tr)) {
     return $tr($txt);
   }
   return $txt;
}

And in lang.es.php, have:

$tr = array();
$tr["How are you?"] = "¿Como Estas?";

You would probably want to do printf(tr("How are you, %s?"), $name); for variables, or proper nouns that should not be translated.

bradlis7
Thanks, this is the way I've always done it.
Martti Laine
A: 

I think you should use the Joomla way. Language files must be in ini extension:

FOO=translation
BAR=translation2

then you parse the file with parse_ini_file function and get the translation array:

$dictionary=parse_ini_file("english.ini");

    function translate($text)
    {
       global $dictionary;
       if(isset($dictionary[strtoupper($text)])) return $dictionary[strtoupper($text)];
       else return $text;
    }
mck89
A: 

It's not as simple as you think it is, do you really need hundreds of rows in an array in order to translate I deleted 45 comments, or I deleted 192 comments? etc.

It would be very helpful if you could call a translate function with: translate('I deleted %d comments', $number);

<?php
    $dict = parse_ini_file('lang.ini');

    function translate($text){
        global $dict;
        $args = func_get_args();
        if(isset($dict[$text])){
            // I am not sure how to convert %d in $args[.], maybe someone else could provide a regular expression for this.
        } else {
            return $text;
        }
    }
?>
Harmen
A: 

How will you manage plural form ?

Some languages have very tricky plural rules : example here

In Polish we use e.g. plik (file) this way:

      1 plik
      2,3,4 pliki
      5-21 pliko'w
      22-24 pliki
      25-31 pliko'w

For this reason, I suggest you to use gettext because everything has been done for you.

Luc M
Do every server support gettext? This is gonna be an open-source so it should work on most servers.
Martti Laine
If not, you could expand on my answer :). It works similar to how gettext does. You would just need to add another function. The good thing about gettext, is that there is a way (at least in Python) to extract every string that needs to be translated, but you could write a script to do that I think.
bradlis7