diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 78facc0aad..bd517f36ce 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -48,6 +48,12 @@ accept_language_re = re.compile(r''' language_code_prefix_re = re.compile(r'^/([\w-]+)(/|$)') +# some browsers use deprecated locales. refs #18419 +_DEPRECATED_LOCALES = { + 'zh-cn': 'zh-hans', + 'zh-tw': 'zh-hant', +} + @receiver(setting_changed) def reset_cache(**kwargs): @@ -202,17 +208,11 @@ def activate(language): language and installs it as the current translation object for the current thread. """ - if isinstance(language, six.string_types): - if language == 'zh-cn': - warnings.warn( - "The use of the language code 'zh-cn' is deprecated. " - "Please use the 'zh-hans' translation instead.", - PendingDeprecationWarning, stacklevel=2) - elif language == 'zh-tw': - warnings.warn( - "The use of the language code 'zh-tw' is deprecated. " - "Please use the 'zh-hant' translation instead.", - PendingDeprecationWarning, stacklevel=2) + if language in _DEPRECATED_LOCALES: + msg = ("The use of the language code %r is deprecated. " + "Please use the %r translation instead.") + warnings.warn(msg % (language, _DEPRECATED_LOCALES[language]), + PendingDeprecationWarning, stacklevel=2) _active.value = translation(language) @@ -410,16 +410,14 @@ def get_supported_language_variant(lang_code, supported=None, strict=False): If `strict` is False (the default), the function will look for an alternative country-specific variant when the currently checked is not found. """ - # some browsers use deprecated language codes -- #18419 - if lang_code == 'zh-cn' and 'zh-hans' in supported: - return 'zh-hans' - elif lang_code == 'zh-tw' and 'zh-hant' in supported: - return 'zh-hant' - if supported is None: from django.conf import settings supported = OrderedDict(settings.LANGUAGES) if lang_code: + # some browsers use deprecated language codes -- #18419 + if (lang_code not in supported and lang_code in _DEPRECATED_LOCALES and + _DEPRECATED_LOCALES[lang_code] in supported): + return _DEPRECATED_LOCALES[lang_code] # if fr-CA is not supported, try fr-ca; if that fails, fallback to fr. generic_lang_code = lang_code.split('-')[0] variants = (lang_code, lang_code.lower(), generic_lang_code, diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index c63724f4bb..eb7df979de 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -476,8 +476,8 @@ these changes. * The class ``django.utils.datastructures.MergeDict`` will be removed. -* The ``zh_CN`` and ``zh_TW`` language codes will be removed and have been - replaced by the ``zh_Hans`` and ``zh_Hant`` language code respectively. +* The ``zh-cn`` and ``zh-tw`` language codes will be removed and have been + replaced by the ``zh-hans`` and ``zh-hant`` language code respectively. 2.0 --- diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index 1bf93468ef..85e01cbe59 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -707,10 +707,12 @@ arguments into a ``REQUEST`` property on ``WSGIRequest``. To merge dictionaries, use ``dict.update()`` instead. The class ``MergeDict`` is deprecated and will be removed in Django 1.9. -Language codes ``zh_CN`` and ``zh_TW`` +Language codes ``zh-cn`` and ``zh-tw`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The currently used language codes for Simplified Chinese ``zh_CN`` and -Traditional Chinese ``zh_TW`` are deprecated and should be replaced by the -recently introduced language codes ``zh_Hans`` and ``zh_Hant`` respectively. -The deprecated language codes will be removed in Django 1.9. +The currently used language codes for Simplified Chinese ``zh-cn`` and +Traditional Chinese ``zh-tw`` are deprecated and should be replaced by the +recently introduced language codes ``zh-hans`` and ``zh-hant`` respectively. +If you use these language codes, you should rename the locale directories +and update your settings to reflect these changes. The deprecated language +codes will be removed in Django 1.9. diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index f68a7361f6..42e743dedf 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -893,9 +893,36 @@ class MiscTests(TransRealMixin, TestCase): r.COOKIES = {} r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,en'} self.assertEqual(g(r), 'zh-hans') + r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-tw,en'} self.assertEqual(g(r), 'zh-hant') + @override_settings( + LANGUAGES=( + ('en', 'English'), + ('zh-cn', 'Simplified Chinese'), + ('zh-hans', 'Simplified Chinese'), + ('zh-hant', 'Traditional Chinese'), + ('zh-tw', 'Traditional Chinese'), + ) + ) + def test_backwards_compatibility(self): + """ + While the old chinese language codes are being deprecated, they should + still work as before the new language codes were introduced. + + refs #18419 -- this is explicitly for backwards compatibility and + should be removed in Django 1.9 + """ + g = get_language_from_request + r = self.rf.get('/') + r.COOKIES = {} + r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,en'} + self.assertEqual(g(r), 'zh-cn') + + r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-tw,en'} + self.assertEqual(g(r), 'zh-tw') + def test_parse_language_cookie(self): """ Now test that we parse language preferences stored in a cookie correctly.