Gettext忽略setlocale

时间:2017-01-13 17:50:59

标签: php gettext ubuntu-16.04

我正在为项目的可能翻译解决方案测试gettext。

我创建了一个具有以下结构的迷你项目:

├── index.php 
└── locale
    ├── en_
    │   └── LC_MESSAGES
    │       ├── messages.mo
    │       └── messages.po
    └── en_US.utf8
        └── LC_MESSAGES
            ├── messages.mo
            └── messages.po

这是php代码:

$language = "en_US.utf8";
putenv("LC_MESSAGES=$language");
setlocale(LC_MESSAGES, $language);

bindtextdomain("messages", "locale");    
textdomain("messages");

echo gettext("HELLO_WORLD") , "\n";

这是en_US.utf8 / LC_MESSAGES文件夹中的message.po

#Test token 1
msgid "HELLO_WORLD"
msgstr "Hello World!11111111"

#Test token 2
msgid "TEST_TRANSLATION"
msgstr "Testing translation..1111111"

当我这样做时:

  

php index.php

我明白了:

  

程序hello_world

问题似乎是setLocale没有得到尊重。当我做一个strace时,我可以看到这个。

  

strace php index.php

输出:

getcwd("/home/antonio/www/locale_test", 4096) = 47
lstat("/home/antonio/www/locale_test/locale", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 4
fstat(4, {st_mode=S_IFREG|0644, st_size=2995, ...}) = 0
read(4, "# Locale name alias data base.\n#"..., 4096) = 2995
read(4, "", 4096)                       = 0
close(4)                                = 0
open("/home/antonio/www/locale_test/locale/en_GB/LC_MESSAGES/messages.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/home/antonio/www/locale_test/locale/en/LC_MESSAGES/messages.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale-langpack/en_GB/LC_MESSAGES/messages.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale-langpack/en/LC_MESSAGES/messages.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(1, "HELLO_WORLD", 11HELLO_WORLD)             = 11

您可以看到它将转到本地语言环境文件夹,但不会转到正确的en_US.utf8文件夹。它尝试en_GB和en

我确实有en_US.utf8语言环境。 (我可以从locale -a中看到列表) 我使用PHP 7.0.13-0ubuntu0.16.04.1(cli)(NTS)在ubuntu 16上运行它

我错过了什么?

0 个答案:

没有答案