4

I am working on a small project that consists of registration, login, password reset and user management on the back-end. I have to create translation files for different languages and instead of using something like gettext (which I know nothing about), I decided to implement a very simple method using a static array for each language file like this:

function plLang($phrase) {
    $trimmed = trim($phrase);
    static $lang = array(
    /* -----------------------------------
    1. REGISTRATION HTML
    ----------------------------------- */
    'LNG_1'     => 'some text',
    'LNG_2'     => 'some other text',
    etc. ...
    );

    $returnedPhrase = (!array_key_exists($trimmed,$lang)) ? $trimmed : $lang[$trimmed];
    echo $returnedPhrase;
}

It works fine, it is very fast at this stage but my markup now is littered with php language tags and I'm not sure I've made the right decision. I've never done this before so I have no idea what I'm looking forward to. It also seems that by the time I am all done, this file is going to be a mile long.

Is this a good way of doing this? Is there a better way you could suggest?

Thank you!

user1002039
  • 860
  • 4
  • 17
  • 31
  • In my opinion YAML format is better solution for the localization. Please look at my question here: http://stackoverflow.com/questions/9062753/yaml-internationalization – Oyeme Jun 10 '12 at 18:49

3 Answers3

8

this is what im doing in my cms:

  • for each plugin/program/entity (you name it) i develop, i create a /translations folder.
  • i put there all my translations, named like el.txt, de.txt, uk.txt etc. all languages
  • i store the translation data in JSON, because its easy to store to, easy to read from and easiest for everyone to post theirs.
  • files can be easily UTF8 encoded in-file without messing with databases, making it possible to read them in file-mode. (just JSON.parse them)
  • on installation of such plugins, i just loop through all translations and put them in database, each language per table row. (etc. a data column of TEXT datatype)
  • for each page render i just query once the database for taking this row of selected language, and call json_decode() to the whole result to get it once; then put it in a $_SESSION so the next time to get flash-speed translated strings for current selected language.

the whole thing was developed having i mind both performance and compatibility.

in your case a row in such file could be like:

in en.txt

{"id":"LNG_1","str":"My word"}

in de.txt

{"id":"LNG_1","str":"Mein Wort"}

The current language could be stored in session like $_SESSION["language"]; and use that as starting point. Then, you can read translations like:

lang("LNG_1");
  • This is super good info. Thank you! I decided to implement this and it works really good. The one tiny problem is that I can't add comments to json which makes them a bit hard to sort through. – user1002039 Jun 13 '12 at 21:39
  • Thanx. I solved the comment issue by adding a '#' in front of a line. I just test the first character of each line parsed (step 5 of procedure) and in case of this character, i just 'continnue' to the next line in the loop. A valid JSON opens with '{' so the '#' works well as first character. –  Jun 13 '12 at 23:41
  • I have over 3000 translations per language. How big this array can be ?? Is there a limit ? – lio Nov 21 '18 at 19:51
3

Many frameworks store those language-arrays in separate files, wherein each array in any language has the same name. Your language-function then just requires the appropriate file for the user-selected language (require('lang.en.php')).

// lang.en.php
$languageStrings = array(
    'login' => 'Log in',
);

// lang.ru.php
$languageStrings = array(
    'login' => 'Авторизовать в системе',
);

// lang.ja.php
$languageStrings = array(
    'login' => 'ログイン',
);

Which language currently is in use (e.g. selected by the user), can be determined via some globally accessible variable.

Idea: Use IETF language tags as language keys.

feeela
  • 29,399
  • 7
  • 59
  • 71
0

You should use a template engine that supports internazionalization.
My own template engine for example allows me to do something like:

<p>(_text to be localized here)</p>

And the text inside that marker will be translated. This avoid to open every time the php tags in the template file like:

<p><?php plLang('lang'); ?></p>

The standard solution anyway is to use gettext http://php.net/manual/en/book.gettext.php

Example:

// alias _() for gettext()
echo _("Have a nice day");
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • You mean you don't have language file with the translation you want to display? Auto translation? – user1002039 Jun 10 '12 at 18:47
  • I have language file but are located somewhere else, of course. – dynamic Jun 10 '12 at 18:47
  • 1
    Well, then what I have is the same exact thing. – user1002039 Jun 10 '12 at 18:48
  • I'm just using tags like this: plLang('LNG_2'); – user1002039 Jun 10 '12 at 18:50
  • @yes123 A template engine is minor important to localization. An application should know which language is used and pass the translated strings to the tpl-engine. From your answer: `_('some string')` is a call to one Wordpress core function, but you can retrieve translated strings on other methods, as WP knows how to handle localized strings and not the template (which may be created by a third party). – feeela Jun 10 '12 at 20:47