]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Sync gettextutils from oslo
authorLuis A. Garcia <luis@linux.vnet.ibm.com>
Tue, 24 Sep 2013 22:17:05 +0000 (22:17 +0000)
committerLuis A. Garcia <luis@linux.vnet.ibm.com>
Tue, 24 Sep 2013 22:34:37 +0000 (22:34 +0000)
Add type check for Message params when setting locale to avoid
AttributeError.

Fixes bug: #1229364

Change-Id: I6fe55ebc4eea5fef1d335ad83a84c4879f7f06f9

cinder/openstack/common/gettextutils.py

index 9e05e6efb596bffbbaa1b898f7d89ad7e0ac79a8..1610322524cf1b011a0bd461e4e1fd9d6f61dfd7 100644 (file)
@@ -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