问题描述
如何调试对gettext的使用?我已经按照此处,创建两个.pot .po文件,并生成一个.mo文件.
How can I debug my use of gettext ? I have followed the steps described here, created both .pot .po files, and generated a .mo file.
但是,当我启动页面时,什么都没有翻译,也没有错误消息,所以我不知道出了什么问题.
Yet when I launch my page, nothing is translated, and there is no error message, so I don't know what's wrong.
这是我创建翻译文件(项目名称为"ft")的方式:
Here's how I've created the translation files (the project name is 'ft') :
find ft/. -name "*.php" ! -path "*.svn*" > ft/i18n/listpot.txt
xgettext --from-code=utf-8 --default-domain=ft --output=ft/i18n/ft.pot --files-from=ft/i18n/listpot.txt
rm ft/i18n/listpot.txt
#wrote the translated strings in poedit
msgfmt -c -v -o ft/i18n/en/LC_MESSAGES/en.mo ft/i18n/ft_en.po
#restarted apache
在我的ft.php页面中,我包含包含以下内容的i18n.php:
In my ft.php page I include i18n.php which contains :
$folder = 'ft';
$lang = 'en_US.UTF8';
$domain = 'messages';
setlocale(LC_ALL, 'en_US.UTF8', 'en.UTF8', 'en_US.UTF-8', 'en.UTF-8')."<br />";
putenv("LC_ALL=$lang");
bindtextdomain("$domain", dirname(__FILE__)."/$folder/i18n");
textdomain("$domain");
bind_textdomain_codeset("$domain", 'UTF-8');
在ft.php中,我用:
In ft.php I echo strings with :
echo _('test_string');
但这回显了'test_string',而不是翻译后的版本:(
But this echoes 'test_string', not the translated version :(
有没有一种方法可以显示由php-gettext脚本生成的错误?我忘了一步吗?
Is there a way to display errors generated by the php-gettext script ? Am I forgetting a step ?
edit :我有以下文件夹树:
edit : I have the following folder tree :
./ft/i18n/en/LC_MESSAGES/messages.mo
messages.po
fr/LC_MESSAGES/messages.po
messages.mo
我尝试将"en"文件夹更改为"en_US"和"en_US.UTF8",将"messages.mo"文件更改为"en.mo","en_US.mo","en_US.UTF8.mo" ",但没有任何翻译.
I have tried changing the "en" folder to "en_US" and "en_US.UTF8", and the "messages.mo" file to "en.mo", "en_US.mo", "en_US.UTF8.mo", but nothing gets translated.
推荐答案
gettext并不是很容易调试,因为当出现问题时它什么也没说.尝试使用is_dir()检查.po和.mo文件所在的文件夹.
gettext is not very easy to debug, as it doesn't say anything when something is wrong. Try using is_dir() to check the folder where the .po and .mo files are.
在require()中,变量范围也有问题
I also had a problem with variable scope in a require()
这是对我有用的东西:
$folder = 'ft';
$domain = 'messages';
$lang = 'fr_FR.utf8';
$directory = dirname(__FILE__)."/../../$folder/i18n";
//putenv("LANG=".$locale); //not needed for my tests, but people say it's useful for windows
setlocale(LC_MESSAGES, $lang);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
目录为:
./ft/i18n/en/LC_MESSAGES/messages.mo
messages.po
fr/LC_MESSAGES/messages.po
messages.mo
这篇关于使用gettext不会翻译任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!