How to debug gettext not working in PHP?

Asked
Active3 hr before
Viewed126 times

8 Answers

working
90%

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.,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:,To my surprise, using gettext inside a PHP script when ran from the command line actually worked as expected, in contrast to when it was ran by Apache. After running the command locales from the terminal, I noticed that the LANGUAGE environment variable was in fact set to an empty string. While when ran by Apache it was set to en_US:en for whatever reason, as I discovered by using the getenv function.,I too had that issue after upgrading Debian from Stretch to Buster, and I've spent an embarrassing long time trying to debug this. I'm not exactly sure why, but in my case I managed to fix the issue simply by adding the following line in my code,

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";
load more v
88%

Make sure gettext is installed and enabled on PHP,Make sure gettext and iconv are installed,General TipsMake sure gettext is installed and enabled on PHPSet the language for your HTML documentSpecify your character set using bind_textdomain_codeset(),Set the language for your HTML document

Otherwise, you’ll have to install gettext and enable it. If you’re on XAMPP, your PHP installation should already have gettext — you’ll only need to enable it. Find php.ini on XAMPP, and search for this line:

;
extension = gettext
load more v
72%

The locale has to match one that's available on your system. Are you sure "fr" is valid? (check against the system locales). I got this working recently (I wasn't that easy as debugging is very hard) but I recall the locales had names like en_GB.utf8 not just two chars. Also it doesn't work at all on Windows but I guess you know that.,"Setting a language isn't enough for some systems and the putenv() should be used to define the current locale.",Why does it have to match? I don't actually want to use any of the translations the system has provided... I want to use my own locale files...,Try LC_ALL instead of LC_MESSAGES. Since LC_ALL has precedence over all of the other LC_ variables, it's possible that your environment already has LC_ALL set, overriding whatever you set LC_MESSAGES to.

Here's my ./locale/fr/LC_MESSAGES/messages.po file:

msgid "NO GOOD"
msgstr "IT WORKED!"

Command used to generate the .mo file:

$ msgfmt - o locale / fr / LC_MESSAGES / messages.mo locale / fr / LC_MESSAGES / messages.po

My PHP file (being run via CLI, for testing purposes, as root).

< ? php
bindtextdomain('messages', './locale');
textdomain('messages');
setlocale(LC_MESSAGES, 'fr');
echo gettext("NO GOOD");

Here's my output:

$ NO GOOD

PHPInfo:

GetText Support => enabled
load more v
65%

How can i debug the issue because i think the problem from server not from a code ??,Hello , How can i fix gettext issue with this website dellooni.com/ar-jo/ that has wordked correctly on old server ,You debug the issue by talking to your web developer. The server (besides PHP version) is irrelevant., These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello , How can i fix gettext issue with this website dellooni.com/ar-jo/ that has wordked correctly on old server

 if (Request::isHttpGet('lang')) {
    if (Request::getHttpGet('lang') == 'ar-jo')
       $language = 'ar_JO';
 } else {
    $language = 'en_US';
 }

 putenv("LANG=$language");
 setlocale(LC_ALL, $language);
 DEFINE('BASE_PATH', __DIR__.DIRECTORY_SEPARATOR);
 $path = BASE_PATH.
 'locale';
 $domain = 'words'; //this is the default text-domain
 bindtextdomain($domain, $path);
 //  bind_textdomain_codeset($domain, 'UTF-8');
 textdomain($domain);
 bind_textdomain_codeset($domain, 'UTF-8');
75%

bind_textdomain_codeset — Specify or get the character encoding in which the messages from the DOMAIN message catalog will be returned,dcgettext — Overrides the domain for a single lookup,gettext — Lookup a message in the current domain,bindtextdomain — Sets or gets the path for a domain

load more v
40%

How to debug gettext not working in PHP?

1.2. < ? php3.$locale = 'es';
4.
if (isset($_GET["locale"])) $locale = $_GET["locale"];
5.6.$domain = 'messages';
7.8.$results = putenv("LC_ALL=$locale");
9.
if (!$results) {
   10. exit('putenv failed');
   11.
}
12.13. // http://msdn.microsoft.com/en-us/library/39cwe7zf%28v=vs.100%29.aspx14.$results = setlocale(LC_ALL, $locale, 'spanish');15.if (!$results) {16.    exit ('setlocale failed: locale function is not available on this platform, or the given local does not exist in this environment');17.}18.19.$results = bindtextdomain($domain, "./locales");20.echo 'new text domain is set: ' . $results. "\n";21.22.$results = textdomain($domain);23.echo 'current message domain is set: ' . $results. "\n";24.25.$results = gettext("Hello world");26.if ($results === "Hello world") {27.    echo "Original English was returned. Something wrong\n";28.}29.echo $results . "\n";30.
22%

This is Codeigniter PHP framework library for dealing with gettext translation and localization.,If you are loading this library inside your controller you can override any of the default configuration directives. For example if you want to override default $config['gettext_locale'] with hr_HR.UTF-8 you could use something like this:,Add 'gettext' to Auto-load Libraries array or use $this->load->library('gettext') inside your controller.,You can also fork or contribute to Codeigniter Gettext by visiting its official GitHub repository page:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
load more v
60%

Read the names of the input files from file instead of getting them from the command line. , For PHP: _, gettext, dgettext:2, dcgettext:2, ngettext:1,2, dngettext:2,3, dcngettext:2,3. , For Python: gettext, ugettext, dgettext:2, ngettext:1,2, ungettext:1,2, dngettext:2,3, _. , For Lisp: gettext, ngettext:1,2, gettext-noop.

xgettext[option][inputfile]
load more v

Other "working-undefined" queries related to "How to debug gettext not working in PHP?"