From a01d887a3ad9029ed198b6af974534fec46223f9 Mon Sep 17 00:00:00 2001 From: Krzysztof Urbaniak Date: Fri, 28 Oct 2016 11:36:53 +0200 Subject: [PATCH] Fixed #27063 -- Prevented i18n_patterns() from using too much of the URL as the language. --- django/utils/translation/trans_real.py | 2 +- docs/releases/1.10.3.txt | 3 +++ tests/i18n/tests.py | 7 +++++++ tests/i18n/urls_default_unprefixed.py | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 06331a502f..84a42490eb 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -45,7 +45,7 @@ language_code_re = re.compile( re.IGNORECASE ) -language_code_prefix_re = re.compile(r'^/([\w@-]+)(/|$)') +language_code_prefix_re = re.compile(r'^/(\w+([@-]\w+)?)(/|$)') @receiver(setting_changed) diff --git a/docs/releases/1.10.3.txt b/docs/releases/1.10.3.txt index 5bb340a3a8..ba01287aa5 100644 --- a/docs/releases/1.10.3.txt +++ b/docs/releases/1.10.3.txt @@ -23,3 +23,6 @@ Bugfixes * Fixed ``QuerySet.bulk_create()`` on PostgreSQL when the number of objects is a multiple plus one of ``batch_size`` (:ticket:`27385`). + +* Prevented ``i18n_patterns()`` from using too much of the URL as the language + to fix a use case for ``prefix_default_language=False`` (:ticket:`27063`). diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 1e49565a8c..daba0d34a7 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -1490,6 +1490,7 @@ class MiscTests(SimpleTestCase): self.assertEqual(g('/de/'), 'de') self.assertEqual(g('/de-at/'), 'de-at') self.assertEqual(g('/de-ch/'), 'de') + self.assertIsNone(g('/de-simple-page/')) def test_get_language_from_path_null(self): from django.utils.translation.trans_null import get_language_from_path as g @@ -1822,6 +1823,7 @@ class LocaleMiddlewareTests(TestCase): USE_I18N=True, LANGUAGES=[ ('en', 'English'), + ('de', 'German'), ('fr', 'French'), ], MIDDLEWARE=[ @@ -1852,6 +1854,11 @@ class UnprefixedDefaultLanguageTests(SimpleTestCase): with self.assertRaisesMessage(AssertionError, "Unexpected kwargs for i18n_patterns(): {'foo':"): i18n_patterns(object(), foo='bar') + def test_page_with_dash(self): + # A page starting with /de* shouldn't match the 'de' langauge code. + response = self.client.get('/de-simple-page/') + self.assertEqual(response.content, b'Yes') + @override_settings( USE_I18N=True, diff --git a/tests/i18n/urls_default_unprefixed.py b/tests/i18n/urls_default_unprefixed.py index 8cadbfaa73..92ef8c6246 100644 --- a/tests/i18n/urls_default_unprefixed.py +++ b/tests/i18n/urls_default_unprefixed.py @@ -4,6 +4,7 @@ from django.http import HttpResponse from django.utils.translation import ugettext_lazy as _ urlpatterns = i18n_patterns( + url(r'^(?P[\w-]+)-page', lambda request, **arg: HttpResponse(_("Yes"))), url(r'^simple/$', lambda r: HttpResponse(_("Yes"))), prefix_default_language=False, )