我正在尝试在 Django 网站上安装俄语翻译。
我所做的:在“语言环境”文件夹中,与其他语言一起创建了“ru”文件夹,然后在其中创建了“LC_MESSAGES”文件夹,这就是我放置带有翻译的“django.po”文件的地方。我手动创建了那个 .po 文件,因为翻译是作为 Excel 文件提供给我的。在那个 .po 文件中,我复制了在同一个项目的另一个 .po 文件中找到的标头,然后使用脚本创建了“msgid”/“msgstr”对,从 Excel 文件中提取此信息。我检查了文件,在手动修改了一些错误后,它似乎是正确的,并且在执行编译消息时,我没有收到任何错误,并且创建了 .mo 文件。然后我在设置文件中添加了“ru”作为一种语言。然后我重新加载了主页,在我们用来切换语言的小部件中出现了俄语。但是当我单击它时,它会重新加载页面并且我收到 Unicode 错误。
一些上下文:该站点是英文的,但已经安装了西班牙语翻译,并且可以使用非 ascii 字符正常工作。
欢迎任何想法,即使不是一个完整的解决方案,只是一种调试它的方法将不胜感激。
错误是:“ascii”编解码器无法解码位置 0 中的字节 0xd0:序数不在范围内(128)。你传入 ()
从我发现这是 ugettext 懒惰的问题,但我不明白为什么它适用于其他翻译......
我从 Django 得到一个 Unicode 提示:无法编码/解码的字符串是:������ 这个字符串会根据我尝试加载的页面而变化。
这是回溯:
Traceback:
File "/home/env/project/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
136. response.render()
File "/home/env/project/lib/python2.7/site-packages/django/template/response.py" in render
96. self._set_content(self.rendered_content)
File "/home/env/project/lib/python2.7/site-packages/django/template/response.py" in rendered_content
75. content = template.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
123. return self._render(context)
File "/home/env/project/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
57. return self.nodelist.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render_node
757. return node.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/home/env/project/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
57. return self.nodelist.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render_node
757. return node.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/home/env/project/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
57. return self.nodelist.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render_node
757. return node.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render_node
757. return node.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/templatetags/i18n.py" in render
76. return _render_value_in_context(output, context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in _render_value_in_context
777. value = force_unicode(value)
File "/home/env/project/lib/python2.7/site-packages/django/utils/encoding.py" in force_unicode
93. raise DjangoUnicodeDecodeError(s, *e.args)
Exception Type: DjangoUnicodeDecodeError at /
Exception Value: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128). You passed in <django.utils.functional.__proxy__ object at 0x9f3f16c> (<class 'django.utils.functional.__proxy__'>)
编辑:
我试图从头开始。我删除了手动创建的语言环境文件夹和 .po 文件以尝试使用“makemessages”命令,在执行该命令期间出现此错误:
processing language ru
Traceback (most recent call last):
File "./manage.py", line 15, in <module>
execute_manager(settings)
File "/home/env/project/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/home/env/project/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/env/project/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/env/project/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/home/env/project/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/home/env/project/lib/python2.7/site-packages/django/core/management/commands/makemessages.py", line 365, in handle_noargs
make_messages(locale, domain, verbosity, process_all, extensions, symlinks, ignore_patterns, no_wrap, no_obsolete)
File "/home/env/project/lib/python2.7/site-packages/django/core/management/commands/makemessages.py", line 233, in make_messages
f.write(templatize(src, orig_file[2:]))
File "/home/env/project/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 127, in templatize
return _trans.templatize(src, origin)
File "/home/env/project/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 450, in templatize
content = u''.join(comment)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128)
所以很明显,其他地方一定有问题。我会继续寻找。
编辑:
显然,“makemessages”工具无法处理模板中的非 ascii 字符,无论是否标记为要翻译。我发现了很多,但我还不知道如何处理它们。一旦我找到它,我会在这里报告。