Fixed #14825 -- LocaleMiddleware keeps language

* LocaleMiddleware stores language into session if it is not present there.
This commit is contained in:
Vlastimil Zíma 2013-03-25 15:45:24 +01:00 committed by Claude Paroz
parent 1514f17aa6
commit 6de81d65f4
4 changed files with 42 additions and 1 deletions

View File

@ -53,6 +53,10 @@ class LocaleMiddleware(object):
request.get_host(), language, request.get_full_path())
return HttpResponseRedirect(language_url)
# Store language back into session if it is not present
if hasattr(request, 'session'):
request.session.setdefault('django_language', language)
if not (self.is_language_prefix_patterns_used()
and language_from_path):
patch_vary_headers(response, ('Accept-Language',))

View File

@ -266,6 +266,10 @@ Minor features
``False`` allows the field to reference proxy models. The default is ``True``
to retain the old behavior.
* The middleware :class:`~django.middleware.locale.LocaleMiddleware` now
stores active language in session if it is not present there. This
prevents loss of language settings after session flush, e.g. logout.
Backwards incompatible changes in 1.6
=====================================

View File

@ -1152,6 +1152,38 @@ class LocaleMiddlewareTests(TransRealMixin, TestCase):
response = self.client.get('/en/streaming/')
self.assertContains(response, "Yes/No")
@override_settings(
MIDDLEWARE_CLASSES=(
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
),
)
def test_session_language(self):
"""
Check that language is stored in session if missing.
"""
# Create an empty session
engine = import_module(settings.SESSION_ENGINE)
session = engine.SessionStore()
session.save()
self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
# Clear the session data before request
session.save()
response = self.client.get('/en/simple/')
self.assertEqual(self.client.session['django_language'], 'en')
# Clear the session data before request
session.save()
response = self.client.get('/fr/simple/')
self.assertEqual(self.client.session['django_language'], 'fr')
# Check that language is not changed in session
response = self.client.get('/en/simple/')
self.assertEqual(self.client.session['django_language'], 'fr')
@override_settings(
USE_I18N=True,
LANGUAGES=(

View File

@ -1,9 +1,10 @@
from __future__ import unicode_literals
from django.conf.urls.i18n import i18n_patterns
from django.http import StreamingHttpResponse
from django.http import HttpResponse, StreamingHttpResponse
from django.utils.translation import ugettext_lazy as _
urlpatterns = i18n_patterns('',
(r'^simple/$', lambda r: HttpResponse()),
(r'^streaming/$', lambda r: StreamingHttpResponse([_("Yes"), "/", _("No")])),
)