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

Fixes bug: #1229364

Change-Id: I6fe55ebc4eea5fef1d335ad83a84c4879f7f06f9

neutron/openstack/common/gettextutils.py

index ce0abe19a6792de2ecaf62b2107eb174b664256a..bc1afd17ac21e159ba10a0b68ebf4594f113f76a 100644 (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,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