Sync gettextutils from oslo

Add type check for Message params when setting locale to avoid
AttributeError.

Fixes bug: #1229364

Change-Id: I6fe55ebc4eea5fef1d335ad83a84c4879f7f06f9
This commit is contained in:
Luis A. Garcia 2013-09-24 22:42:51 +00:00
parent d8394a0fc4
commit a8950c7111

View File

@ -60,6 +60,8 @@ def _(msg):
if USE_LAZY:
return Message(msg, 'neutron')
else:
if six.PY3:
return _t.gettext(msg)
return _t.ugettext(msg)
@ -105,10 +107,14 @@ def install(domain, lazy=False):
"""
return Message(msg, domain)
import __builtin__
__builtin__.__dict__['_'] = _lazy_gettext
from six import moves
moves.builtins.__dict__['_'] = _lazy_gettext
else:
localedir = '%s_LOCALEDIR' % domain.upper()
if six.PY3:
gettext.install(domain,
localedir=os.environ.get(localedir))
else:
gettext.install(domain,
localedir=os.environ.get(localedir),
unicode=True)
@ -142,8 +148,13 @@ class Message(_userString.UserString, object):
localedir=localedir,
fallback=True)
if six.PY3:
ugettext = lang.gettext
else:
ugettext = lang.ugettext
full_msg = (self._left_extra_msg +
lang.ugettext(self._msg) +
ugettext(self._msg) +
self._right_extra_msg)
if self.params is not None:
@ -173,6 +184,7 @@ class Message(_userString.UserString, object):
if isinstance(param, Message):
param.locale = value
return
if isinstance(self.params, dict):
for param in self.params.values():
if isinstance(param, Message):
param.locale = value
@ -195,7 +207,7 @@ class Message(_userString.UserString, object):
params[key] = copy.deepcopy(dict_param[key])
except TypeError:
# cast uncopyable thing to unicode string
params[key] = unicode(dict_param[key])
params[key] = six.text_type(dict_param[key])
return params
@ -214,7 +226,7 @@ class Message(_userString.UserString, object):
try:
self.params = copy.deepcopy(other)
except TypeError:
self.params = unicode(other)
self.params = six.text_type(other)
return self
@ -223,6 +235,8 @@ class Message(_userString.UserString, object):
return self.data
def __str__(self):
if six.PY3:
return self.__unicode__()
return self.data.encode('utf-8')
def __getstate__(self):
@ -319,7 +333,7 @@ def get_localized_message(message, user_locale):
if isinstance(message, Message):
if user_locale:
message.locale = user_locale
return unicode(message)
return six.text_type(message)
else:
return message