summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/translation/__init__.py5
-rw-r--r--django/utils/translation/trans_real.py21
2 files changed, 15 insertions, 11 deletions
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
index 6be664550d..14056d4759 100644
--- a/django/utils/translation/__init__.py
+++ b/django/utils/translation/__init__.py
@@ -212,7 +212,10 @@ string_concat = lazy(_string_concat, six.text_type)
def get_language_info(lang_code):
from django.conf.locale import LANG_INFO
try:
- return LANG_INFO[lang_code]
+ lang_info = LANG_INFO[lang_code]
+ if 'fallback' in lang_info and 'name' not in lang_info:
+ return get_language_info(lang_info['fallback'][0])
+ return lang_info
except KeyError:
if '-' not in lang_code:
raise KeyError("Unknown language code %s." % lang_code)
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
index 56a0da2639..5715d57853 100644
--- a/django/utils/translation/trans_real.py
+++ b/django/utils/translation/trans_real.py
@@ -11,6 +11,7 @@ import warnings
from django.apps import apps
from django.conf import settings
+from django.conf.locale import LANG_INFO
from django.core.exceptions import AppRegistryNotReady
from django.dispatch import receiver
from django.test.signals import setting_changed
@@ -22,7 +23,6 @@ from django.utils import six, lru_cache
from django.utils.six import StringIO
from django.utils.translation import TranslatorCommentWarning, trim_whitespace, LANGUAGE_SESSION_KEY
-
# Translations are cached in a dictionary for every language.
# The active translations are stored by threadid to make them thread local.
_translations = {}
@@ -51,13 +51,11 @@ language_code_re = re.compile(r'^[a-z]{1,8}(?:-[a-z0-9]{1,8})*$', re.IGNORECASE)
language_code_prefix_re = re.compile(r'^/([\w-]+)(/|$)')
# some browsers use deprecated locales. refs #18419
-_BROWSERS_DEPRECATED_LOCALES = {
+_DJANGO_DEPRECATED_LOCALES = {
'zh-cn': 'zh-hans',
'zh-tw': 'zh-hant',
}
-_DJANGO_DEPRECATED_LOCALES = _BROWSERS_DEPRECATED_LOCALES
-
@receiver(setting_changed)
def reset_cache(**kwargs):
@@ -429,13 +427,16 @@ def get_supported_language_variant(lang_code, strict=False):
if _supported is None:
_supported = OrderedDict(settings.LANGUAGES)
if lang_code:
- # some browsers use deprecated language codes -- #18419
- replacement = _BROWSERS_DEPRECATED_LOCALES.get(lang_code)
- if lang_code not in _supported and replacement in _supported:
- return replacement
- # if fr-ca is not supported, try fr.
+ # If 'fr-ca' is not supported, try special fallback or language-only 'fr'.
+ 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]
- for code in (lang_code, generic_lang_code):
+ possible_lang_codes.append(generic_lang_code)
+
+ for code in possible_lang_codes:
if code in _supported and check_for_language(code):
return code
if not strict: