2

在我看来,我设置这样的语言。

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

当去 url /pt/foo/faa/

有用。

但是语言没有设置。一旦我点击了一个链接,比如 about 链接,url 就会变成 /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')),
    ....

设置.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 = ()

我理解错了吗?

4

2 回答 2

2

文档中所述

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

中间件计数和语言环境的顺序应该在会话之后。

真的很难猜出问题是什么,因为这个过程中有很多细节,但这里解释得很好。这就是 django 用来检测 i18n 的算法。按照它的链接仔细阅读它。这一定是我们缺少的东西。

希望能帮助到你!

于 2013-06-06T20:57:15.677 回答
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>

或者,如果您想在您的视图中“手动”设置它,请确保您返回 a RequestContext,否则您的语言不会坚持。使用render()快捷方式确保您的视图返回正确的上下文。

于 2013-06-06T21:15:54.073 回答