Search code examples
phpapacheinternationalizationlocalegettext

Gettext not working through php-cli, but works in php-apache


The code i run looks as the following:

...
$this->locale = da_DK;
...
putenv("LC_ALL=".$this->locale);
putenv('LANG='.$this->locale);
$res = setlocale(LC_ALL, $this->locale);
if($res != $this->locale){
    throw new Exception("The language could not be set.");
}
bindtextdomain("domain", "./locale");
textdomain("domain");

 echo setlocale(LC_MESSAGES, 0 );
 echo ' ';
 echo __('description');

Result running it through apache2 is:

da_DK Beskrivelse

Which is as we want.

Running it throgh cli:

da_DK Description

Which would have been correct if we had used english. The 'd' is upper case in the english translation and the lower case in the source (From out of context it seems a bit weird)

So gettext works as it can translate the string, but somehow it disregards that i changed locale in the script and chose the .po file from the english directory.

In case you wonder why i need locales on a cli script: The script is used to emails out invoices.

I use: Ubuntu 12.10, PHP 5.4.6-1ubuntu1, apache 2.2.22, and gettext 0.18.1.


Solution

  • You have to reset the environment Variable LANGUAGE:

    putenv("LANGUAGE=");

    Took me a while to find out.