PHP localization

I am working on a small project that consists of registration, login, reset password and user management on the internal server. I have to create translation files for different languages, and instead of using something like gettext (which I don’t know anything about), I decided to implement a very simple method using a static array for each language file as follows:

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 great, very fast at this point, but now my markup is dotted with php tags, and I'm not sure I made the right decision. I have never done this before, so I have no idea what I'm looking forward to. It also seems that by the time I'm done, this file will be a mile long.

Is this a good way to do this? Is there a better way you could suggest?

!

+3
3

, cms:

  • each // ( ), , /translations.
  • , el.txt, de.txt, uk.txt ..
  • JSON, , .
  • UTF8 , . ( JSON.parse )
  • , . ( .. a data column of TEXT datatype)
  • json_decode() , ; $_ SESSION, flash .

, .

:

ru.txt

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

de.txt

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

, $_ SESSION [ "language" ]; . , :

lang("LNG_1");
+5

, . (require('lang.en.php')).

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

// lang.ru.php
$languageStrings = array(
    'login' => '  ',
);

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

(, ), - .

: IETF .

+2

, .
, - :

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

. php , :

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

- gettext http://php.net/manual/en/book.gettext.php

:

// alias _() for gettext()
echo _("Have a nice day");
0

Source: https://habr.com/ru/post/1688508/


All Articles