2

我有一个英文网站,想添加俄文。设置.py

LANGUAGE_CODE = 'en'

LANGUAGES = (('en', ('English')), ('ru', ('Russian')), )

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.middleware.clickjacking.XFrameOptionsMiddleware', )

网址.py

url(r'^i18n/', 包括('django.conf.urls.i18n')),

简单的表格 grom django docs

> <form action="/i18n/setlang/" method="post" id="language-select">
>        <input name="next" type="hidden" value="/" />
>             {% csrf_token %}
>             <select name="language">
>                 {% for lang in LANGUAGES %}
>                     <option value="{{ lang.0 }}">{{ lang.1 }}</option>
>                 {% endfor %}
>             </select>
>        <input type="submit" value="Go" /> </form>

我安装gettext,添加'locale'文件夹(在项目文件夹中,靠近manage.py)。使用 .po 文件创建“ru 文件夹”并添加一些翻译。但是在更改语言后,它会返回 [26/Jul/2013 13:43:32] "POST /i18n/setlang/ HTTP/1.1" 302 0 并重定向到英文页面。这里有什么问题?

4

2 回答 2

4

你必须把django.middleware.locale.LocaleMiddleware 之后 django.contrib.sessions.middleware.SessionMiddleware之前 django.middleware.common.CommonMiddleware

例子:

MIDDLEWARE_CLASSES = (
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.locale.LocaleMiddleware',
   'django.middleware.common.CommonMiddleware',
)

https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-language-preference

于 2013-07-26T11:38:59.707 回答
0

您是否在 urls.py 中使用 i18n_patterns 作为支持两种语言的 URL?

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

urlpatterns += i18n_patterns(
    url(r'^about/$', views.anything, name='about'),
)

https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#language-prefix-in-url-patterns

于 2016-04-19T19:38:39.627 回答