From c326ae2c2b23f51a9846c8e5a2a96837e800ce63 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Wed, 26 Jul 2017 05:25:09 +0200 Subject: [PATCH] [mod] clean code of try/cache mess --- moulinette/core.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/moulinette/core.py b/moulinette/core.py index 712dc0a2..cc833737 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -87,20 +87,15 @@ class Translator(object): - key -- The key to translate """ - def _load_key(locale): - value = self._translations[locale][key] - return value.encode('utf-8').format(*args, **kwargs) + if key in self._translations.get(self.locale, {}): + return self._translations[self.locale][key].encode('utf-8').format(*args, **kwargs) + + if 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 _load_key(self.locale) - except (KeyError, IndexError): - if self.default_locale != self.locale: - logger.info("untranslated key '%s' for locale '%s'", - key, self.locale) - try: - return _load_key(self.default_locale) - except: - pass logger.exception("unable to retrieve key '%s' for default locale '%s'", key, self.default_locale) return key