From 7dd0ceba2e8413d79c2aaff62733f4631cd3c1e7 Mon Sep 17 00:00:00 2001
From: Jannis Leidel <jannis@leidel.info>
Date: Sat, 18 Feb 2012 13:37:30 +0000
Subject: [PATCH] =?UTF-8?q?Fixed=20#17720=20--=20Stopped=20the=20LocaleMid?=
 =?UTF-8?q?dleware=20from=20overeagerly=20using=20the=20request=20path=20f?=
 =?UTF-8?q?or=20language=20activation=20if=20it's=20actually=20not=20wante?=
 =?UTF-8?q?d.=20Thanks=20to=20Anssi=20K=C3=A4=C3=A4ri=C3=A4inen=20for=20th?=
 =?UTF-8?q?e=20initial=20patch.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17547 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
 django/middleware/locale.py                        |  4 +++-
 django/utils/translation/__init__.py               |  4 ++--
 django/utils/translation/trans_null.py             |  2 +-
 django/utils/translation/trans_real.py             | 12 ++++++++----
 tests/regressiontests/i18n/patterns/tests.py       | 14 ++++++++++++++
 .../i18n/patterns/urls/path_unused.py              | 10 ++++++++++
 tests/regressiontests/i18n/tests.py                |  2 +-
 7 files changed, 39 insertions(+), 9 deletions(-)
 create mode 100644 tests/regressiontests/i18n/patterns/urls/path_unused.py

diff --git a/django/middleware/locale.py b/django/middleware/locale.py
index d42a615b04..2a71f3d150 100644
--- a/django/middleware/locale.py
+++ b/django/middleware/locale.py
@@ -15,7 +15,9 @@ class LocaleMiddleware(object):
     """
 
     def process_request(self, request):
-        language = translation.get_language_from_request(request)
+        check_path = self.is_language_prefix_patterns_used()
+        language = translation.get_language_from_request(
+            request, check_path=check_path)
         translation.activate(language)
         request.LANGUAGE_CODE = translation.get_language()
 
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
index 8ad9c5d59a..dbe48239d0 100644
--- a/django/utils/translation/__init__.py
+++ b/django/utils/translation/__init__.py
@@ -144,8 +144,8 @@ def check_for_language(lang_code):
 def to_locale(language):
     return _trans.to_locale(language)
 
-def get_language_from_request(request):
-    return _trans.get_language_from_request(request)
+def get_language_from_request(request, check_path=False):
+    return _trans.get_language_from_request(request, check_path)
 
 def get_language_from_path(path):
     return _trans.get_language_from_path(path)
diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py
index 5c9edb1dd4..0fabc707ce 100644
--- a/django/utils/translation/trans_null.py
+++ b/django/utils/translation/trans_null.py
@@ -55,7 +55,7 @@ def to_locale(language):
     else:
         return language.lower()
 
-def get_language_from_request(request):
+def get_language_from_request(request, check_path=False):
     return settings.LANGUAGE_CODE
 
 def get_language_from_path(request):
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
index 419b3809e7..82072b1397 100644
--- a/django/utils/translation/trans_real.py
+++ b/django/utils/translation/trans_real.py
@@ -363,20 +363,24 @@ def get_language_from_path(path, supported=None):
         if lang_code in supported and check_for_language(lang_code):
             return lang_code
 
-def get_language_from_request(request):
+def get_language_from_request(request, check_path=False):
     """
     Analyzes the request to find what language the user wants the system to
     show. Only languages listed in settings.LANGUAGES are taken into account.
     If the user requests a sublanguage where we have a main language, we send
     out the main language.
+
+    If check_path is True, the URL path prefix will be checked for a language
+    code, otherwise this is skipped for backwards compatibility.
     """
     global _accepted
     from django.conf import settings
     supported = dict(settings.LANGUAGES)
 
-    lang_code = get_language_from_path(request.path_info, supported)
-    if lang_code is not None:
-        return lang_code
+    if check_path:
+        lang_code = get_language_from_path(request.path_info, supported)
+        if lang_code is not None:
+            return lang_code
 
     if hasattr(request, 'session'):
         lang_code = request.session.get('django_language', None)
diff --git a/tests/regressiontests/i18n/patterns/tests.py b/tests/regressiontests/i18n/patterns/tests.py
index 40a62d4fb6..1cc4520358 100644
--- a/tests/regressiontests/i18n/patterns/tests.py
+++ b/tests/regressiontests/i18n/patterns/tests.py
@@ -78,6 +78,20 @@ class URLDisabledTests(URLTestCaseBase):
             self.assertEqual(reverse('prefixed'), '/prefixed/')
 
 
+class PathUnusedTests(URLTestCaseBase):
+    """
+    Check that if no i18n_patterns is used in root urlconfs, then no
+    language activation happens based on url prefix.
+    """
+    urls = 'regressiontests.i18n.patterns.urls.path_unused'
+
+    def test_no_lang_activate(self):
+        response = self.client.get('/nl/foo/')
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response['content-language'], 'en')
+        self.assertEqual(response.context['LANGUAGE_CODE'], 'en')
+
+
 class URLTranslationTests(URLTestCaseBase):
     """
     Tests if the pattern-strings are translated correctly (within the
diff --git a/tests/regressiontests/i18n/patterns/urls/path_unused.py b/tests/regressiontests/i18n/patterns/urls/path_unused.py
new file mode 100644
index 0000000000..1254d16c4e
--- /dev/null
+++ b/tests/regressiontests/i18n/patterns/urls/path_unused.py
@@ -0,0 +1,10 @@
+from django.conf.urls import url
+from django.conf.urls import patterns
+from django.views.generic import TemplateView
+
+
+view = TemplateView.as_view(template_name='dummy.html')
+
+urlpatterns = patterns('',
+    url(r'^nl/foo/', view, name='not-translated'),
+)
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
index 4b543d2bbf..99a55bd24d 100644
--- a/tests/regressiontests/i18n/tests.py
+++ b/tests/regressiontests/i18n/tests.py
@@ -39,7 +39,7 @@ from .models import Company, TestModel
 from .patterns.tests import (URLRedirectWithoutTrailingSlashTests,
     URLTranslationTests, URLDisabledTests, URLTagTests, URLTestCaseBase,
     URLRedirectWithoutTrailingSlashSettingTests, URLNamespaceTests,
-    URLPrefixTests, URLResponseTests, URLRedirectTests)
+    URLPrefixTests, URLResponseTests, URLRedirectTests, PathUnusedTests)
 from .test_warnings import DeprecationWarningTests