【发布时间】:2017-04-05 02:48:15
【问题描述】:
我正在使用 Django 的国际化功能为 web 应用程序生成翻译字符串。
我尝试调用makemessages 时出现问题,而现有语言.po 文件包含特殊字符(例如$、£ 等)。
如果其中之一存在,makemessages 会尝试加载现有的.po 文件并对其进行解码。当它这样做时,我得到一个错误:
Traceback (most recent call last):
File "manage.py", line 18, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemessages.py", line 325, in handle
self.write_po_file(potfile, locale)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemessages.py", line 458, in write_po_file
msgs, errors, status = gettext_popen_wrapper(args)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemessages.py", line 51, in gettext_popen_wrapper
stdout = stdout.decode(stdout_encoding)
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa2' in position 2105: ordinal not in range(128)
我试图在这里追溯回溯,但我不知道发生了什么。
似乎 Django 尝试将现有的 .po 文件解码为 UTF8,但是在重新编码时,它使用的是 ASCII 编解码器。
我们将不胜感激任何有关问题所在的见解。
编辑:
- 操作系统:Ubuntu 15.10 和 OS X 10.11.6
- Python:2.7.10 和 2.7.11
- Django:1.8.14
- 六:1.10.0
我已尝试按照建议重新安装 Django/Six,但错误仍然存在。
Ubuntu 的localedef --list-archive:
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
有问题的翻译文件的内容类型:
"Content-Type: text/plain; charset=UTF-8\n"
【问题讨论】:
-
试试这个:
LC_CTYPE=en_US.UTF-8 python manage.py makemessages;有用吗? -
@nobe4:使用
LC_CTYPE=en_US.UTF-8 python manage.py makemessages运行它会有所不同吗?还有,顺便说一句,你的操作系统和版本是什么? -
@AntonisChristofides:抱歉耽搁了,这个命令不能解决问题。我已详细更新了我的问题。
-
你能显示
localedef --list-archive的输出吗?这将适用于 Ubuntu;我不确定 Mac OS X。
标签: python django utf-8 translation