简体   繁体   中英

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.

You have to reset the environment Variable LANGUAGE:

putenv("LANGUAGE=");

Took me a while to find out.

You need to check if the bindtextdomain() points the same resource file from php-cli and php-apache. I suggest,

  1. echo the return value of the bindtextdomain() and see them.
  2. try to replace "./locale" to absolute path (eg "/var/www/yourproject/locale") and see if it works.

I don't know how much this is relevant or not but …

I just found a solution to the opposite problem: Gettext not working through php-apache , but working in php-cli .

Also concerned, strftime localization not working through php-apache but working in php-cli (on a Linux Debian buster system).

LANGUAGE environment variable did not help as I do not have such environment variable.

Strangely, I just disabled the perl module in Apache and the problem disappeared.

So maybe some bugs in any Apache module could trigger this kind of problem…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM