国际化,切换语言的一些问题

时间:2013-07-26 10:56:04

标签: django internationalization locale

我有一个英文网站,想要添加俄文。 settings.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',)

urls.py

  

url(r'^ i18n /',include('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并重定向到英文页面。这有什么不对?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

y

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