如何分析 gettext 不起作用的原因?
我目前正在尝试将 gettext 与 PHP 和 poedit 一起使用。我编写了以下 test.php 文件:
<?php
error_reporting(E_ALL | E_DEPRECATED | E_USER_DEPRECATED | -1);
bindtextdomain('messages', './i18n/');
textdomain('messages');
setlocale(LC_ALL, $_GET['l']);
putenv("LANG=".$_GET['l']);
echo _('test :-(');
?>
这是我的 messages.po:
msgid ""
msgstr ""
"Project-Id-Version: Community Chess\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-10-07 18:34+0100\n"
"PO-Revision-Date: \n"
"Last-Translator: Martin Thom <[email protected]>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-SourceCharset: utf-8\n"
"X-Poedit-Basepath: /var/www/community-chess\n"
"X-Poedit-SearchPath-0: .\n"
#: test.php:8
msgid "test :-("
msgstr "Juhu :-)"
#~ msgid "test"
#~ msgstr "Juhu!"
我的目录结构是
community-chess
test.php
i18n
de_DE
LC_MESSAGES
messages.po
messages.mo
一旦我查看 http://localhost/community-chess/test.php?l=de_DE 我得到“test :-(”
我已经生成了语言环境
sudo locale-gen de_DE
并检查了
locale -a
为什么它不起作用我怎样才能得到一些反馈?获取文本?
I am currently trying to use gettext with PHP and poedit. I wrote the following test.php file:
<?php
error_reporting(E_ALL | E_DEPRECATED | E_USER_DEPRECATED | -1);
bindtextdomain('messages', './i18n/');
textdomain('messages');
setlocale(LC_ALL, $_GET['l']);
putenv("LANG=".$_GET['l']);
echo _('test :-(');
?>
and this is my messages.po:
msgid ""
msgstr ""
"Project-Id-Version: Community Chess\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-10-07 18:34+0100\n"
"PO-Revision-Date: \n"
"Last-Translator: Martin Thom <[email protected]>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-SourceCharset: utf-8\n"
"X-Poedit-Basepath: /var/www/community-chess\n"
"X-Poedit-SearchPath-0: .\n"
#: test.php:8
msgid "test :-("
msgstr "Juhu :-)"
#~ msgid "test"
#~ msgstr "Juhu!"
My directory structure is
community-chess
test.php
i18n
de_DE
LC_MESSAGES
messages.po
messages.mo
As soon as I look at http://localhost/community-chess/test.php?l=de_DE I get "test :-("
I have generated the locale with
sudo locale-gen de_DE
and checked with
locale -a
Why doesn't it work? How can I get some feedback from gettext?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我在 Linux 上的 CE ZendServer 和 NetBsd 上的 Apache 服务器上的工作
文件“message.po”是从应用程序的根生成的:
这是新的树目录:
我必须添加“eo”符号链接,因为只有三个我的系统上支持的世界语语言环境是:
编译的whit:
目前我的locale.php代码是:
因为whitout它gettext不起作用。我认为区域设置目录必须与 setlocale 设置的语言具有相同的名称。另一件事是测试每个函数调用的返回值。它们绝不能返回 NULL 或 FALSE。您可以通过简单的方式做到这一点:
最后但不是列表,请记住为所有 .mo 文件设置正确的 apache 权限,重新启动 apache 服务器并通过 phpinfo() 验证“GetText Support”是否已“启用”。
This is what work for me on CE ZendServer on linux and Apache server on NetBsd
File "message.po" is generated from the root of application:
This is the new tree-directory:
I must add the "eo" symlink because the only three esperanto locales on my system supported are:
compiled whit:
and at the moment my locale.php code is:
because whitout it gettext doesn't work. I think that the locale directory must have the same name of the language setted whit setlocale. Another things is to test every function called what return. Them must never return NULL or FALSE. You can do that in simple way:
The last but not the list, remember to set the right apache permission to all .mo file, to restart your apache server and verify by phpinfo() that "GetText Support" is "enabled".
当我通过cmd“/path/to/nginx -s reload”重新启动nginx时,它不起作用,但是当我使用cmd“/etc/init.d/php-fpm restart”重新启动php-fpm后,它成功了!
希望我的经验对遇到这个问题的人有帮助:)
When I restart nginx through the cmd "/path/to/nginx -s reload",it dosen't work,but after I restart php-fpm with the cmd "/etc/init.d/php-fpm restart",it worked!
Hope my experience will be helpful to someone with the issue :)
如果你错过了,OP 在上面的评论中说他通过简单地重新启动 Apache 来解决这个问题。我遇到了
_()
无法正常工作的问题。语言环境设置良好,bindtextdomain
和textdomain
返回正确的值,但它不起作用。 我重新启动了 Apache,它成功了。In case you missed it, the OP said in his comments above that he fixed this by simply restarting Apache. I was having troubles with
_()
just not working. The locale was setting fine,bindtextdomain
andtextdomain
were returning the correct values, but it just wasn't working. I restarted Apache and it worked.