无法使用Django 1.5进行国际化

时间:2014-04-07 11:33:46

标签: django localization internationalization

我正在尝试以不同语言提供网站。我正在学习我在GitHub上发现的一些教程(https://github.com/mjp/django-multi-language)。

views.py

#! -*- coding: utf-8 -*-
from django.shortcuts import render
from django.utils.translation import ugettext as _


# ──────────────────────────────────────────────────────────────────────────────
def index(request):

    # Rendering the page
    context = { 'foobar':_('Hello !') }
    response = render(request, 'home/index.html', context)
    request.session['django_language'] = 'fr'
    return response

的index.html

{% load i18n %}
<p>{{ foobar }}</p>

settings.py

# Project path for further access
PROJECT_PATH = path.realpath(path.dirname(__file__))

# Locale paths
LOCALE_PATHS = (              # Next to the settings.py
    PROJECT_PATH+'/locale/',  # the time I find a better way to
)                             # avoid hard coded absolute path

本地化非常简单,但我的页面一直在说 Hello!而不是 Bonjour!


修改

- documentation/
- locale/          <-- I want it here
- sources/
  - application1/
  - application2/
  - projectname/
    - settings.py
    - ...
    - locale/      <-- Currently here
- toolbox/

1 个答案:

答案 0 :(得分:1)

在这种情况下,设置request.session['django_langauge']没有帮助。我假设您已安装的LocaleMiddleware将在请求到达view函数之前解析该translation。您应该直接使用from django.utils.translation import activate def index(request): activate('fr') .... return response 函数来更改此语言。

{{1}}