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

心血来潮,突然想研究下php多语言。

看下了自己公司的项目和wordpress都是用的gettext()来进行多语言化的。

于是打开php手册复制了下面一段

 

<?php
// Set language to German
putenv('LC_ALL=de_DE');
setlocale(LC_ALL, 'de_DE');
 
// Specify location of translation tables
bindtextdomain("myPHPApp", "./locale");
 
// Choose domain
textdomain("myPHPApp");
 
// Translation is looking for in ./locale/de_DE/LC_MESSAGES/myPHPApp.mo now
 
// Print a test message
echo 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能正常工作。

posted on 2014-10-08 13:35  死成鬼  阅读(1054)  评论(0编辑  收藏  举报