我正在使用Django的国际化特性为webapp生成翻译字符串。
当我尝试调用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编解码器。
任何关于问题所在的见解都会受到极大的赞赏。
编辑:
我试着按照建议重新安装Django/6,但是错误仍然存在。
Ubuntu's 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"发布于 2016-11-26 14:47:45
注这是与注释中提到的https://stackoverflow.com/questions/22106777/unicode-issue-with-makemessages-all-django-1-6-2-python-3-3不同的异常位置。
在我看来,发生这种情况的唯一方法是,如果已经对django安装进行了修改,或者python2.7版本中有一个bug。
你的堆栈是:
> msgs, errors, status = gettext_popen_wrapper(args)
> stdout = stdout.decode(stdout_encoding)gettext_popen_wrapper (在django 1.8上,我认为您正在使用它,您能确认吗?)和popen_wrapper,它创建了stdout (在删除了注释/文档字符串并为了清晰起见,请参阅github上的包装器和包装器以获得未掺假的代码):
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时,str应该是一个普通的stdout.decode()对象(即需要解码的一组字节)。但是,如果是这样的话,为什么en编码中会有例外呢?如果对象已经是unicode对象,即如果它是unicode类型,那么我们只需要对其进行编码。当然,如果我们加上这一行
stdout = stdout.decode('utf-8')在此之前
stdout = stdout.decode(stdout_encoding)然后,decode方法首先尝试使用默认的ascii编码来访问unicode stdout,这将导致您看到的异常。通过将manual_io_wrapper设置为True,我也得到了同样的错误,这也导致了stdout = io.TextWrapper(...)行的发生(这也会产生一个unicode ),但这不应该是True,因为您在python 2而不是3。
所以我想要么是:
django或six的安装很糟糕,或者已经被编辑了。试着重新安装它们。subprocess.Popen.communicate()中遇到了一个bug,由于某种原因,它返回的是一个unicode,而不是一个str (我相信unicode3如果被打开的话,这是可能的。您可以通过重新安装python或升级到更高的版本来获得里程。但我的主要观点是,我不认为这是一个环境问题。很有兴趣知道任何后续行动:
发布于 2016-11-30 15:29:33
在下面的行中,不知何故,stdout不是字节str,而是它的unicode,在该unicode的隐式编码过程中,您将得到异常。
stdout = stdout.decode('utf-8')这是因为decode()应该在字节str上执行,当我们尝试在unicode上调用decode时,在python2.7中,在decode之前会有一个对encode的隐式调用,而对encode的调用将使用默认的charset,在python中是ascii。
unicode.encode() --> byte # results in str
byte.decode() --> unicode # results in unicode
unicode.decode() --> unicode.encode().decode() # implicit encode call所以,开始调查导致stdout成为unicode的原因。
谢谢。
https://stackoverflow.com/questions/40724723
复制相似问题