Django 1.95模板i18n无法正常工作

时间:2016-09-07 10:31:59

标签: python django internationalization

我正在尝试将字符串从英语翻译为zh-hans

settings.py

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

我多次运行python manage.py makemessages -l zh-hanspython 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@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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>

标签返回&#34;作者&#34;而不是汉字。 我也在view.py

尝试了ugettext
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)

msg仍然是&#34;作者&#34;。

使用

 {% get_current_language as LANGUAGE_CODE %}
 {{ LANGUAGE_CODE }}

语言代码是zh-hans。

那么这里出了什么问题?如何在模板中显示已翻译的字符串?谢谢

1 个答案:

答案 0 :(得分:1)

这是一个非常愚蠢的错误。

虽然在setting.py中可以将中文代码设置为'zh-hans',但要实现i18n,应该是makemessages -l zh_hans而不是zh-hans。