如何调试 gettext 在 PHP 中不起作用?

我正在尝试在 php 5.5 中使用 php gettext 扩展(在 win2008 服务器上,使用 IIS7).我正在这样做:

i am trying to use the php gettext extension in php 5.5 (on win2008 server, using IIS7). I am doing this:

<?php

$locale = "es";
if (isSet($_GET["locale"])) $locale = $_GET["locale"];
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
textdomain("messages");

echo gettext("Hello world");

?>

有了这个文件夹结构:

locale/es/LC_MESSAGES/messages.mo

但它总是只返回 Hello world 而不是正确的翻译,目前(基于我缺乏西班牙语技能)在 messages.po 文件中是这样的:

But it always just returns Hello world and not the correct translation which for now (based on my lack of spanish skills) is this in the messages.po file:

msgid ""
msgstr ""
"Project-Id-Version: TestXlations
"
"POT-Creation-Date: 2014-04-19 08:15-0500
"
"PO-Revision-Date: 2014-04-19 09:18-0500
"
"Language-Team: 
"
"Language: es
"
"MIME-Version: 1.0
"
"Content-Type: text/plain; charset=UTF-8
"
"Content-Transfer-Encoding: 8bit
"
"X-Generator: Poedit 1.6.3
"
"X-Poedit-Basepath: .
"
"Plural-Forms: nplurals=2; plural=(n != 1);
"
"X-Poedit-SearchPath-0: c:/dev
"

msgid "Hello world"
msgstr "Hola World"

这从 cmd 行和通过 IIS 失败.所以我看到 gettext 调用等并执行它,但它没有读取翻译文件.我该如何进一步调试?即使删除翻译文件,我也会得到相同的行为.

This fails from the cmd line and via IIS. So i it's seeing the gettext call, etc and executing it but it's not reading the translation file. how can i debug this further? even if remove the translation file, i get the same behavior.

推荐答案

你应该检查返回值并知道哪个函数失败了.它不是 i18n 特定的,但对任何 PHP 脚本或任何编程语言调试都很有用.

You should check returned values and know which function failed. It is not i18n specific but useful for any PHP scripts, or any programming language debugging.

<?php
$locale = 'es';
if (isset($_GET["locale"])) $locale = $_GET["locale"];

$domain = 'messages';

$results = putenv("LC_ALL=$locale");
if (!$results) {
    exit ('putenv failed');
}

// http://msdn.microsoft.com/en-us/library/39cwe7zf%28v=vs.100%29.aspx
$results = setlocale(LC_ALL, $locale, 'spanish');
if (!$results) {
    exit ('setlocale failed: locale function is not available on this platform, or the given local does not exist in this environment');
}

$results = bindtextdomain($domain, "./locales");
echo 'new text domain is set: ' . $results. "
";

$results = textdomain($domain);
echo 'current message domain is set: ' . $results. "
";

$results = gettext("Hello world");
if ($results === "Hello world") {
    echo "Original English was returned. Something wrong
";
}
echo $results . "
";

相关文章