標籤:style blog color io os 使用 for 檔案 sp
心血來潮,突然想研究下php多語言。
看下了自己公司的項目和wordpress都是用的gettext()來進行多語言化的。
於是開啟php手冊複製了下面一段
<?php// Set language to Germanputenv(‘LC_ALL=de_DE‘);setlocale(LC_ALL, ‘de_DE‘); // Specify location of translation tablesbindtextdomain("myPHPApp", "./locale"); // Choose domaintextdomain("myPHPApp"); // Translation is looking for in ./locale/de_DE/LC_MESSAGES/myPHPApp.mo now // Print a test messageecho gettext("Welcome to My PHP Application"); // Or use the alias _() for gettext()echo _("Have a nice day");?>
然後網上下了個POedit用來編譯mo檔案
結果過程中投了個懶,將mo檔案名稱改成了test,將代碼裡的textdomin值賦值成test
呵呵.....結果就悲劇了
gettext()函數弄死翻譯不了了
然後各種嘗試以下是不科學的實驗過程(函數要求textdomain值需要與mo檔案名稱一致)
| textdomain值 |
mo檔案名稱 |
gettext運行狀態 |
注釋 |
| he |
he.mo |
正常 |
|
| heh |
heh.mo |
正常 |
|
| test |
test.mo |
撲街 |
|
| zh_CN |
zh_CN.mo |
撲街 |
|
| zh_CN |
zh__CN.mo |
正常 |
中間是兩條底線 |
| hehe |
hehe.mo |
撲街 |
|
| heheh |
heheh.mo |
正常 |
從存字元長度大於等於5時開始正常 |
| hehehehe |
hehehehe.mo |
正常 |
|
| heheheheh |
heheheheh.mo |
正常 |
|
| hehehehehe |
hehehehehe.mo |
正常 |
|
| myPHPApp |
myPHPApp.mo |
正常 |
|
| myphpapp |
myphpapp.mo |
正常 |
|
| translations |
translations.mo |
正常 |
|
結論:gettext翻譯函數對應textdomain的值在等於4個字元(或添加一個底線)的時候,會出現讀取錯誤的bug(註:當textdomain值與mo檔案名稱不能對應時,系統會嘗試在緩衝中尋找,如果有緩衝,則gettext能正常工作。
)
PHP gettext使用方法 附加奇怪bug一枚