Django设置了语言

时间:2013-06-06 20:41:48

标签: django internationalization

我在我的视图中设置这样的语言。

    if request.LANGUAGE_CODE == 'en':
        translation.activate('en')
    else:
        translation.activate('pt')

进入url / pt / foo / faa /

它有效。

但语言没有设定。一旦我点击链接,就像关于链接一样,网址就会改为/en/about/../而不是/pt/about../,正如我认为的那样。

这是我的网址:

from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns('',
    (r'^i18n/', include('django.conf.urls.i18n')),
    (r'^news/', include('news.urls')),
    ....

Settings.py

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = ()

我是否以错误的方式理解这一点?

2 个答案:

答案 0 :(得分:2)

阅读the docs

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.i18n,
    )

您应该在TEMPLATE_CONTEXT_PROCESSORS设置中拥有该功能。

现在尝试向上移动Locale这样的中间件:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

中间件计数和区域设置的顺序应该在会话之后。

很难猜出问题会是什么,因为这个过程中有很多细节,但很好地解释了here。这是django用来检测i18n的算法。仔细阅读它的链接。它一定是我们遗漏的东西。

希望它有所帮助!

答案 1 :(得分:1)

您的设置和网址都存在一些配置问题。

首先,更新您的settings.py

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"
)
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
 )
 USE_I18N = True

接下来,在urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns('',
    (r'^foo/fla/$', 'foo.flah.view'))

urlpatterns += patterns('/i18n/', include('django.conf.urls.i18n'))

请注意,您未在i18n_patterns中包含帮助程序视图。

使用此表单创建模板,该模板将设置会话的语言:

<form action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}" />
<select name="language">
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}>
    {{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>

或者,如果您想在视图中“手动”设置它,请确保返回RequestContext,否则您的语言将无法停留。使用render() shortcut确保您的视图返回正确的上下文。