Gettext 将始终使用系统默认语言环境 [英] Gettext will always use system default locale

查看:22
本文介绍了Gettext 将始终使用系统默认语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要本地化一个仅限 Windows 的 PHP 网络应用程序,我正在评估 gettext 扩展,但我'我最难的是让它在我的 Windows 7 开发箱中工作.我已经将试错法与 Process Monitor 一起使用来克服困难和不准确的文档,我设法使 _() 显示来自 *.po 目录的字符串,该目录对应于计算机的默认语言环境(在我的情况下为现代西班牙语).我设置不同语言环境的所有尝试都被默默地忽略了.

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>

就我而言,<?=_('codigo_idioma')?> 总是打印 es_ES@modern.

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

我有 PHP/5.4.5,但我希望它能在我们客户拥有的任何合理的最新服务器上运行.

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

我已经阅读了很多关于即使在 Windows 上也需要安装语言环境的模糊参考,但没有确切的细节.可能是什么问题?

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?

(我知道常见的建议是转储 gettext 并使用任何其他库.)

进一步测试:

我的代码在另外两台计算机上完美运行:32 位 Windows Vista 和 32 位 Windows 7 32 位.它在我的计算机(64 位 Windows 7)和另一台计算机(32 位 Windows Server 2003)中失败

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 版本似乎无关紧要(命令行解释器也会发生这种情况).
  • PHP 版本似乎无关紧要(还在我的 PC 上尝试了最新的 32 位 PHP/5.5.5).
  • 我的[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls] 注册表树与其他七个框相同.
  • 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_MACHINE\SYSTEM\CurrentControlSet\Control\Nls] registry tree is identical to the other Seven box.

在命令行上进行测试时,我发现在运行 PHP 脚本之前设置 LANG 环境变量最终会更改语言:

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:\test\gettext.php

这明确地证明了我的计算机拥有正确的资产,但也让我想知道为什么 PHP 声称 putenv() 可以工作然后忽略它:

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';

推荐答案

这是一个问题 已确认并部分修复 由 PHP 团队提供.

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

这是一个相当技术性的事情,显然与底层平台处理环境变量(gettext 严重依赖)的方式有关.此外,从 VC9 到 VC11 的 Visual C 运行时库发生了一些变化,影响了所有这些.

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.

总结:

  • 已于 2014 年 11 月 21 日st 在源树中修复了非线程安全构建.
  • 线程安全构建(例如 Apache 模块)还没有,也没有明确的解决方案.
  • 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.

这篇关于Gettext 将始终使用系统默认语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆