[英]PHP bindtextdomain fails
我正在嘗試在運行 PHP 7.1 的 CentOS 服務器上設置 PHP 國際化
這是我的目錄結構:
/home/project/public_html/locale/japanese/LC_MESSAGES/messages.po
/home/project/public_html/locale/japanese/LC_MESSAGES/messages.mo
/home/project/public_html/index.php
messages.po 包含該行(除其他外):
"Language: japanese\n"
"Content-Type: text/plain; charset=UTF-8\n"
我有以下代碼:
$check_putenv = putenv("LC_ALL=japanese");
if (!$check_putenv) {
echo "Warning: putenv LC_ALL failed!\n";
}
$check_putenv2 = putenv("LANGUAGE=japanese");
if (!$check_putenv2) {
echo "Warning: putenv LANGUAGE failed!\n";
}
$check_locale = setlocale(LC_MESSAGES, 'japanese');
if (!$check_locale) {
echo "Warning: Failed to set locale japanese!\n";
}
$check_bind = bindtextdomain("messages", "/home/project/public_html/locale");
if (!$check_bind) {
echo "Warning: Failed to bind text domain!\n";
}
$check_textdomain = textdomain("messages");
if ($check_textdomain !== "messages") {
echo "Warning: Failed to set text domain!\n";
}
輸出是
Warning: Failed to bind text domain!
locale -a 返回(除其他外)
ja_JP
ja_JP.utf8
japanese
知道有什么問題嗎?
正如評論中所討論的, gettext擴展依賴於包含語言和區域代碼的標准語言環境說明符,即ja_JP
表示“日本的日語”或具有指定編碼ja_JP.utf-8
。 即使有像japanese
這樣的別名,gettext 的 PHP 實現也不接受這個。 請注意,必須在您的系統上安裝和配置語言環境。
語言和區域說明符可以在IANA 語言子標簽注冊表中找到
此代碼已經適用於日語:
$dir = $_SERVER['DOCUMENT_ROOT'] . '/locale';
$domain = 'messages';
$locale = 'ja_JP.utf8';
$codeset = 'UTF-8';
setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $dir);
textdomain($domain);
bind_textdomain_codeset($domain, $codeset);
請記住還將您的目錄重命名為locale/ja_JP.utf8
。 確保您的 .po 文件以正確的編碼存儲,即本例中的UTF-8
,並包含以下行
"Content-Type: text/plain; charset=UTF-8\n"
(正如您已經完成的那樣)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.