phpgettextphp-gettext

can't translate to any other language using gettext php function


I use a WAMP server on Windows and make a test project. I have these files in the website folder:

C:\wamp\www\project\locale\ar_EG\LC_MESSAGES\messages.po C:\wamp\www\project\locale\en_US\LC_MESSAGES\messages.po

Below is the PHP Code:

$language="en_US";
$encoding = "UTF-8";
putenv("LANG=".$language);
setlocale(LC_ALL,$language);
$domain="messages"; // name of PO file
bindtextdomain($domain,"Locale");
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
echo gettext("name");

Above code is working fine. When I try to replace en_US with ar_EG it also displays the translation for en_US, and when I try to remove the en_US folder and try again it displays the msgid not msgstr.

I searched and found that there is a problem with setlocale on the Windows platform, but I need a solution for it to run on Windows.


Solution

  • setlocale() on Windows has issues.

    From setLocale() in PHP Manual:

    Windows users will find useful information about locale strings at Microsoft's MSDN website. Supported language strings can be found in the » language strings documentation and supported country/region strings in the » country/region strings documentation.

    You might try to use $language = 'english-us'; instead of $language = 'en_US';.

    I'm not sure about the language code for ar_eg, maybe Arabic_Egypt or Arabic_Egypt.1256.

    If that doesn't work, you still have the option to use a PHP library, which implements gettext, like https://github.com/oscarotero/Gettext or https://launchpad.net/php-gettext.