Django {%trans%}标记不显示任何文本

时间:2017-06-28 19:21:24

标签: django translation

我的模板中的{%trans%}标记都没有显示除了给它的msgid之外的任何内容。我能够使用makemessages正确地创建.po文件,并使用compilemessages成功编译它,但是当我运行服务器并打开一个页面时,它不会显示语言的翻译,只显示msgid。

例如:

{% trans "about-me-text" %}

这只会显示" about-me-text"在页面上而不是英文或法文翻译。

我的设置:

LOCALE_PATHS = (
    '/path/to/project/locale',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.gzip.GZipMiddleware',
)

LANGUAGE_CODE = 'en'

LANGUAGES = (
    ('en','English'),
    ('fr','French'),
)

这个项目是Django 1.3.1和Python 2.7

编辑:

我的.po文件为about-me-text

生成
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-06-28 12:48-0700\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"

#: .\templates\about_me.html.py:8
msgid "about-me-text"
msgstr "This is the about me block"

about_me template:

{% extends "base.html" %}
{% load i18n %}
{% block content %}
    <article class="wrapped">
        <h2>{% trans "About Me" %}</h2>
        <div class="divider"></div>
            {% trans "about-me-text" %}
    </article>
{% endblock %}

查看:

def about_me(request):
    return render_to_response('about_me.html', {}, context_instance=RequestContext(request))

2 个答案:

答案 0 :(得分:0)

我相信你在尝试同时做两件不同的事情。

  1. 翻译文本
  2. 将文本作为变量传递到模板中
  3. 如果是这样,这里是documentation

    {% trans about-me-text %}
    

    将文本作为变量about-me-text传递。有关如何在视图中执行此操作的详细信息,请参阅documentation

    解决你的观点:

    from django.utils.translation import ugettext_lazy as _
    
    def about_me(request):
        output = _('This is the about me block.')
        return render_to_response('about_me.html', {'output':output}, context_instance=RequestContext(request))
    

答案 1 :(得分:0)

因此,在尝试了Emile和Melvyn的一些建议后,我很幸运地将LANGUAGE_CODE改为&#39; en&#39;到了&#39; en-us&#39;并对语言进行相同的更改。我的更新设置:

LANGUAGE_CODE = 'en-us'

LANGUAGES = (
    ('en-us','English'),
    ('fr','French'),
)

我错过了这个问题,因为我忽略了此处显示的错误AttributeError: 'Nonetype' object has no attribute '_info',它告诉我&#39; en&#39;不是有效的LANGUAGE_CODE。我不确定为什么en-us在en上工作但是现在它解决了这个问题。