如何分析 gettext 不起作用的原因?

发布于 2024-12-08 11:40:56 字数 1503 浏览 0 评论 0原文

我目前正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分开我的手 2024-12-15 11:40:56

这就是我在 Linux 上的 CE ZendServer 和 NetBsd 上的 Apache 服务器上的工作

文件“message.po”是从应用程序的根生成的:

#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-04-30 19:38+0200\n"
"PO-Revision-Date: 2013-04-12 14:00+0000\n"
"Last-Translator: gin(e) <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: eo\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: include/apteryx_text.php:3
msgid "email:"
msgstr "Retpoŝtadreso:"

这是新的树目录:

res/locale/
    de_DE/
       LC_MESSAGES/
            messages.mo
    eo_EO/
       LC_MESSAGES/
            messages.mo
    eo->eo_EO (symlink)

我必须添加“eo”符号链接,因为只有三个我的系统上支持的世界语语言环境是:

eo
eo.iso88593
eo.utf8

编译的whit:

sudo locale-gen eo
sudo locale-gen eo.iso88593
sudo locale-gen eo.utf8
sudo update-locale
sudo dpkg-reconfigure locales

目前我的locale.php代码是:

$charset="UTF-8";
$gettext_domain="messages";
$locale_dir="res/locale";
putenv("LC_ALL=$lang");
$l=split("_",$lang);
/* not in all system and not all locales got the classic name this stupid method try to solve it*/
if(! setlocale(LC_ALL, $lang.".".$charset)) 
  if(! setlocale(LC_ALL, $lang)) 
    if(! setlocale(LC_ALL,$l[0].".".$charset))  
        setlocale(LC_ALL,$l[0]);

bindtextdomain($gettext_domain, "res/locale");
textdomain($gettext_domain);
bind_textdomain_codeset($gettext_domain, $charset);

因为whitout它gettext不起作用。我认为区域设置目录必须与 setlocale 设置的语言具有相同的名称。另一件事是测试每个函数调用的返回值。它们绝不能返回 NULL 或 FALSE。您可以通过简单的方式做到这一点:

var_dump(bindtextdomain($gettext_domain, "res/locale"));
var_dump(textdomain($gettext_domain)); 
..and so on..

最后但不是列表,请记住为所有 .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:

#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-04-30 19:38+0200\n"
"PO-Revision-Date: 2013-04-12 14:00+0000\n"
"Last-Translator: gin(e) <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: eo\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: include/apteryx_text.php:3
msgid "email:"
msgstr "Retpoŝtadreso:"

This is the new tree-directory:

res/locale/
    de_DE/
       LC_MESSAGES/
            messages.mo
    eo_EO/
       LC_MESSAGES/
            messages.mo
    eo->eo_EO (symlink)

I must add the "eo" symlink because the only three esperanto locales on my system supported are:

eo
eo.iso88593
eo.utf8

compiled whit:

sudo locale-gen eo
sudo locale-gen eo.iso88593
sudo locale-gen eo.utf8
sudo update-locale
sudo dpkg-reconfigure locales

and at the moment my locale.php code is:

$charset="UTF-8";
$gettext_domain="messages";
$locale_dir="res/locale";
putenv("LC_ALL=$lang");
$l=split("_",$lang);
/* not in all system and not all locales got the classic name this stupid method try to solve it*/
if(! setlocale(LC_ALL, $lang.".".$charset)) 
  if(! setlocale(LC_ALL, $lang)) 
    if(! setlocale(LC_ALL,$l[0].".".$charset))  
        setlocale(LC_ALL,$l[0]);

bindtextdomain($gettext_domain, "res/locale");
textdomain($gettext_domain);
bind_textdomain_codeset($gettext_domain, $charset);

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:

var_dump(bindtextdomain($gettext_domain, "res/locale"));
var_dump(textdomain($gettext_domain)); 
..and so on..

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".

深者入戏 2024-12-15 11:40:56

当我通过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 :)

厌倦 2024-12-15 11:40:56

如果你错过了,OP 在上面的评论中说他通过简单地重新启动 Apache 来解决这个问题。我遇到了 _() 无法正常工作的问题。语言环境设置良好,bindtextdomaintextdomain 返回正确的值,但它不起作用。 我重新启动了 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 and textdomain were returning the correct values, but it just wasn't working. I restarted Apache and it worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文