gettext:如何避免 unicode 字符失败?

gettext: how to avoid fail at unicode character?

这是一个 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:\Python27\pythonw.exe C:\Python27\Tools\i18n\pygettext.py -d string_file string_file.py时,结果POT文件包含正确的header但遇到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')
}