我正在尝试以不同语言提供网站。我正在学习我在GitHub上发现的一些教程(https://github.com/mjp/django-multi-language)。
#! -*- 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
{% load i18n %}
<p>{{ foobar }}</p>
# 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/
答案 0 :(得分:1)
在这种情况下,设置request.session['django_langauge']
没有帮助。我假设您已安装的LocaleMiddleware
将在请求到达view
函数之前解析该translation
。您应该直接使用from django.utils.translation import activate
def index(request):
activate('fr')
....
return response
函数来更改此语言。
{{1}}