From facc153af7dc96ad83f1bfb86c92d41ec97aa14b Mon Sep 17 00:00:00 2001 From: sarahboyce Date: Thu, 6 Apr 2023 19:40:14 +0200 Subject: [PATCH] [4.2.x] Fixed #34455 -- Restored i18n_patterns() respect of prefix_default_language argument when fallback language is used. Regression in 94e7f471c4edef845a4fe5e3160132997b4cca81. Thanks Oussama Jarrousse for the report. Backport of 3b4728310a7a64f8fcc548163b0aa5f98a5c78f5 from main --- django/urls/resolvers.py | 5 +++-- django/utils/translation/__init__.py | 1 + docs/releases/4.2.1.txt | 4 ++++ tests/i18n/tests.py | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/django/urls/resolvers.py b/django/urls/resolvers.py index 9a22b0d87d8..e5c6dfdea71 100644 --- a/django/urls/resolvers.py +++ b/django/urls/resolvers.py @@ -23,7 +23,7 @@ from django.utils.datastructures import MultiValueDict from django.utils.functional import cached_property from django.utils.http import RFC3986_SUBDELIMS, escape_leading_slashes from django.utils.regex_helper import _lazy_re_compile, normalize -from django.utils.translation import get_language +from django.utils.translation import get_language, get_supported_language_variant from .converters import get_converter from .exceptions import NoReverseMatch, Resolver404 @@ -351,7 +351,8 @@ class LocalePrefixPattern: @property def language_prefix(self): language_code = get_language() or settings.LANGUAGE_CODE - if language_code == settings.LANGUAGE_CODE and not self.prefix_default_language: + default_language = get_supported_language_variant(settings.LANGUAGE_CODE) + if language_code == default_language and not self.prefix_default_language: return "" else: return "%s/" % language_code diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 0b3f78e4866..69820a2fc45 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -17,6 +17,7 @@ __all__ = [ "get_language_from_request", "get_language_info", "get_language_bidi", + "get_supported_language_variant", "check_for_language", "to_language", "to_locale", diff --git a/docs/releases/4.2.1.txt b/docs/releases/4.2.1.txt index 1a5fda3d4bb..525830a01bd 100644 --- a/docs/releases/4.2.1.txt +++ b/docs/releases/4.2.1.txt @@ -25,3 +25,7 @@ Bugfixes * Enforced UTF-8 client encoding on PostgreSQL, following a regression in Django 4.2 (:ticket:`34470`). + +* Fixed a regression in Django 4.2 where ``i18n_patterns()`` didn't respect the + ``prefix_default_language`` argument when a fallback language of the default + language was used (:ticket:`34455`). diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 43d1682c960..9ccbc25ca55 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -2132,6 +2132,12 @@ class UnprefixedDefaultLanguageTests(SimpleTestCase): response = self.client.get("/simple/") self.assertEqual(response.content, b"Yes") + @override_settings(LANGUAGE_CODE="en-us") + def test_default_lang_fallback_without_prefix(self): + response = self.client.get("/simple/") + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, b"Yes") + def test_other_lang_with_prefix(self): response = self.client.get("/fr/simple/") self.assertEqual(response.content, b"Oui")