[enh] Manage arguments in translation - and try to fix encoding (again)

This commit is contained in:
Jérôme Lebleu 2014-05-30 18:52:13 +02:00
parent dbc2776b94
commit 90bd495096
7 changed files with 24 additions and 21 deletions

View file

@ -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",

View file

@ -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é",

View file

@ -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

View file

@ -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

View file

@ -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):

View file

@ -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))

View file

@ -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)