[1.8.x] Fixed #26046 -- Fixed a crash with translations and Django-unknown language code

Thanks Jens Lundstrom for the report and Tim Graham for the review.
Backport of 632a9f21bc from master.
This commit is contained in:
Claude Paroz 2016-01-06 18:33:29 +01:00
parent 7688089e0f
commit 61437dd0a0
3 changed files with 10 additions and 0 deletions

View File

@ -121,6 +121,9 @@ class DjangoTranslation(gettext_module.GNUTranslations):
# default lang should have at least one translation file available.
raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)
self._add_fallback()
if self._catalog is None:
# No catalogs found for this language, set an empty catalog.
self._catalog = {}
def __repr__(self):
return "<DjangoTranslation lang:%s>" % self.__language

View File

@ -11,3 +11,6 @@ Bugfixes
* Fixed a regression that caused the "user-tools" items to display on the
admin's logout page (:ticket:`26035`).
* Fixed a crash in the translations system when the current language has no
translations (:ticket:`26046`).

View File

@ -1273,6 +1273,10 @@ class TestLanguageInfo(TestCase):
def test_unknown_language_code(self):
six.assertRaisesRegex(self, KeyError, r"Unknown language code xx\.", get_language_info, 'xx')
with translation.override('xx'):
# A language with no translation catalogs should fallback to the
# untranslated string.
self.assertEqual(ugettext("Title"), "Title")
def test_unknown_only_country_code(self):
li = get_language_info('de-xx')