Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14825 -- LocaleMiddleware keeps language

 * LocaleMiddleware stores language into session if it is not present there.
  • Loading branch information...
commit 6de81d65f443a01961c23139ca5d7653ef012d35 1 parent 1514f17
Vlastimil Zíma authored March 25, 2013 claudep committed May 25, 2013
4  django/middleware/locale.py
@@ -53,6 +53,10 @@ def process_response(self, request, response):
53 53
                     request.get_host(), language, request.get_full_path())
54 54
                 return HttpResponseRedirect(language_url)
55 55
 
  56
+        # Store language back into session if it is not present
  57
+        if hasattr(request, 'session'):
  58
+            request.session.setdefault('django_language', language)
  59
+
56 60
         if not (self.is_language_prefix_patterns_used()
57 61
                 and language_from_path):
58 62
             patch_vary_headers(response, ('Accept-Language',))
4  docs/releases/1.6.txt
@@ -266,6 +266,10 @@ Minor features
266 266
   ``False`` allows the field to reference proxy models. The default is ``True``
267 267
   to retain the old behavior.
268 268
 
  269
+* The middleware :class:`~django.middleware.locale.LocaleMiddleware` now
  270
+  stores active language in session if it is not present there. This
  271
+  prevents loss of language settings after session flush, e.g. logout.
  272
+
269 273
 Backwards incompatible changes in 1.6
270 274
 =====================================
271 275
 
32  tests/i18n/tests.py
@@ -1152,6 +1152,38 @@ def test_streaming_response(self):
1152 1152
         response = self.client.get('/en/streaming/')
1153 1153
         self.assertContains(response, "Yes/No")
1154 1154
 
  1155
+    @override_settings(
  1156
+        MIDDLEWARE_CLASSES=(
  1157
+            'django.contrib.sessions.middleware.SessionMiddleware',
  1158
+            'django.middleware.locale.LocaleMiddleware',
  1159
+            'django.middleware.common.CommonMiddleware',
  1160
+        ),
  1161
+    )
  1162
+    def test_session_language(self):
  1163
+        """
  1164
+        Check that language is stored in session if missing.
  1165
+        """
  1166
+        # Create an empty session
  1167
+        engine = import_module(settings.SESSION_ENGINE)
  1168
+        session = engine.SessionStore()
  1169
+        session.save()
  1170
+        self.client.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
  1171
+
  1172
+        # Clear the session data before request
  1173
+        session.save()
  1174
+        response = self.client.get('/en/simple/')
  1175
+        self.assertEqual(self.client.session['django_language'], 'en')
  1176
+
  1177
+        # Clear the session data before request
  1178
+        session.save()
  1179
+        response = self.client.get('/fr/simple/')
  1180
+        self.assertEqual(self.client.session['django_language'], 'fr')
  1181
+
  1182
+        # Check that language is not changed in session
  1183
+        response = self.client.get('/en/simple/')
  1184
+        self.assertEqual(self.client.session['django_language'], 'fr')
  1185
+
  1186
+
1155 1187
 @override_settings(
1156 1188
     USE_I18N=True,
1157 1189
     LANGUAGES=(
3  tests/i18n/urls.py
... ...
@@ -1,9 +1,10 @@
1 1
 from __future__ import unicode_literals
2 2
 
3 3
 from django.conf.urls.i18n import i18n_patterns
4  
-from django.http import StreamingHttpResponse
  4
+from django.http import HttpResponse, StreamingHttpResponse
5 5
 from django.utils.translation import ugettext_lazy as _
6 6
 
7 7
 urlpatterns = i18n_patterns('',
  8
+    (r'^simple/$', lambda r: HttpResponse()),
8 9
     (r'^streaming/$', lambda r: StreamingHttpResponse([_("Yes"), "/", _("No")])),
9 10
 )

0 notes on commit 6de81d6

Please sign in to comment.
Something went wrong with that request. Please try again.