gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:20:03 27 4
gpt4 key购买 nike

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

<?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 文件中是这样的:

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

msgid "Hello world"
msgstr "Hola World"

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

最佳答案

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

<?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. "\n";

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

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

关于php - 如何调试 gettext 在 PHP 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23170819/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com