如何分析为什么 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 :-(');
?>

and this is my messages.po:

msgid ""
msgstr ""
"Project-Id-Version: Community Chessn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2011-10-07 18:34+0100n"
"PO-Revision-Date: n"
"Last-Translator: Martin Thom <info@martin-thoma.de>n"
"Language-Team: n"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=UTF-8n"
"Content-Transfer-Encoding: 8bitn"
"X-Poedit-SourceCharset: utf-8n"
"X-Poedit-Basepath: /var/www/community-chessn"
"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

为什么不工作?我怎样才能从gettext得到一些反馈?

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

文件"消息。Po "是从应用程序的根目录生成的:

#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSIONn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2013-04-30 19:38+0200n"
"PO-Revision-Date: 2013-04-12 14:00+0000n"
"Last-Translator: gin(e) <ginex@posta.indivia.net>n"
"Language-Team: LANGUAGE <LL@li.org>n"
"Language: eon"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=utf-8n"
"Content-Transfer-Encoding: 8bitn"
#: 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
编制些微

:

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

因为没有它,gettext无法工作。我认为locale目录必须与setlocale中设置的语言名称相同。另一件事是测试每个叫做什么返回的函数。它们绝不能返回NULL或FALSE。您可以通过简单的方式做到这一点:

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

最后但不是列表,记住设置正确的apache权限为所有。mo文件,重新启动apache服务器并通过phpinfo()验证"GetText Support"是"enabled"

当我通过命令"/path/to/nginx -s reload"重新启动nginx时,它不工作,但当我用命令"/etc/init. exe "重新启动php-fpm后。D/php-fpm restart",它工作了!希望我的经验能对解决这个问题的人有所帮助:)

如果您错过了它,OP在上面的评论中说,他通过简单地重新启动Apache来修复这个问题。我在_()上遇到了麻烦,就是不能工作。地区设置良好,bindtextdomaintextdomain返回正确的值,但它只是不工作。

最新更新