PHP gettext使用方法 附加奇怪bug一枚

來源:互聯網
上載者:User

標籤: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一枚

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.