Skip to content

Commit

Permalink
Fixed django#14825 -- LocaleMiddleware keeps language
Browse files Browse the repository at this point in the history
 * LocaleMiddleware stores language into session if it is not present there.
  • Loading branch information
ziima authored and claudep committed May 25, 2013
1 parent 1514f17 commit 6de81d6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions django/middleware/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',))
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/1.6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====================================

Expand Down
32 changes: 32 additions & 0 deletions tests/i18n/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=(
Expand Down
3 changes: 2 additions & 1 deletion tests/i18n/urls.py
Original file line number Diff line number Diff line change
@@ -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")])),
)

0 comments on commit 6de81d6

Please sign in to comment.