Skip to content

Commit

Permalink
Merge pull request #6111 from escattone/zh-hant-fallback-fix-1820
Browse files Browse the repository at this point in the history
fix Traditional Chinese mappings
  • Loading branch information
escattone authored Jul 9, 2024
2 parents fc12b2b + 11d5687 commit 2a4c7d1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions kitsune/sumo/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
FALLBACK_LANGUAGE_URL_MAP = dict(
(k.lower(), v if v else "en-US") for k, v in settings.NON_SUPPORTED_LOCALES.items()
)
MAP_TO_TRADITIONAL_CHINESE = frozenset(("zh-hant", "zh-hk", "zh-mo"))


class LocalePrefixPattern(django.urls.LocalePrefixPattern):
Expand Down Expand Up @@ -101,6 +102,14 @@ def normalize_path(path, force_language=False):
if force_language:
return f"/{force_language}{remaining_path}"
elif actual_language_from_path != normalized_language_from_path:
if (normalized_language_from_path == "zh-CN") and (
actual_language_from_path.lower() in MAP_TO_TRADITIONAL_CHINESE
):
# Django will always map any unsupported Traditional Chinese locale, like
# "zh-hant" or "zh-hk" for example, into Simplified Chinese ("zh-cn") because
# we're not using "strict" mode, and "zh-cn" is always the first "zh-" match.
# For these specific cases we should be mapping to "zh-tw" instead.
normalized_language_from_path = "zh-TW"
return f"/{normalized_language_from_path}{remaining_path}"
elif force_language:
return f"/{force_language}{path}"
Expand Down
21 changes: 21 additions & 0 deletions kitsune/sumo/tests/test_locale_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ def test_upper_accept_lang(self):
response = self.client.get("/search/", HTTP_ACCEPT_LANGUAGE="en-US,fr;q=0.3")
self.assertRedirects(response, "/en-US/search/")

def test_traditional_chinese_mappings(self):
"""'zh-hant' or 'zh-hk' should be mapped to 'zh-TW' instead of 'zh-CN'"""
with self.subTest("zh-hant"):
response = self.client.get("/zh-hant/search/")
self.assertRedirects(response, "/zh-TW/search/")
with self.subTest("zh-Hant"):
response = self.client.get("/zh-Hant/search/")
self.assertRedirects(response, "/zh-TW/search/")
with self.subTest("zh-hk"):
response = self.client.get("/zh-hk/")
self.assertRedirects(response, "/zh-TW/")
with self.subTest("zh-HK"):
response = self.client.get("/zh-HK/")
self.assertRedirects(response, "/zh-TW/")
with self.subTest("zh-mo"):
response = self.client.get("/zh-mo/")
self.assertRedirects(response, "/zh-TW/")
with self.subTest("zh-MO"):
response = self.client.get("/zh-MO/")
self.assertRedirects(response, "/zh-TW/")


class PreferredLanguageTests(TestCase):
def test_anonymous_change_language(self):
Expand Down

0 comments on commit 2a4c7d1

Please sign in to comment.