[enh] Manage several namespaces in Moulinette18n

This commit is contained in:
Jérôme Lebleu 2014-11-17 15:30:55 +01:00
parent cb91f102d4
commit d840d11427

View file

@ -249,9 +249,17 @@ class Moulinette18n(object):
self.locale = default_locale self.locale = default_locale
self.pkg = package self.pkg = package
# Init translators # Init global translator
self._global = Translator(self.pkg.localedir, default_locale) self._global = Translator(self.pkg.localedir, default_locale)
self._namespace = None
# Define namespace related variables
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): def load_namespace(self, namespace):
"""Load the namespace to use """Load the namespace to use
@ -263,20 +271,23 @@ class Moulinette18n(object):
- namespace -- The namespace to load - namespace -- The namespace to load
""" """
if self._namespace and self._namespace[0] == namespace: if namespace not in self._namespaces:
return # Create new Translator object
n = Translator('%s/%s/locales' % (self.pkg.libdir, namespace),
self.default_locale)
n.set_locale(self.locale)
self._namespaces[namespace] = n
self._namespace = (namespace, Translator('%s/%s/locales' # Set current namespace
% (self.pkg.libdir, namespace), self.default_locale)) self._current_namespace = namespace
self._namespace[1].set_locale(self.locale)
def set_locale(self, locale): def set_locale(self, locale):
"""Set the locale to use""" """Set the locale to use"""
self.locale = locale self.locale = locale
self._global.set_locale(locale) self._global.set_locale(locale)
if self._namespace: for n in self._namespaces.values():
self._namespace[1].set_locale(locale) n.set_locale(locale)
def g(self, key, *args, **kwargs): def g(self, key, *args, **kwargs):
"""Retrieve proper translation for a moulinette key """Retrieve proper translation for a moulinette key
@ -293,18 +304,20 @@ class Moulinette18n(object):
def n(self, key, *args, **kwargs): def n(self, key, *args, **kwargs):
"""Retrieve proper translation for a moulinette key """Retrieve proper translation for a moulinette key
Attempt to retrieve value for a key from loaded namespace translations Attempt to retrieve value for a key from current loaded namespace
using the current locale or the default locale if 'key' is not found. translations using the current locale or the default one if 'key' is
not found.
Keyword arguments: Keyword arguments:
- key -- The key to translate - key -- The key to translate
""" """
if not self._namespace: try:
logger.error("unable to retrieve translation for key '%s' without " \ return self._namespace.translate(key, *args, **kwargs)
"loaded namespace", key) except:
logger.exception("cannot translate key '%s' for namespace '%s'",
key, self._current_namespace)
return key return key
return self._namespace[1].translate(key, *args, **kwargs)
class MoulinetteSignals(object): class MoulinetteSignals(object):