Valerio Bulla
Valerio Bulla

Reputation: 95

PHP gettext not working, how to debug?

I have been trying to set up a PHP website internationalization. No matter how much I try, I cannot get gettext to work.

I am running an ubuntu apache server on a vagrant box, all locales I need are both available and generated (used locales -a to check).

Gettext is installed and enabled (checked with phpinfo())

This is my PHP:

$locale = 'en_US'

putenv('LANGUAGE=' . $locale); //found it somewhere, doesn't make a difference though
putenv('LC_ALL=' . $locale);
echo setlocale(LC_ALL, $locale); //seems to work fine

echo bindtextdomain($locale, "/vagrant/build/locale"); //folder exists and corresponds with return string
textdomain($locale);

echo gettext("Not working!");

This is my /vagrant/build/locale/en_US/LC_MESSAGES/en_US.po file:

msgid ""
msgstr ""
"Language: en_US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "Not working!"
msgstr "ITS WORKIN OMFG!"

Everything seems to check out, except the translation just does not work.

I tried adding .utf-8 (and variations such as .UTF-8 and .utf8) to the locale in putenv(), setlocale() and the folder name, but nothing changed.

How does one debug this kind of stuff? Outputting variables only took me so far, as everything seems to be working.

Is there something I'm missing here? I wasted so much time on this issue, I just wish I had gone with associative arrays instead.

Upvotes: 5

Views: 2519

Answers (2)

user9110002
user9110002

Reputation:

I had the same problem on Windows, the reason was that the OS did not knew the specific languages I was using in gettext. I solved the issue installing all the desired languages in the system's settings.

Please read this note from the php manual:

Note: On Windows, setlocale(LC_ALL, '') sets the locale names from the system's regional/language settings (accessible via Control Panel).

http://php.net/manual/en/function.setlocale.php

I hope this will solve your issue.

Upvotes: 1

Ilia Shakitko
Ilia Shakitko

Reputation: 1457

I've made an attempt helping those who is still looking for this here: PHP gettext doesn't works

In short - your LANGUAGE definition is causing the issue.

Hope that helps.

Upvotes: 0

Related Questions