PHP gettext 不返回翻译后的字符串

问题描述 投票:0回答:1

我正在尝试使用 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,但我收到错误消息,指出出现问题。

检查 locale -a 我知道服务器上存在 en_GB。

有什么想法吗?

我尝试了在谷歌上找到的多种建议。 尝试更改bindtextdomain中的路径

php gettext
1个回答
0
投票

这个例子对我有用。

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"); ?>
© www.soinside.com 2019 - 2024. All rights reserved.