PHP gettext找不到任何转换的日志字符串
I have a website that is internationalized using php's built-in Gettext / PO support
echo _("string to be translated");
The site being in beta, there are certainly some strings that don't get translated. To help debugging, is there a way to log gettext errors, i.e. log strings for which PO has not found any matching entry in the PO file?
我有一个使用php内置的Gettext / PO支持进行国际化的网站 p>
网站处于测试阶段,肯定会有一些字符串无法翻译。 为了帮助调试,有没有办法记录gettext错误,即PO在PO文件中找不到任何匹配条目的日志字符串? p>
div> echo _(“要翻译的字符串”); code> p>
The problem with what you want to do is that you'll only find untranslated strings at runtime, and only for code paths which are actually executed. It may take years for you to find all strings, and in the meantime you have to look at your log files regularly and sync your updated translations with what's happening in the log file. That's not a deterministic approach to translations.
If you're using gettext, you need to establish a workflow that's appropriate for it. And the gettext workflow and toolchain are pretty well thought out and time tested.
- Prepare your source code by wrapping strings in gettext functions.
- Extract the prepared strings into POT files, typically using
xgettext
but possibly using a custom parser/extractor and a library like Kunststube\POTools (shameless self promotion). - Create PO files from the extracted template files using
msginit
. - Translate the PO files.
- Keep updated source/POT files in sync with PO files using
msgmerge
. - Compile PO files to MO files using
msgfmt
. - Rinse, repeat.
This is the proper gettext workflow in a nutshell, and it accounts for most problems in the translation process. Especially if you have distributed translators you typically have a time lag and overlap between source code being updated and translations coming in. Without automated string extraction and the msgmerge
utility it's easy for this process to devolve into chaos, which is why you should stick to the above steps.
If you do, your question becomes irrelevant since you can check your PO files at any time using the gettext utilities or a tool like Poedit to see which strings are untranslated.
Probably more important as your in beta would be to do some pseudo localisation, using a tool like podebug. Thus once you've extracted your PO strings you can localise them so that you can see if all strings are extracted in your UI.