1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #33514 -- Added fallbacks to subsequent language codes in Select2 translations.

This commit is contained in:
My-Name-Is-Nabil 2022-02-18 03:21:35 +02:00 committed by Mariusz Felisiak
parent b2ed0d78f2
commit 3079133c73
2 changed files with 21 additions and 1 deletions

View File

@ -450,6 +450,19 @@ SELECT2_TRANSLATIONS = {
SELECT2_TRANSLATIONS.update({"zh-hans": "zh-CN", "zh-hant": "zh-TW"})
def get_select2_language():
lang_code = get_language()
supported_code = SELECT2_TRANSLATIONS.get(lang_code)
if supported_code is None:
# If 'zh-hant-tw' is not supported, try subsequent language codes i.e.
# 'zh-hant' and 'zh'.
i = None
while (i := lang_code.rfind("-", 0, i)) > -1:
if supported_code := SELECT2_TRANSLATIONS.get(lang_code[:i]):
return supported_code
return supported_code
class AutocompleteMixin:
"""
Select widget mixin that loads options from AutocompleteJsonView via AJAX.
@ -466,7 +479,7 @@ class AutocompleteMixin:
self.db = using
self.choices = choices
self.attrs = {} if attrs is None else attrs.copy()
self.i18n_name = SELECT2_TRANSLATIONS.get(get_language())
self.i18n_name = get_select2_language()
def get_url(self):
return reverse(self.url_name % self.admin_site.name)

View File

@ -166,6 +166,13 @@ class AutocompleteMixinTests(TestCase):
)
languages = (
("de", "de"),
# Subsequent language codes are used when the language code is not
# supported.
("de-at", "de"),
("de-ch-1901", "de"),
("en-latn-us", "en"),
("nl-nl-x-informal", "nl"),
("zh-hans-HK", "zh-CN"),
# Language with code 00 does not exist.
("00", None),
# Language files are case sensitive.