summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorMaxim Beder <macsim.beder@gmail.com>2021-04-30 00:40:06 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-05-05 09:37:54 +0200
commit06fd4df41afb5aa1d681b853c3c08d8c688ca3a5 (patch)
treeab9ee295772833a975639f3440b4569d029ab5ab /django/utils
parent96f55ccf798c7592a1203f798a4dffaf173a9263 (diff)
Fixed #32479 -- Added fallbacks to subsequent language codes in translations.
Thanks Claude Paroz and Nick Pope for reviews.
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/translation/trans_real.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
index 8042f6fdc4..173ad08639 100644
--- a/django/utils/translation/trans_real.py
+++ b/django/utils/translation/trans_real.py
@@ -474,14 +474,17 @@ def get_supported_language_variant(lang_code, strict=False):
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
if lang_code:
- # If 'fr-ca' is not supported, try special fallback or language-only 'fr'.
+ # If 'zh-hant-tw' is not supported, try special fallback or subsequent
+ # language codes i.e. 'zh-hant' and 'zh'.
possible_lang_codes = [lang_code]
try:
possible_lang_codes.extend(LANG_INFO[lang_code]['fallback'])
except KeyError:
pass
- generic_lang_code = lang_code.split('-')[0]
- possible_lang_codes.append(generic_lang_code)
+ i = None
+ while (i := lang_code.rfind('-', 0, i)) > -1:
+ possible_lang_codes.append(lang_code[:i])
+ generic_lang_code = possible_lang_codes[-1]
supported_lang_codes = get_languages()
for code in possible_lang_codes: