这几天我一直在思考这个问题,但没有任何成功。我只想让 gettext 工作。
发生的事情是打印 php 文件中写入的字符串,而不是翻译后的字符串。 IE。如果我执行“echo gettext("Service");"然后打印“Service”,而不是瑞典语“Tjänster”。
httpd/error_log 中绝对没有错误消息。不是在运行应用程序或(重新)启动 apache 时。
使用 Centos 6.2 和 php 5.3。
这是我的目录结构:
/path/to/locale/sv_SE.utf8/LC_MESSAGES/messages.mo
/path/to/setlocale.php
我的messages.po文件:
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-01-18 20:09+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: setlocale.php:29
msgid "Service"
msgstr "Tjänst"
#: setlocale.php:30
msgid "Name"
msgstr "Tjänst"
setlocale.php:
<?php
if (!function_exists("gettext"))
{
echo "gettext is not installed";
}
else
{
echo "gettext is supported";
}
echo "Exist?: ".setlocale(LC_MESSAGES, 'sv_SE.utf8')."; //Prints 'sv_SE.utf8, hence it exist in the system.
bindtextdomain('messages', '/var/www/html/smic/modules/core/localization/locale');
textdomain('messages');
bind_textdomain_codeset('messages', 'UTF-8');
echo gettext("Service");
echo _("Name");
?>
由于打印了“gettext is supported”,gettext 至少在某种程度上起作用了。
'sv_SE.utf8'也被打印出来,因此它存在于系统中
为了安装正确的包,我一直在尽我所能(好吧,至少根据我的理解,当然这里可能会有一些问题)。 PHP 5.3 将 intl 作为核心的一部分。我还没有设法使用 ./configure --enable-intl 进行编译。我在尝试使用它时确实遇到了错误,我想这是因为它是 php 5.3 的一部分。我对吗?
我也找不到要添加到 php.ini 文件的任何 intl.so(updatedb 完成并搜索它),没有添加 extension=intl.so。我也没有成功,但如果我找不到 intl.so 我想添加 extension=intl.so 是没有意义的?
一些线程提到了一些要安装的包,我也试过了:
yum install libicu-devel
yum install libicu
如果有人能提供帮助,那就太好了,我不知道如何前进,大多数帖子/主题都在讨论如何在使用不包含 intl 的 PHP 5.2 时进行安装。但我什至不知道问题是否与系统设置有关或其他任何问题。
最佳答案
不确定这是否对您有帮助,但这段代码对我有用。
$lang_path = FCPATH.APPPATH.'language/locale';
putenv('LANG='.$code.'.UTF-8');
setlocale(LC_ALL, $code.'.UTF-8');
bindtextdomain('lang', $lang_path);
textdomain('lang');
关于php - Gettext 不工作,没有错误消息,使用 php 5.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8917968/