我安装了model_translation、rosetta和locale_url,但是无法改变语言。
我的settings.py文件:
在模板中:
我的中间件:
我的settings.py文件:
LANGUAGE_CODE = 'ru'
MODELTRANSLATION_TRANSLATION_REGISTRY = "project.translation"
TRANSLATION_REGISTRY = "project.translation"
ugettext = lambda s: s
LANGUAGES = (
('ru', ugettext(u'Russian')),
('uk', ugettext(u'Ukrainian')),
)
我的语言切换观点:
def set_language(request):
next = request.REQUEST.get('next', None)
if not next:
next = request.META.get('HTTP_REFERER', None)
if not next:
next = '/'
response = http.HttpResponseRedirect(next)
if request.method == 'GET':
lang_code = request.GET.get('language', None)
if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session['django_language'] = lang_code
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
return response
在模板中:
<a href="{% url set_lang %}?lang=uk&next={{request.path}}">Ukranian</a>
我的中间件:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.locale.LocaleMiddleware',
'localeurl.middleware.LocaleURLMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
但语言切换无效。如果我打开链接 http://localhost/uk/ 进行语言切换,但在切换到另一个页面时,语言会回到默认语言。 我应该怎么办?
translation.LANGUAGE_SESSION_KEY
作为会话密钥。 - Shih-Wen Su