我将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"
有人可以帮我弄清楚它为什么不起作用吗?这是我第一次使用国际化服务。
提前致谢!
答案 0 :(得分:0)
您是否尝试删除&#34; #, fuzzy
&#34;你的.po文件的标题行?无论如何,您应该这样做以便加载翻译。
如果问题仍然存在,请在idomas.html模板中打印当前语言 - 类似
{% get_current_language as lang %}{{lang}}
检查选择框是否实际更改了语言。如果是这样,问题在于翻译机制。如果没有,那就是你设置语言的方式。请报告您的调查结果,看看接下来可以做些什么。