0

我将我的配置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_br并运行en_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"

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

提前致谢!

4

1 回答 1

0

您是否尝试#, fuzzy过从 .po 文件的标题中删除 " " 行?无论如何,您都应该这样做以加载翻译。

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

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

检查选择框是否真的改变了语言。如果是这样,问题出在翻译机制上。如果不是,那是您设置语言的方式。请报告您的发现,看看接下来可以做什么。

于 2014-05-15T09:11:25.730 回答