0
我试图从英文翻译的字符串ZH-汉斯的Django 1.95模板国际化不能正常工作
在settings.py
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
我已经运行了python manage.py makemessages -l zh-hans
和python manage.py makemessages -a
多次。
的.po文件很简单如下:
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-09-07 09:53+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: blog/views.py:18
msgid "author"
msgstr "作者"
在HTML文件中:
<div id = "info">
<p>
{% trans "author" %} : {{ blog.author }}
</p>
</div>
,而不是中国文字标签返回 “作者”。 此外,我已经在view.py
def detail(request, slug):
blog = get_object_or_404(Post, slug=slug)
msg = _("author")
print(msg)
context = {
'blog': blog,
'author': msg,
}
return render(request, 'blog/detail.html', context)
味精仍然是 “作家” 试图ugettext。
使用
{% get_current_language as LANGUAGE_CODE %}
{{ LANGUAGE_CODE }}
语言代码ZH-汉斯。
那么这里怎么回事?如何在模板中显示翻译的字符串?谢谢
你用'python manage.py compilemessages'编译你的.po文件吗? –
@SardorbekImomaliev是的,我已经做了很多次 – Mix
你确定你的django发现你的翻译?上次我遇到问题,这是由于我的翻译不是bieng发现https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#how-django-discovers-translations因此我不得不手动将它们添加到https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-LOCALE_PATHS –