nob*_*be4 6 python django translation utf-8
我正在使用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)
Run Code Online (Sandbox Code Playgroud)
我试图在这里追溯回溯,但我不知道发生了什么.
似乎Django试图将现有.po文件解码为UTF8,但是当重新编码它时,它正在使用ASCII编解码器.
任何有关错误的见解都会受到大力赞赏.
编辑:
我按照建议尝试重新安装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
Run Code Online (Sandbox Code Playgroud)
有问题的翻译文件的内容类型:
"Content-Type: text/plain; charset=UTF-8\n"
Run Code Online (Sandbox Code Playgroud)
请注意,这是与评论中提到的类似问题不同的异常位置。
在我看来,发生这种情况的唯一方法是你的 django 安装发生了修改,或者 python 2.7 版本中存在错误。
你的堆栈是:
> msgs, errors, status = gettext_popen_wrapper(args)
> stdout = stdout.decode(stdout_encoding)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
因此,当我们调用 时stdout,应该是一个普通str对象(即一堆需要解码的字节)stdout.decode()。但是,如果是这样的话,为什么en编码会出现异常呢?仅当对象已经是 unicode 对象(即类型为 )时,我们才需要进行编码unicode。果然,如果我们添加这一行
stdout = stdout.decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
前
stdout = stdout.decode(stdout_encoding)
Run Code Online (Sandbox Code Playgroud)
然后现在该decode方法首先尝试使用默认编码 asciiencode来访问unicode ,这会导致您看到的异常。我也通过设置to得到了同样的错误,这也导致了该行的发生(它也生成了一个unicode),但这不应该是因为你使用的是python 2而不是3。stdoutmanual_io_wrapperTruestdout = io.TextWrapper(...)True
所以我认为:
django或six,或者它已被编辑。尝试重新安装它们。subprocess.Popen.communicate()并且由于某种原因它返回了一个unicode而不是str(我相信在 python 3 中,如果universal_newlines打开的话这是可能的。你可以通过重新安装 python 或升级到更高版本来获得里程。但我的主要观点是,我认为这不是环境问题。了解任何后续行动将会很有趣: