i am trying to use the php gettext extension in php 5.5 (on win2008 server, using IIS7). I am doing this:
<?php
$locale = "es";
if (isSet($_GET["locale"])) $locale = $_GET["locale"];
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
textdomain("messages");
echo gettext("Hello world");
?>
With this folder structure in place:
locale/es/LC_MESSAGES/messages.mo
But it always just returns Hello world and not the correct translation which for now (based on my lack of spanish skills) is this in the messages.po file:
msgid ""
msgstr ""
"Project-Id-Version: TestXlations\n"
"POT-Creation-Date: 2014-04-19 08:15-0500\n"
"PO-Revision-Date: 2014-04-19 09:18-0500\n"
"Language-Team: \n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.6.3\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-SearchPath-0: c:/dev\n"
msgid "Hello world"
msgstr "Hola World"
This fails from the cmd line and via IIS. So i it's seeing the gettext call, etc and executing it but it's not reading the translation file. how can i debug this further? even if remove the translation file, i get the same behavior.
As per not knowing which language pack to use on the OS, thankfully the
setlocale()
function allows for an array. As per the PHP Docs:With this, you can dig to find out locale the OS is falling back to by checking the set after:
$setTo
will be provided with the$locales
value that was successful. This might be helpful when finding which locale to write a.po
for.As per written in my comments, I had the issue where I was not performing this
setlocale()
at the top of each script of every page request, as you'll need to retain the user's choice of language by session or database value. As I believed naively once it was set, it was set forever!You should check returned values and know which function failed. It is not i18n specific but useful for any PHP scripts, or any programming language debugging.
Do you find "es" in the output of 'locale -a' ? If not then you need to run the following command.
It's a bit old question but here goes this answer in the hope of being useful for someone.
Try changing
LC_ALL
toLANG
in theputenv
function, from this:to this:
In the mac it was given problems with LC_ALL and probably it the same here .
Also check this answer in other cases https://stackoverflow.com/a/3535866/6628843
Hope it helps!
Try to set also env vars
LC_LANG
andLC_LANGUAGE
not only theLC_ALL
:Having the same problem on Linux, I came to this conclusion: even if you provide your own *.mo files for your project, the locale itself (es) must be known to the operating system.
Installing the requested locale at a system level fixed the problem for me.
See: locale-gen
It might not help with actually debugging gettext, but at least it's something you can try.