Internationalization not working properly - No act

2019-09-09 02:49发布

问题:

I configured my setting.py as show below:

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/')

Created an app called core that has in its folder another folder structure as:

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

The code inside core.html is:

{% 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>

And the code inside idiomas.html is:

{% 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>

My urls.py is:

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')),
)

When I load the page it looks ok, but when I change the combobox to English nothing works. Seemingly the page reloads but the combo option is back to Portugues Brasileiro and still the text in portuguese.

I create the folder locale on my projects base path and runned django-admin.py make messages -l pt_br and en_us and configued the .po file for en_us which is like this:

# 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"

Could someone help me to figure out why it is not working? This is the first time I am using internationalization service.

Thanks in advance!

回答1:

have you tried removing the "#, fuzzy" line from the header of your .po file? You should do this anyway for the translations to load.

If the problem persists print the current language in the idomas.html template - something like

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

to check if the select box actually changes the language. If it does, the problem lies in the translations mechanism. If not, it's in the way you set the language. Please report back your findings to see what can be done next.