1

我在 Windows 上使用 WAMP 服务器并制作了一个测试项目。我在网站文件夹中有这些文件:

C:\wamp\www\project\locale\ar_EG\LC_MESSAGES\messages.po C:\wamp\www\project\locale\en_US\LC_MESSAGES\messages.po

下面是PHP代码:

$language="en_US";
$encoding = "UTF-8";
putenv("LANG=".$language);
setlocale(LC_ALL,$language);
$domain="messages"; // name of PO file
bindtextdomain($domain,"Locale");
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
echo gettext("name");

上面的代码工作正常。当我尝试en_USar_EG它替换时,也会显示翻译en_US,当我尝试删除en_US文件夹并重试时,它会显示msgidnot msgstr

我搜索了一下,发现是setlocale在windows平台上有问题,但是我需要一个解决方案让它在windows上运行。

4

2 回答 2

1

Windows 上的 setlocale() 存在问题。

来自PHP 手册中的 setLocale()

Windows 用户可以在 Microsoft 的 MSDN 网站上找到有关区域设置字符串的有用信息。支持的语言字符串可在 »语言字符串文档中找到,支持的国家/地区字符串可在 »国家/地区字符串文档中找到。

您可以尝试使用$language = 'english-us';而不是$language = 'en_US';.

我不确定ar_eg, 也许Arabic_Egypt或的语言代码Arabic_Egypt.1256

如果这不起作用,您仍然可以选择使用实现 gettext 的 PHP 库,例如https://github.com/oscarotero/Gettexthttps://launchpad.net/php-gettext

于 2014-12-30T12:15:09.003 回答
0

我使用以下代码使其在 Windows 上工作:

$locale = 'en_US';

if(!defined('LC_MESSAGES') || !setlocale(LC_ALL, $locale)) {
    putenv("LC_ALL=$locale");
}

$domain = 'messages';
$path = "C:\wamp\www\project\locale";
$codeset = 'UTF-8';

bindtextdomain($domain, $path);
textdomain($domain);
bind_textdomain_codeset($domain, $codeset);

echo gettext("name");
于 2014-12-30T12:14:53.860 回答