Skip to content
Advertisement

Gettext will always use system default locale

I need to localise a Windows-only PHP web application and I’m evaluating the gettext extension but I’m having the hardest time trying to make it work in my Windows 7 development box. I’ve used trial and error together with Process Monitor to overcome the poor and inaccurate documentation and I’ve managed to make _() display strings from the *.po catalogue that corresponds to the computer’s default locale (Modern Spanish in my case). All my attempts to set a different locale are silently ignored.

I’ve written a test script with lots of redundant stuff:

<dl><?php

define('DIR_LOCALE', __DIR__ . DIRECTORY_SEPARATOR . 'locale');
bindtextdomain('general', DIR_LOCALE);
bind_textdomain_codeset('general', 'UTF-8');
textdomain('general');

if(!defined('LC_MESSAGES')){
    define('LC_MESSAGES', 5);
}

$pruebas = array(
    'enu',
    'es_ES',
    'en_GB',
    'english-uk',
    'Spanish_Spain.1252',
    'esn',
    'spanish',
    'spanish-modern',
);
foreach($pruebas as $locale){
    putenv("LC_ALL=$locale");
    setlocale(LC_ALL, $locale);

    putenv("LC_MESSAGES=$locale");
    setlocale(LC_MESSAGES, $locale);

    putenv("LANGUAGE=$locale");
    putenv("LANG=$locale");
?>
    <dt><?=htmlspecialchars($locale)?></dt>
    <dd><?=_('codigo_idioma')?></dd>
<?php } ?>
</dl>

In my case, <?=_('codigo_idioma')?> always prints es_ES@modern.

I have PHP/5.4.5 but I’m expecting to get it working in any reasonably up-to-date server our customers own.

I’ve read lots of vague references about the need to install locales even on Windows but no exact details. What can the problem be?

(I’m aware that the common advice is to dump gettext and use any other library.)


Further testing:

My code run flawlessly as-is in another two computers: 32-bit Windows Vista and 32-bit Windows 7 32-bit. It fails in my computer (64-bit Windows 7) and another one (32-bit Windows Server 2003)

  • Apache version seems irrelevant (it also happens with the command-line interpreter).
  • PHP version seems irrelevant (also tried latest 32-bit PHP/5.5.5 in my PC).
  • My [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlNls] registry tree is identical to the other Seven box.

Edit: While testing on the command-line, I’ve discovered that setting the LANG environment variable before running the PHP script finally changes the language:

C:>set LANG=en_GB
C:>php C:testgettext.php

This definitively proves that my computer has the correct assets but also makes me wonder why PHP claims that putenv() works and then ignores it:

var_dump( getenv('LANG'), putenv('LANG=en_GB'), getenv('LANG') );
bool(false)
bool(true)
string(5) "en_GB"

Even this doesn’t have any effect:

$_ENV['LANG'] = 'en_GB';
$_SERVER['LANG'] = 'en_GB';

Advertisement

Answer

It’s an issue that’s been acknowledged and partially fixed by the PHP team.

It’s a fairly technical thing, apparently related to the way environment variables (on which gettext heavily depends) are handled by the underlying platform. Also, something has changed in the Visual C Runtime libraries from VC9 to VC11 that affects all this.

To sum up:

  • Non thread-safe builds have been fixed in the source tree on 21st Nov. 2014.
  • Thread-safe builds (e.g. Apache module) haven’t and there isn’t a clear solution in sight.
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement