问题描述
我对GetText有问题,实际上我的问题是:可能可以手动设置.mo-filepath吗?
I have a problem with GetText and my question actually is: Is it maybe possible to set the .mo-filepath manually?
如果我在shell中键入"locales -a",则会得到以下列表
If I type "locales -a" in a shell, I get the following list
C
de_DE.utf8
en_US.utf8
POSIX
我的PHP代码像这样:
My PHP-Code likes like this:
$directory = './locale';
$domain = 'texts';
$locale = 'fr_FR.utf8';
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
putenv('LC_MESSAGES='.$locale);
setlocale(LC_MESSAGES, $locale);
如果我将变量$ locale更改为"en_US"或"de_DE",并且该文件可用,则可以正常工作.但是它不适用于不在上面列表中的语言环境.但是必须有一种无需安装所有语言即可完成此操作的方法.
If I change the variable $locale to "en_US" or "de_DE" and the file is available, it works perfectly. But it does not work with locales not in the list above. But there must be a way to do this without installing every language..
也许您知道一个很好的建议.预先谢谢;)
Maybe you know a good advice. Thank you in advance ;)
推荐答案
下面是对前面提到的一种变通方法的解释:
Here's an explanation of a dirty workaround that has been mentioned previously:
如果我的语言环境文件夹如下:
If my locale folder looks like:
locale
fr_FR.UTF-8
zh_CN.UTF-8
如果我知道我的代码将在至少具有'en_US.UTF-8'语言环境的系统上运行,则可以在每种语言下用该名称创建一个虚拟文件夹,其中包含.po或.mo文件:
If I know that the systems my code will run on at least have the 'en_US.UTF-8' locale, I can make a dummy folder under each language with that name, containing the .po or .mo files:
locale
fr_FR.UTF-8
en_US.UTF-8
zh_CN.UTF-8
en_US.UTF-8
然后,我可以这样设置我的语言环境
Then, I can set my locale like this
// Dynamically set your desired locale here
$locale = 'fr_FR.UTF-8'
// Set locale to the same 'safe' locale every time
setlocale(LC_ALL, 'en_US.UTF-8');
// Call bindtextdomain with the directory containing the fake 'en_US.UTF-8' folder that is actually a different language
bindtextdomain('domain', BASE_DIR . DS . 'locale' . DS . $locale);
bind_textdomain_codeset('domain', 'UTF-8');
textdomain('domain');
有点骇人听闻,但它就像一种魅力.
Kind of hacky, but it works like a charm.
希望有人可以向bindtextdomain添加更多选项,以便可以自定义其如何处理当前设置的语言环境
Wish someone would add more options to bindtextdomain so that how it handles the currently set locale can be customized
¯__(ツ)_/¯
这篇关于在服务器上没有语言环境的PHP GetText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!