Merge pull request #168 from YunoHost/dont_crash_on_failed_to_format_translation_string

Dont crash on failed to format translation string
This commit is contained in:
Bram 2018-08-14 11:16:39 +02:00 committed by GitHub
commit 315168dd79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -88,14 +88,23 @@ class Translator(object):
- key -- The key to translate
"""
failed_to_format = False
if key in self._translations.get(self.locale, {}):
return self._translations[self.locale][key].encode('utf-8').format(*args, **kwargs)
try:
return self._translations[self.locale][key].encode('utf-8').format(*args, **kwargs)
except KeyError as e:
logger.exception("Failed to format translated string '%s' with error: %s" % (key, e))
failed_to_format = True
if self.default_locale != self.locale and key in self._translations.get(self.default_locale, {}):
if failed_to_format or (self.default_locale != self.locale and key in self._translations.get(self.default_locale, {})):
logger.info("untranslated key '%s' for locale '%s'",
key, self.locale)
return self._translations[self.default_locale][key].encode('utf-8').format(*args, **kwargs)
try:
return self._translations[self.default_locale][key].encode('utf-8').format(*args, **kwargs)
except KeyError as e:
logger.exception("Failed to format translatable string '%s' with error: %s" % (key, e))
return self._translations[self.locale][key].encode('utf-8')
logger.exception("unable to retrieve key '%s' for default locale '%s'",
key, self.default_locale)