Search code examples
phplocalizationgettext

gettext ignores .mo file


I'm trying to make get text working, and everything seems to be ok, but it wouldn't translate my text anyway. I read about 5 different posts on stackoverflow and another 5 on google - nothing helps. Here is what I have now:

  1. I took php file from php.net

  2. I took po file example from here

  3. I took gettext binaries from here

These are the files which I end up using to make sure there can not be any error:

<?php
// Set language to English
putenv('LC_ALL=en');
putenv("LANGUAGE=en");
putenv("LC_MESSAGES=en");
putenv("LANG=en"); 
setlocale(LC_ALL, 'en');

// Specify location of translation tables
echo bindtextdomain("myPHPApp", "./locale");
echo "<br>";

// Choose domain
echo textdomain("myPHPApp");
echo "<br>";

// Translation is looking for in ./locale/en/LC_MESSAGES/myPHPApp.mo now

// Print a test message
echo gettext("Welcome to My PHP Application");
echo "<br>";

// Or use the alias _() for gettext()
echo _("boolean_key");
echo "<br>";

echo _("key_with_description");
echo "<br>";


var_dump(_("key_with_description"));

echo setlocale(LC_ALL, 0);
echo "<br>";
echo getenv('LC_ALL');
echo "<br>";
echo getenv('LANGUAGE');
echo "<br>";
echo getenv('LC_MESSAGES');
echo "<br>";
echo getenv('LANG');

echo phpInfo();

?>

locale\en\LC_MESSAGES\myPHPApp.po:

msgid ""
msgstr ""
"Language: English\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: PhraseApp (phraseapp.com)\n"

msgid "boolean_key"
msgstr "--- true"

msgid "empty_string_translation"
msgstr ""

# This is the amazing description for this key!
msgid "key_with_description"
msgstr "Check it out! This key has a description! (At least in some formats)"

msgid "key_with_line-break"
msgstr "This translations contains\na line-break."

msgid "nested.deeply.key"
msgstr "Wow, this key is nested even deeper."

msgid "nested.key"
msgstr "This key is nested inside a namespace."

msgid "null_translation"
msgstr ""

msgid "pluralized_key"
msgid_plural ""
msgstr[0] "Only one pluralization found."
msgstr[1] "Wow, you have %s pluralizations!"

msgid "sample_collection\n"
msgstr "---\n- first item\n- second item\n- third item\n"

msgid "simple_key"
msgstr "simple key, simple message, so simple.2"

#, fuzzy
msgid "unverified_key"
msgstr "I need verification, please verify me! (In some formats we also export this status)"


msgfmt en\LC_MESSAGES\myPHPApp.po --output-file=en\LC_MESSAGES\myPHPApp.mo

the output I get is:

D:\Work\locale
myPHPApp
Welcome to My PHP Application
boolean_key
key_with_description
string(20) "key_with_description" en
en
en
en
en 

As you can see putenv, setlocale, bindtextdomain - all commands are working as expected.

I also check with phpInfo that gettext and mbstring extensions are detected. enter image description here

I also tried to restart apache.

I also tried to do the same, using 'de' instead of 'en'. Also I tried to use 'ru' and 'en-US'. All give me the same result. 'en_US' (with underscore) breaks even earlier, setlocale ignores it and the value of LC_ALL is left to be LC_COLLATE=C;LC_CTYPE=Russian_Russia.1251;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C.

I have english and russian installed on my system. enter image description here

What can be wrong here?

I'm using windows 7, PHP 5.6.40.


Solution

  • This article has helped me. The solution was to set LANG=en in shell and then to run XAMPP from this exact shell.

    Finally, to get gettext working in XAMPP, you need to set the LANG attribute on the XAMPP shell before you fire up the XAMPP Control Panel every time you want to use XAMPP.

    The XAMPP shell is right inside the install folder, which means it should be at C:\xampp\xampp_shell.bat for most people. Run it (preferrably as an Administrator), and type the following:

    set LANG=YOUR_LANGUAGE_CODE

    YOUR_LANGUAGE_CODE needs to be replaced with the appropriate language code of your localisation. For example, if my localisation was for Simplified Chinese, it would look like this:

    set LANG=zh_CN

    Once done, use the following command to open the XAMPP Control Panel from the shell.

    xampp-control