From c326ae2c2b23f51a9846c8e5a2a96837e800ce63 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Wed, 26 Jul 2017 05:25:09 +0200 Subject: [PATCH 1/4] [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 From b8b3c8db9f5a145717c6b1c57243ed816048ccfb Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Wed, 26 Jul 2017 05:27:11 +0200 Subject: [PATCH 2/4] [mod] simplify, called code should never raised --- moulinette/core.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/moulinette/core.py b/moulinette/core.py index cc833737..e97f6f01 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -206,12 +206,7 @@ class Moulinette18n(object): - key -- The key to translate """ - try: - return self._namespace.translate(key, *args, **kwargs) - except: - logger.exception("cannot translate key '%s' for namespace '%s'", - key, self._current_namespace) - return key + return self._namespace.translate(key, *args, **kwargs) class MoulinetteSignals(object): From e2ff1dca39194edbb2edde86726df89b49f24549 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Wed, 26 Jul 2017 05:28:56 +0200 Subject: [PATCH 3/4] [mod] simplify code again --- moulinette/core.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/moulinette/core.py b/moulinette/core.py index e97f6f01..e3d8f395 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -150,11 +150,6 @@ class Moulinette18n(object): self._namespaces = {} self._current_namespace = None - @property - def _namespace(self): - """Return current namespace's Translator object""" - return self._namespaces[self._current_namespace] - def load_namespace(self, namespace): """Load the namespace to use @@ -206,7 +201,7 @@ class Moulinette18n(object): - key -- The key to translate """ - return self._namespace.translate(key, *args, **kwargs) + return self._namespaces[self._current_namespace].translate(key, *args, **kwargs) class MoulinetteSignals(object): From 7266e7de1543f07ae1abb59f18329851fd07b336 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Wed, 26 Jul 2017 05:29:35 +0200 Subject: [PATCH 4/4] [mod] rename variable --- moulinette/core.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/moulinette/core.py b/moulinette/core.py index e3d8f395..9a42a2a7 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -162,10 +162,10 @@ class Moulinette18n(object): """ if namespace not in self._namespaces: # Create new Translator object - n = Translator('%s/%s/locales' % (LIB_DIR, namespace), - self.default_locale) - n.set_locale(self.locale) - self._namespaces[namespace] = n + translator = Translator('%s/%s/locales' % (LIB_DIR, namespace), + self.default_locale) + translator.set_locale(self.locale) + self._namespaces[namespace] = translator # Set current namespace self._current_namespace = namespace