How to download two mo files for gettext localization

I am trying to solve this problem in order to download two mo files. I have two mo files, I have different msgid and msgstr.

My folder structure is below. local / en _US / LC_MESSAGES / lang.mo local / en_US / LC_MESSAGES / brand.mo

The following code that I use to download a mo file.

define('PROJECT_DIR', realpath('./'));
define('LOCALE_DIR', PROJECT_DIR .'/locale');
define('DEFAULT_LOCALE', 'en_US');
$encoding = 'UTF-8';
$locale = (isset($_SESSION['lang']))? $_SESSION['lang'] : DEFAULT_LOCALE;
// gettext setup
T_setlocale(LC_MESSAGES, $locale);
// Set the text domain as 'messages'
$our_domain = 'lang';
T_bindtextdomain($our_domain, LOCALE_DIR);
T_bind_textdomain_codeset($our_domain, $encoding);
T_textdomain($our_domain);

How can I add another mo file here

+4
source share
2 answers

You can simply use the functions bindtextdomainand textdomainagain to set the new translation file as the default resource.

A good way to simplify would be to create a let say utility function selectTrFile($filename)that makes it look like this:

function selectTrFile($trFile) {
    bindtextdomain($trFile, TRANSLATION_BASE_DIR); // The constant being the path of you translations file directory
    textdomain($trFile);
}

// Now set the first file

selectTrFile('default');

// Do your stuff with this translations file

selectTrFile('anotherTranslationsResource');

// Have fun with it

, , .

function _f($msgid, $trFile){ // called _f to make it very short to type
    selectTrFile($trFile); // Switch the translations file
    return _($msgid); // Return the translated string
}

// Then wrap your translations filenames in short variables
$f1 = 'lang';
$f2 = 'brand';

// now you can translate Strings easily from one file or another.
_f("to be translated in file 'lang'", $f1);
_f("to be translated in file 'brand'", $f2);

, , _f($msgid, $trFile);

function _f($msgid){

     selectTrFile('lang'); // or set a variable containing the filename and use it

     $msgstr = _($msgid);

     if($msgstr == $msgid){ // if the Strings are equals it means that there was no match in the first file
         selectTrFile('brand'); // or set a variable containing the second filename and use it
         $msgstr = _$(msgid);
     }

     return $msgstr;
 }

, , , . , 'lang', , , - .

, . . .mo, .

msgfmt , . .po/.mo. Mo - .

, , .po. , .

: . ( , ), .

, .

##
# File : /your/php/file.php
##
msgid "first String in file.php"
msgstr "first translation in file.php
.
.
.
##
# File : your/other/file.php
##
msgid "other translation"
msgstr "and then you go on"

, , .mo .php, .

. ( PO).

+1

, :

locale/
    en/                    <-- language
       LC_MESSAGES/        <-- category
           messages.mo     <-- domain
           brands.mo       <-- domain

, gettext, . , _('Foo'). :

_('Foo')                                  // LC_MESSAGES/messages.mo
dgettext('brands', 'Foo')                 // LC_MESSAGES/brands.mo
dcgettext('brands', 'Foo', LC_MONETARY)   // LC_MONETARY/brands.mo

, .

+4

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


All Articles