php - 我如何分析为什么gettext不起作用?

标签 php internationalization gettext poedit

我目前正在尝试将 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 :-(');

?>

这是我的消息.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 <info@martin-thoma.de>\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我得到“测试:-(”

我已经用

生成了语言环境
sudo locale-gen de_DE

并检查了

locale -a

为什么不起作用?我怎样才能从 gettext 获得一些反馈?

最佳答案

这就是我在 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) <ginex@posta.indivia.net>\n"
"Language-Team: LANGUAGE <LL@li.org>\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”符号链接(symbolic link),因为我的系统仅支持三种世界语语言环境:

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 不起作用。我认为语言环境目录必须具有与 setlocale 设置的语言相同的名称。另一件事是测试每个调用什么返回的函数。它们绝不能返回 NULL 或 FALSE。您可以通过简单的方式做到这一点:

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

最后但不是列表,请记住为所有 .mo 文件设置正确的 apache 权限,重新启动 apache 服务器并通过 phpinfo() 验证“GetText 支持”已“启用”。

关于php - 我如何分析为什么gettext不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7690266/

相关文章:

php - 语法错误,意外的 $end

internationalization - Plone 主题中的事件语言

gettext - 在 Weblate 中本地化 Markdown 文件的最佳方法是什么?

c - xgettext 无法识别定义为宏的字符串

javascript - 使用 javascript 更新文本框中的值

php - 最后一行在 MYSQL 中返回不正确的值

php - 我如何使用 get 方法输入值以使用 PHP 形成字段

Spring MVC i18n Hibernate 验证

java - Spring Boot中如何将特定的短信国际化?

java - 文本从 JSpinner 到 JTextArea