Fixed #18419 -- Full backwards compatibility for old language codes

Improved documentation about zh-* deprecation and upgrade path.

Thanks to Baptiste Mispelon for the code reviews.
This commit is contained in:
Bouke Haarsma 2013-11-05 10:54:23 +01:00 committed by Baptiste Mispelon
parent 76da053641
commit e5e044da87
4 changed files with 51 additions and 24 deletions

View File

@ -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,

View File

@ -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
---

View File

@ -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.

View File

@ -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.