我正在使用 Django 1.6,我觉得我错过了一些东西,但是 cookie 设置为当前选择的语言,但显示语言保持默认。
对应代码:
设置.py
LANGUAGES = (
('hu', 'Hungarian'),
('en', 'English'),
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request"
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware'
)
LANGUAGE_CODE = 'en-US'
TIME_ZONE = 'CET'
USE_I18N = True
USE_L10N = True
USE_TZ = True
网址.py
urlpatterns = patterns('',
url(r'^i18n/', include('django.conf.urls.i18n')),
...
)
模板
{% extends 'base.html' %}
{% load i18n %}
...
<h4>{% trans "Modern Technologies" %}</h4>
...
我运行makemessages -a来创建 lang 文件,安装 Rosetta 并编辑语言。然后我运行了compilemessages。在 Chrome 中检查 cookie “django_language” 的设置是否正确。但实际文本仍然是默认的“现代技术”。