Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #14825 -- LocaleMiddleware keeps language

 * LocaleMiddleware stores language into session if it is not present there.
  • Loading branch information...
commit 6de81d65f443a01961c23139ca5d7653ef012d35 1 parent 1514f17
@ziima ziima authored claudep committed
View
4 django/middleware/locale.py
@@ -53,6 +53,10 @@ def process_response(self, request, response):
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
4 docs/releases/1.6.txt
@@ -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
32 tests/i18n/tests.py
@@ -1152,6 +1152,38 @@ def test_streaming_response(self):
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
3  tests/i18n/urls.py
@@ -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")])),
)
Please sign in to comment.
Something went wrong with that request. Please try again.