Gettext:如何避免Unicode字符失败?



这是一个 python 文件,它会导致gettext在 unicode 字符代码u2191失败。

texts = {
'first': _(u'Hello world'),
'fails': _(u'Arrow: u2191'),  # This code causes problems for gettext
'omitted': _(u'Innocent string here')
}

在命令行中运行C:Python27pythonw.exe C:Python27Toolsi18npygettext.py -d string_file string_file.py时,结果 POT 文件包含正确的标头,但在遇到 unicode 箭头时失败:

#: translate.py:2
msgid "Hello world"
msgstr ""
#: translate.py:3
msgid

我该怎么做才能让它与 unicode 字符代码一起使用?

解决方法是从要翻译的字符串中删除代码

# Not wrapped in _() so does not enter gettext
arrrow_char = u'u2191'
# These are now accessible to gettext
texts = {
'first': _(u'Hello world'),
'fails': _(u'Arrow: %s') %arrow_char,  # No longer causes a problem
'omitted': _(u'Innocent string here')
}

最新更新