Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed #19811 - Added language code fallback in get_language_info #777

Merged
merged 1 commit into from

3 participants

@HonzaKral HonzaKral merged commit 0ad7684 into django:master
@claudep

This test will surely fail on Python 3 (no u prefix). Either use the str_prefix utility in the test, or simply don't use the %r placeholder in the exception.

If you want I can remove %r from exception message.

@konradhalas

Fixed in #791 and bfd4a71.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 django/utils/translation/__init__.py
@@ -187,4 +187,10 @@ def get_language_info(lang_code):
try:
return LANG_INFO[lang_code]
except KeyError:
+ if '-' in lang_code:
+ splited_lang_code = lang_code.split('-')[0]
+ try:
+ return LANG_INFO[splited_lang_code]
+ except KeyError:
+ raise KeyError("Unknown language code %r and %r." % (lang_code, splited_lang_code))
raise KeyError("Unknown language code %r." % lang_code)
View
15 tests/regressiontests/i18n/tests.py
@@ -956,6 +956,21 @@ def test_localized_language_info(self):
self.assertEqual(li['name'], 'German')
self.assertEqual(li['bidi'], False)
+ def test_unknown_language_code(self):
+ with self.assertRaisesRegexp(KeyError, "Unknown language code u\'xx\'."):
+ get_language_info('xx')
+
+ def test_unknown_only_country_code(self):
+ li = get_language_info('de-xx')
+ self.assertEqual(li['code'], 'de')
+ self.assertEqual(li['name_local'], 'Deutsch')
+ self.assertEqual(li['name'], 'German')
+ self.assertEqual(li['bidi'], False)
+
+ def test_unknown_language_code_and_country_code(self):
+ with self.assertRaisesRegexp(KeyError, "Unknown language code u\'xx-xx\' and u\'xx\'."):
+ get_language_info('xx-xx')
+
class MultipleLocaleActivationTests(TestCase):
"""
Something went wrong with that request. Please try again.