Gettext没有通过php-cli工作,但在php-apache中工作

Cod*_*wer 8 php apache locale gettext internationalization

我运行的代码如下所示:

...
$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');
Run Code Online (Sandbox Code Playgroud)

通过apache2运行它的结果是:

da_DK Beskrivelse
Run Code Online (Sandbox Code Playgroud)

这是我们想要的.

运行它throgh cli:

da_DK Description
Run Code Online (Sandbox Code Playgroud)

如果我们使用英语,那将是正确的.'d'在英语翻译中是大写,在源中是小写(从上下文看起来有点奇怪)

因此gettext可以工作,因为它可以翻译字符串,但不知何故,它忽略了我在脚本中更改了语言环境并从英语目录中选择了.po文件.

如果您想知道我为什么需要cli脚本上的语言环境:该脚本用于通过电子邮件发送发票.

我使用:Ubuntu 12.10,PHP 5.4.6-1ubuntu1,apache 2.2.22和gettext 0.18.1.

edl*_*erd 21

您必须重置环境变量LANGUAGE:

运行putenv( "LANGUAGE =");

花了一段时间才发现.

  • 我希望我能不止一次地赞成这一点 (6认同)