From 90bd495096b65bff6d9c7062dfda17a01f673ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Fri, 30 May 2014 18:52:13 +0200 Subject: [PATCH] [enh] Manage arguments in translation - and try to fix encoding (again) --- locales/en.json | 6 +++--- locales/fr.json | 6 +++--- moulinette/__init__.py | 2 +- moulinette/authenticators/ldap.py | 4 ++-- moulinette/core.py | 12 ++++++------ moulinette/interfaces/api.py | 3 ++- moulinette/interfaces/cli.py | 12 +++++++----- 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/locales/en.json b/locales/en.json index 28f3ae16..9e06b5be 100644 --- a/locales/en.json +++ b/locales/en.json @@ -1,5 +1,5 @@ { - "colon" : "%s: ", + "colon" : "{}: ", "success" : "Success!", "warning" : "Warning:", @@ -12,7 +12,7 @@ "unable_retrieve_session" : "Unable to retrieve the session", "ldap_server_down" : "Unable to reach LDAP server", "ldap_operation_error" : "An error occured during LDAP operation", - "ldap_attribute_already_exists" : "Attribute already exists: '%s=%s'", + "ldap_attribute_already_exists" : "Attribute already exists: '{:s}={:s}'", "password" : "Password", "invalid_password" : "Invalid password", @@ -20,7 +20,7 @@ "values_mismatch" : "Values don't match", "authentication_required_long" : "Authentication is required to perform this action", "authentication_required" : "Authentication required", - "authentication_profile_required" : "Authentication to profile '%s' required", + "authentication_profile_required" : "Authentication to profile '{:s}' required", "operation_interrupted" : "Operation interrupted", "logged_in" : "Logged in", diff --git a/locales/fr.json b/locales/fr.json index c4319bf6..01d41dbb 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -1,5 +1,5 @@ { - "colon" : "%s : ", + "colon" : "{} : ", "success" : "Succès !", "warning" : "Attention :", @@ -12,7 +12,7 @@ "unable_retrieve_session" : "Impossible de récupérer la session", "ldap_server_down" : "Impossible d'atteindre le serveur LDAP", "ldap_operation_error" : "Une erreur est survenue lors de l'opération LDAP", - "ldap_attribute_already_exists" : "L'attribut existe déjà : '%s=%s'", + "ldap_attribute_already_exists" : "L'attribut existe déjà : '{:s}={:s}'", "password" : "Mot de passe", "invalid_password" : "Mot de passe incorrect", @@ -20,7 +20,7 @@ "values_mismatch" : "Les valeurs ne correspondent pas", "authentication_required_long" : "L'authentification est requise pour exécuter cette action", "authentication_required" : "Authentification requise", - "authentication_profile_required" : "Authentification au profile '%s' requise", + "authentication_profile_required" : "Authentification au profile '{:s}' requise", "operation_interrupted" : "Opération interrompue", "logged_in" : "Connecté", diff --git a/moulinette/__init__.py b/moulinette/__init__.py index f6e7b6d7..88f17212 100755 --- a/moulinette/__init__.py +++ b/moulinette/__init__.py @@ -109,6 +109,6 @@ def cli(namespaces, args, print_json=False, use_cache=True): 'use_cache': use_cache}) moulinette.run(args, print_json) except MoulinetteError as e: - print(u'%s %s' % (colorize(m18n.g('error'), 'red'), e.strerror)) + print('%s %s' % (colorize(m18n.g('error'), 'red'), e.strerror)) return e.errno return 0 diff --git a/moulinette/authenticators/ldap.py b/moulinette/authenticators/ldap.py index e9ef2271..4f53a528 100644 --- a/moulinette/authenticators/ldap.py +++ b/moulinette/authenticators/ldap.py @@ -192,6 +192,6 @@ class Authenticator(BaseAuthenticator): continue else: raise MoulinetteError(errno.EEXIST, - m18n.g('ldap_attribute_already_exists') - % (attr, value)) + m18n.g('ldap_attribute_already_exists', + attr, value)) return True diff --git a/moulinette/core.py b/moulinette/core.py index 29bf92d3..92f01dfa 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -170,7 +170,7 @@ class Translator(object): self.locale = locale return True - def translate(self, key): + def translate(self, key, *args, **kwargs): """Retrieve proper translation for a key Attempt to retrieve translation for a key using the current locale @@ -191,7 +191,7 @@ class Translator(object): logging.warning("unknown key '%s' for locale '%s'" % (key, self.default_locale)) return key - return value + return value.format(*args, **kwargs).encode('utf-8') def _load_translations(self, locale, overwrite=False): """Load translations for a locale @@ -265,7 +265,7 @@ class Moulinette18n(object): if self._namespace: self._namespace[1].set_locale(locale) - def g(self, key): + def g(self, key, *args, **kwargs): """Retrieve proper translation for a moulinette key Attempt to retrieve value for a key from moulinette translations @@ -275,9 +275,9 @@ class Moulinette18n(object): - key -- The key to translate """ - return self._global.translate(key) + return self._global.translate(key, *args, **kwargs) - def n(self, key): + def n(self, key, *args, **kwargs): """Retrieve proper translation for a moulinette key Attempt to retrieve value for a key from loaded namespace translations @@ -289,7 +289,7 @@ class Moulinette18n(object): """ if not self._namespace: raise RuntimeError("No namespace loaded for translation") - return self._namespace[1].translate(key) + return self._namespace[1].translate(key, *args, **kwargs) class MoulinetteSignals(object): diff --git a/moulinette/interfaces/api.py b/moulinette/interfaces/api.py index 5f987690..077ee1a3 100644 --- a/moulinette/interfaces/api.py +++ b/moulinette/interfaces/api.py @@ -342,7 +342,8 @@ class _ActionsMapPlugin(object): if authenticator.name == 'default': msg = m18n.g('authentication_required') else: - msg = m18n.g('authentication_profile_required') % authenticator.name + msg = m18n.g('authentication_profile_required', + authenticator.name) raise HTTPUnauthorizedResponse(msg) else: return authenticator(token=(s_id, s_hash)) diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 84902eaf..8a014b86 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -35,7 +35,7 @@ def colorize(astr, color): """ if os.isatty(1): - return u'\033[{:d}m\033[1m{:s}\033[m'.format(colors_codes[color], astr) + return '\033[{:d}m\033[1m{:s}\033[m'.format(colors_codes[color], astr) else: return astr @@ -230,11 +230,11 @@ class Interface(BaseInterface): """ if is_password: def prompt(m): - print(colorize(m18n.g('colon') % m, 'blue'), end='') + print(colorize(m18n.g('colon', m), 'blue'), end='') return getpass.getpass() else: def prompt(m): - print(colorize(m18n.g('colon') % m, 'blue'), end='') + print(colorize(m18n.g('colon', m), 'blue'), end='') return raw_input() value = prompt(message) @@ -250,9 +250,11 @@ class Interface(BaseInterface): Handle the core.MoulinetteSignals.display signal. """ + if isinstance(message, unicode): + message = message.encode('utf-8') if style == 'success': - print(u'%s %s' % (colorize(m18n.g('success'), 'green'), message)) + print('{} {}'.format(colorize(m18n.g('success'), 'green'), message)) elif style == 'warning': - print(u'%s %s' % (colorize(m18n.g('warning'), 'yellow'), message)) + print('{} {}'.format(colorize(m18n.g('warning'), 'yellow'), message)) else: print(message)