Fixed #17720 -- Stopped the LocaleMiddleware from overeagerly using the request path for language activation if it's actually not wanted. Thanks to Anssi Kääriäinen for the initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17547 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2012-02-18 13:37:30 +00:00
parent 52b06e29c7
commit 7dd0ceba2e
7 changed files with 39 additions and 9 deletions

View File

@ -15,7 +15,9 @@ class LocaleMiddleware(object):
""" """
def process_request(self, request): 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) translation.activate(language)
request.LANGUAGE_CODE = translation.get_language() request.LANGUAGE_CODE = translation.get_language()

View File

@ -144,8 +144,8 @@ def check_for_language(lang_code):
def to_locale(language): def to_locale(language):
return _trans.to_locale(language) return _trans.to_locale(language)
def get_language_from_request(request): def get_language_from_request(request, check_path=False):
return _trans.get_language_from_request(request) return _trans.get_language_from_request(request, check_path)
def get_language_from_path(path): def get_language_from_path(path):
return _trans.get_language_from_path(path) return _trans.get_language_from_path(path)

View File

@ -55,7 +55,7 @@ def to_locale(language):
else: else:
return language.lower() return language.lower()
def get_language_from_request(request): def get_language_from_request(request, check_path=False):
return settings.LANGUAGE_CODE return settings.LANGUAGE_CODE
def get_language_from_path(request): def get_language_from_path(request):

View File

@ -363,20 +363,24 @@ def get_language_from_path(path, supported=None):
if lang_code in supported and check_for_language(lang_code): if lang_code in supported and check_for_language(lang_code):
return 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 Analyzes the request to find what language the user wants the system to
show. Only languages listed in settings.LANGUAGES are taken into account. 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 If the user requests a sublanguage where we have a main language, we send
out the main language. 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 global _accepted
from django.conf import settings from django.conf import settings
supported = dict(settings.LANGUAGES) supported = dict(settings.LANGUAGES)
lang_code = get_language_from_path(request.path_info, supported) if check_path:
if lang_code is not None: lang_code = get_language_from_path(request.path_info, supported)
return lang_code if lang_code is not None:
return lang_code
if hasattr(request, 'session'): if hasattr(request, 'session'):
lang_code = request.session.get('django_language', None) lang_code = request.session.get('django_language', None)

View File

@ -78,6 +78,20 @@ class URLDisabledTests(URLTestCaseBase):
self.assertEqual(reverse('prefixed'), '/prefixed/') 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): class URLTranslationTests(URLTestCaseBase):
""" """
Tests if the pattern-strings are translated correctly (within the Tests if the pattern-strings are translated correctly (within the

View File

@ -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'),
)

View File

@ -39,7 +39,7 @@ from .models import Company, TestModel
from .patterns.tests import (URLRedirectWithoutTrailingSlashTests, from .patterns.tests import (URLRedirectWithoutTrailingSlashTests,
URLTranslationTests, URLDisabledTests, URLTagTests, URLTestCaseBase, URLTranslationTests, URLDisabledTests, URLTagTests, URLTestCaseBase,
URLRedirectWithoutTrailingSlashSettingTests, URLNamespaceTests, URLRedirectWithoutTrailingSlashSettingTests, URLNamespaceTests,
URLPrefixTests, URLResponseTests, URLRedirectTests) URLPrefixTests, URLResponseTests, URLRedirectTests, PathUnusedTests)
from .test_warnings import DeprecationWarningTests from .test_warnings import DeprecationWarningTests