From 5daf209c0c8df4df7d443a41b49af3729f7e4147 Mon Sep 17 00:00:00 2001 From: "Luis A. Garcia" Date: Tue, 24 Sep 2013 22:17:05 +0000 Subject: [PATCH] Sync gettextutils from oslo Add type check for Message params when setting locale to avoid AttributeError. Fixes bug: #1229364 Change-Id: I6fe55ebc4eea5fef1d335ad83a84c4879f7f06f9 --- cinder/openstack/common/gettextutils.py | 38 +++++++++++++++++-------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/cinder/openstack/common/gettextutils.py b/cinder/openstack/common/gettextutils.py index 9e05e6efb..161032252 100644 --- a/cinder/openstack/common/gettextutils.py +++ b/cinder/openstack/common/gettextutils.py @@ -60,6 +60,8 @@ def _(msg): if USE_LAZY: return Message(msg, 'cinder') else: + if six.PY3: + return _t.gettext(msg) return _t.ugettext(msg) @@ -105,13 +107,17 @@ 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() - gettext.install(domain, - localedir=os.environ.get(localedir), - unicode=True) + if six.PY3: + gettext.install(domain, + localedir=os.environ.get(localedir)) + else: + gettext.install(domain, + localedir=os.environ.get(localedir), + unicode=True) class Message(_userString.UserString, object): @@ -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,9 +184,10 @@ class Message(_userString.UserString, object): if isinstance(param, Message): param.locale = value return - for param in self.params.values(): - if isinstance(param, Message): - param.locale = value + if isinstance(self.params, dict): + for param in self.params.values(): + if isinstance(param, Message): + param.locale = value def _save_dictionary_parameter(self, dict_param): full_msg = self.data @@ -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 -- 2.45.2