国际化不能正常运作 - 似乎没有采取任何行动

时间:2014-05-12 17:37:02

标签: django internationalization django-i18n

我将setting.py配置为如下所示:

LANGUAGE_CODE = 'pt-br'

USE_I18N = True

LANGUAGES = (
    ('pt_br', ('Portugues Brasileiro')),
    ('en_us', ('English'))
)

MIDDLEWARE_CLASSES = (
    ...
    'django.middleware.locale.LocaleMiddleware',
    ...
)

LOCALE_PATHS = ('/home/lucas/Documents/ProjectPython/test_internacionalizacao/test_internacionalizacao/locale/')

创建了一个名为core的应用,在其文件夹中有另一个文件夹结构:

core
---- templates
-------- core
------------ core.html
------------ idiomas.html

core.html内的代码是:

{% load i18n %}

<html>

    <head>
        <title>Internacionalização - 01</title>
    </head>

    <body>
        {% include "core/idiomas.html" %}

        <p>{% trans "Esta string será transformada para Inglês" %}</p>

    </body>

</html>

idiomas.html内的代码是:

{% load i18n %}

{% get_available_languages as LANGUAGES %}

<div class="idiomas">
    <form action="/i18n/setlang/" method="POST">
        {% csrf_token %}
        <select name="languages">
            {% for lang in LANGUAGES %}
                <option value="{{ lang.0 }}">{{ lang.1 }}</option>
            {% endfor %}
        </select>
        <input type="submit" value="{% trans "Mudar idioma" %}">
    </form>
</div>

我的urls.py是:

from django.conf.urls import patterns, include, url
from django.conf import settings

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', 'core.views.core_index'),
    url(r'^admin/', include(admin.site.urls)),
    (r'^i18n/', include('django.conf.urls.i18n')),
)

当我加载页面时,它看起来没问题,但是当我将组合框更改为English时,没有任何作用。看似页面重新加载,但组合选项又回到了Portugues Brasileiro,仍然是葡萄牙语的文本。

我在项目基础路径上创建了文件夹区域设置并运行了django-admin.py make messages -l pt_bren_us并为.po配置了en_us文件,如下所示:

# 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: 2014-05-12 12:29-0300\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"

#: core/templates/core/core.html:12
msgid "Esta string será transformada para Inglês"
msgstr "This string is being converted into English"

#: core/templates/core/idiomas.html:13
msgid "Mudar idioma"
msgstr "Change Language"

有人可以帮我弄清楚它为什么不起作用吗?这是我第一次使用国际化服务。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试删除&#34; #, fuzzy&#34;你的.po文件的标题行?无论如何,您应该这样做以便加载翻译。

如果问题仍然存在,请在idomas.html模板中打印当前语言 - 类似

{% get_current_language as lang %}{{lang}}

检查选择框是否实际更改了语言。如果是这样,问题在于翻译机制。如果没有,那就是你设置语言的方式。请报告您的调查结果,看看接下来可以做些什么。