Skip to content

Commit

Permalink
Fixed #18419 -- Full backwards compatibility for old language codes
Browse files Browse the repository at this point in the history
Improved documentation about zh-* deprecation and upgrade path.

Thanks to Baptiste Mispelon for the code reviews.
  • Loading branch information
Bouke authored and bmispelon committed Nov 5, 2013
1 parent 76da053 commit e5e044d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
32 changes: 15 additions & 17 deletions django/utils/translation/trans_real.py
Expand Up @@ -48,6 +48,12 @@

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):
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions docs/internals/deprecation.txt
Expand Up @@ -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
---
Expand Down
12 changes: 7 additions & 5 deletions docs/releases/1.7.txt
Expand Up @@ -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.
27 changes: 27 additions & 0 deletions tests/i18n/tests.py
Expand Up @@ -893,9 +893,36 @@ def test_support_for_deprecated_chinese_language_codes(self):
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.
Expand Down

0 comments on commit e5e044d

Please sign in to comment.