我正在使用 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"
最佳答案
请注意,这是与 this similar question 不同的异常位置评论中提到。
在我看来,发生这种情况的唯一方法是对您的 django 安装进行了修改,或者 python 2.7 版本中存在错误。
你的堆栈是:
> msgs, errors, status = gettext_popen_wrapper(args)
> stdout = stdout.decode(stdout_encoding)
gettext_popen_wrapper
(在 django 1.8 上,我认为你正在使用它,你能确认吗?)和 popen_wrapper
创建 stdout
(为了清晰起见,删除注释/文档字符串并重新缩进后,请参阅 github 上的 popen_wrapper 和 gettext_popen_wrapper 以获取完整代码):
def popen_wrapper(args, os_err_exc_type=CommandError, universal_newlines=True):
try:
p = Popen(args, shell=False, stdout=PIPE, stderr=PIPE,
close_fds=os.name != 'nt', universal_newlines=universal_newlines)
except OSError as e:
strerror = force_text(e.strerror, DEFAULT_LOCALE_ENCODING,
strings_only=True)
six.reraise(os_err_exc_type, os_err_exc_type('Error executing %s: %s' %
(args[0], strerror)), sys.exc_info()[2])
# NB: subprocess.Popen.communicate() should return two bytes
# (i.e. str in python 2) objects
output, errors = p.communicate()
return (
output,
force_text(errors, DEFAULT_LOCALE_ENCODING, strings_only=True),
p.returncode
)
def gettext_popen_wrapper(args,
os_err_exc_type=CommandError,
stdout_encoding="utf-8"):
manual_io_wrapper = six.PY3 and stdout_encoding != DEFAULT_LOCALE_ENCODING
stdout, stderr, status_code = popen_wrapper(
args, os_err_exc_type=os_err_exc_type,
universal_newlines=not manual_io_wrapper)
if manual_io_wrapper:
stdout = io.TextIOWrapper(io.BytesIO(stdout), encoding=stdout_encoding).read()
if six.PY2:
# EXCEPTION HIT ON THE FOLLOWING LINE
stdout = stdout.decode(stdout_encoding)
return stdout, stderr, status_code
所以当我们调用 stdout.decode()
时,stdout
应该是一个普通的 str
对象(即一堆需要解码的字节) >。但是,如果是这样的话,为什么在en编码中会出现异常?如果对象已经是 unicode 对象,即如果它是 unicode
类型,我们只需要编码。果然,如果我们添加行
stdout = stdout.decode('utf-8')
之前
stdout = stdout.decode(stdout_encoding)
然后 decode
方法首先尝试使用 default encoding of ascii 对 unicode stdout
进行编码
,这会导致您看到的异常。通过将 manual_io_wrapper
设置为 True
,我也遇到了同样的错误,这导致 stdout = io.TextWrapper(...)
行发生为好吧(它也会产生一个 unicode),但这不应该是 True
因为你使用的是 python 2 而不是 3。
所以我认为:
- 您安装的
django
或six
有问题,或者它已被编辑。尝试重新安装它们。 - 您在
subprocess.Popen.communicate()
中遇到了一个错误,并且出于某种原因它返回的是unicode
而不是str
(我相信 python 3 如果universal_newlines
are turned on 是可能的。您可以通过重新安装 python 或升级到更高版本来获得里程。
但我的主要观点是,我认为这不是环境问题。知道任何后续行动会很有趣:
- 你在哪个平台上
- 您使用的是什么 python 2.7
- 你用的是什么 django。
关于python - 调用 Django i18n makemessages 命令时出现 UnicodeError 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40724723/