How to debug gettext not working in PHP? How to debug gettext not working in PHP? php php

How to debug gettext not working in PHP?


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.

<?php$locale = 'es';if (isset($_GET["locale"])) $locale = $_GET["locale"];$domain = 'messages';$results = putenv("LC_ALL=$locale");if (!$results) {    exit ('putenv failed');}// http://msdn.microsoft.com/en-us/library/39cwe7zf%28v=vs.100%29.aspx$results = setlocale(LC_ALL, $locale, 'spanish');if (!$results) {    exit ('setlocale failed: locale function is not available on this platform, or the given local does not exist in this environment');}$results = bindtextdomain($domain, "./locales");echo 'new text domain is set: ' . $results. "\n";$results = textdomain($domain);echo 'current message domain is set: ' . $results. "\n";$results = gettext("Hello world");if ($results === "Hello world") {    echo "Original English was returned. Something wrong\n";}echo $results . "\n";


Do you find "es" in the output of 'locale -a' ? If not then you need to run the following command.

 sudo locale-gen es


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.