我使用gettext对我的php文件进行国际化。我有两个服务器:一个沙箱服务器和一个发布服务器。在沙箱服务器中,像locale/LC_MESSAGES/en这样的目录不能工作,我应该使用locale/LC_MESSAGES/en_GB。但是对于"en_GB“,它不能在我的生产服务器上工作,而且"en”可以很好地工作。对于一些语言如葡萄牙语,我有pt_PT和pt_BR (巴西葡萄牙语)。所以我更喜欢使用"A_B“结构。
我不知道gettext如何检测这些文件夹。是否有使用相同文件夹结构的标准方法?
发布于 2013-08-19 04:27:01
如果您在Linux上运行代码,gettext只对已经安装在操作系统上的地区起作用。这意味着,如果将区域设置为en_GB,那么如果只安装了en_GB.utf8或en_US,则不会得到翻译。
在您的两种环境中都尝试这样做,并比较结果:
locale -a它给出了所有已安装区域的列表:
en_US
en_US.ISO8859-1
en_US.ISO8859-15
en_US.US-ASCII
en_GB
en_GB.utf8
de_DE
de_DE.utf8
C
POSIX现在,您需要确保这两个环境都安装了相同的区域设置;如果需要en_US.utf8、en_AU和en_AU.utf8,可以根据现有的环境创建缺失的区域设置(阅读localedef手册了解详细信息):
sudo localedef -c -i en_US -f UTF-8 en_US.utf8
sudo localedef -c -i en_GB -f UTF-8 en_AU
sudo localedef -c -i en_GB -f UTF-8 en_AU.utf8另外,下面是用于在PHP上使用一般最佳做法的gettext:
<?php
// Set language to German
putenv('LC_ALL=de_DE.utf8');
setlocale(LC_ALL, 'de_DE.utf8');
// Specify location of translation tables
bindtextdomain("myPHPApp", "./locale");
// Choose domain
textdomain("myPHPApp");
// Translation is looking for in ./locale/de_DE.utf8/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");
?>虽然您可以简单地删除编码,只需要de_DE,但是在区域设置中设置字符集是一种很好的做法,因为在某些特定情况下,您可能需要支持非Unicode字符集中的内容。见下文
<?php
// Set language to German written in Latin-1
putenv('LC_ALL=de_DE.ISO8859-1');
setlocale(LC_ALL, 'de_DE.ISO8859-1');
?>https://stackoverflow.com/questions/18304566
复制相似问题