django翻译无法解码

时间:2013-10-10 11:44:59

标签: python django

在django.po中我有这个。

msgid "%(message)s come from weixiang http:///www.iweix.cn "
msgstr "%(message)s 来自微X http:///www.iweix.cn"

并且在视图中我有,(我已添加# - - 编码:utf-8 - - )。

sina_weibo_post(self.user,_("%(message)s come from weixiang http:///www.iweix.cn ")%{"message":self.photo.desc},photo_adr)

我收到了错误:

sina_weibo_post(self.user,_("%(message)s come from weixiang http:///www.iweix.cn")%{"message":self.photo.desc},photo_adr)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 12: ordinal not in range(128)

我该如何解决?谢谢。

2 个答案:

答案 0 :(得分:1)

请检查po文件中是否有这样的标题:

msgid ""
msgstr ""
"Project-Id-Version: 1.0.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-10-04 13:06-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: XXXXX\n"
"Language-Team: Es XXXX\n"
"Language: Es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

这一行非常重要:

"Content-Type: text/plain; charset=UTF-8\n"

答案 1 :(得分:1)

如果您使用的是Python 2和Django <= 1.11(如此处建议的那样),则另一个可能的解决方法是使用My codes now are like the second image. Thak you all for your help.而不是gettextgettext_lazyugettext or ugettext_lazy