提问人:Jeppe Donslund 提问时间:10/17/2023 最后编辑:Casimir et HippolyteJeppe Donslund 更新时间:10/17/2023 访问量:54
PHP gettext 不返回翻译后的字符串
PHP gettext doesn't return translated string
问:
我正在尝试使用 PHP 设置 getText()
$locale = "en_GB";
//if (isset($_GET["locale"])) $locale = $_GET["locale"];
$domain = 'messages';
bindtextdomain($domain, "./locale");
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
var_dump( file_exists("locale/".$locale."/LC_MESSAGES/".$domain.".mo" ) );
$results = gettext("Velkommen");
if ($results === "Velkommen") {
echo "Original English was returned. Something wrong\n";
}
echo $results . "\n";
?>
<?= _("Velkommen"); ?>`
var_dump返回 true,但我收到错误消息,说出了点问题。
检查语言环境 -a 我知道服务器上存在en_GB。
我尝试了在 Google 上找到的多个建议。 尝试更改 bindtextdomain 中的路径
答:
-1赞
Jeppe Donslund
10/17/2023
#1
这个例子对我有用。
print date("d-m-Y H:i:s", date("U"));
$locale = "en_GB";
if (!function_exists("gettext")){ echo "gettext is not installed\n"; } else{ echo "gettext is supported\n"; }
//if (isset($_GET["locale"])) $locale = $_GET["locale"];
putenv('LC_ALL='.$locale);
if (false === setlocale(LC_ALL, $locale)) {
throw new LocaleNotSupportedException(sprintf('Locale "%s" is not installed in the system.', $locale));
}
$domain = 'messages';
bindtextdomain($domain, "locale");
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
var_dump( file_exists("locale/".$locale."/LC_MESSAGES/".$domain.".mo" ) );
$results = gettext("Velkommen");
if ($results === "Velkommen") {
echo "Original Danish was returned. Something wrong\n";
}
echo $results . "\n";
?>
<?= _("Velkommen"); ?>
评论
0赞
DarkBee
10/17/2023
请解释此代码如何解决 OP 遇到的问题
0赞
ADyson
10/17/2023
@DarkBee OP发布了答案......!但是,是的,我同意一个好的答案(根据如何回答)将包括一些关于问题是什么的上下文,以及答案中的代码如何解决它(以及需要更改的内容的详细信息),以便将来对其他读者更有用。
评论
echo $results
if ($results === "Velkommen")