PHP localization

localization, php

Solution

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

Problem

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!

Original source

Related problems