Django i18n不改变语言

时间:2014-01-16 15:45:15

标签: python django internationalization

我正试图用I18N将我的Django网站国际化。当我在模板中设置语言时,它不会改变。这是我的代码:

# my flow
PROJECT
- LOCALE
- MYSITE
  - urls.py
  - settings.py
- APP1
  - views.py
- APP2
manage.py

# settings.py
LANGUAGES = (
 ('it', 'Italiano'),
 ('en', 'English'),
        )
LANGUAGE_CODE = 'it'
USE_I18N = True
LOCALE_PATHS = ('home/project/locale/',)
MIDDLEWARE_CLASSES = ( ...
                  'django.middleware.locale.LocaleMiddleware',
                 )   
TEMPLATE_CONTEXT_PROCESSORS = (...,
                  'django.core.context_processors.i18n',
                 )

# views.py

from django.utils.translation import ugettext as _
text = _("this is a text")
...
return render_to_response('index.html', {'text': text,
                         },
                          context_instance=RequestContext(request)) 
# index.html
...
{% trans 'Dashboard' %}

# In index.html I change the language and I 
<form action="{{site_url}}i18n/setlang/" method="
{% csrf_token %}
<input name="next" type="hidden" value="" />
<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="Change" />
</form>

# urls.py

urlpatterns = patterns('',
     (r'^admin/', include(admin.site.urls)),
     (r'^i18n/', include('django.conf.urls.i18n')),
      url(r'^$', TemplateView.as_view(template_name="login.html")),
     ....

当我改变语言时没有任何反应。我哪里做错了?非常感谢您的帮助

2 个答案:

答案 0 :(得分:1)

在你的urls.py中:

    from django.conf.urls.i18n import i18n_patterns

    urlpatterns += i18n_patterns('',

    )

或者(如果您想要翻译所有网址:

    urlpatterns = i18n_patterns('',

    )

要启用更改lang url请求添加(确保这是在i18n_patterns

之外
    (r'^i18n/', include('django.conf.urls.i18n'))

答案 1 :(得分:1)

从PyCHarm迁移到Linux服务器时遇到同样的问题。在本地服务器上,LANGUAGES设置可以在xx-aa中找到,但是当我们尝试使用相同配置的Ubuntu时,它只能在LANGUAGE_CODE设置中找到默认语言。

如何在LANGUAGES数组中将所有LANGUAGES设置为在Ubuntu Linux下处于活动状态。