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

标签 python unicode translation gettext pot

这是一个 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')
}

关于python - gettext:如何避免 unicode 字符失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52609439/

相关文章:

python - 在 Python 中将字符串截断为字节长度

reactjs - 如何将翻译后的字符串传递给react-jhipster中的另一个组件?

python - 用 Python 编写的 SQL 查询的语法问题

python - 剧情背景图片

unicode - 在数据库表中存储货币符号

Symfony 翻译不起作用

Python:gettext 不会在 Windows 上加载翻译

python - 加快卡迈克尔数的搜索速度?

python - 在 Pandas 的特定位置创建一个新列

html - 就加载时间而言,unicode 或 img 哪个更快?