user1002039
user1002039

Reputation: 860

PHP localization

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!

Upvotes: 4

Views: 17237

Answers (3)

feeela
feeela

Reputation: 29932

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.

Upvotes: 3

user1299518
user1299518

Reputation:

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");

Upvotes: 8

dynamic
dynamic

Reputation: 48091

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");

Upvotes: 0

Related Questions