From 6bd6a568960e9ddebd78da8e008b235b3b76c5a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Ha=C5=82as?= Date: Sat, 23 Feb 2013 17:02:07 +0100 Subject: [PATCH] Fixed #19811 - Added language code fallback in get_language_info. --- django/utils/translation/__init__.py | 6 ++++++ tests/regressiontests/i18n/tests.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 6fd52b6023f..4645f142ceb 100644 --- a/django/utils/translation/__init__.py +++ b/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) diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index 3fcb60c3b78..9e79f35d744 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -956,6 +956,21 @@ class TestLanguageInfo(TestCase): 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): """