]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Sync gettextutils.py from oslo-incubator
authorJay S. Bryant <jsbryant@us.ibm.com>
Mon, 7 Jul 2014 20:21:58 +0000 (15:21 -0500)
committerJay S. Bryant <jsbryant@us.ibm.com>
Wed, 9 Jul 2014 20:56:26 +0000 (15:56 -0500)
This pulls in the latest gettextutils.py from oslo-incubator.
The primary reason for this sync is to get commit
3d90045d2d1ce1df89f75937f415e3982e111063 which backports a
change from the new i18n library to check lazy enablement
at runtime.  Adding this change makes it possible to remove
the global enablement of lazy translation through
gettextutils.install().  Instead we will use gettextutils.enable_lazy()
with explicit imports of _() in each file where _() is used.

The change to remove gettextutils.install() and to add the
explicit _() imports will come in a dependent patch.

Current HEAD in OSLO:
--------------------
Merge: 3d90045d2d1ce1df89f75937f415e3982e111063
Date: Mon, 07 Jul 2014 17:55:18 +0000
Merge "Backport code for i18n to check lazy at runtime"
--------------------

Additional changes being merged (newest to oldest):
--------------------
de4adbc4 - pep8: fixed multiple violations
9912e5df - Add API for creating translation functions
6cc96d05 - Fix test_gettextutils on Python 3
--------------------

related - blueprint: i18n-enablement
Change-Id: I4706b89b0b64c5816a1a022b6cd1dfa01b29b5dc

bin/cinder-all
bin/cinder-api
cinder/db/sqlalchemy/migrate_repo/manage.py
cinder/openstack/common/gettextutils.py
cinder/tests/test_wsgi.py

index 1e5842d88cd443e5d67963af599d45ad40c8b25c..6f26901d798dad0200af6c98a7aa0920f52183de 100755 (executable)
@@ -45,7 +45,7 @@ if os.path.exists(os.path.join(possible_topdir, "cinder", "__init__.py")):
     sys.path.insert(0, possible_topdir)
 
 from cinder.openstack.common import gettextutils
-gettextutils.install('cinder', lazy=False)
+gettextutils.install('cinder')
 
 # Need to register global_opts
 from cinder.common import config   # noqa
index 7df821dfcb96e0a4d965a70cf729712d9c42b2c6..6e83e767855f70e50254623f7c26f707e2d34b3c 100755 (executable)
@@ -35,7 +35,7 @@ if os.path.exists(os.path.join(possible_topdir, "cinder", "__init__.py")):
     sys.path.insert(0, possible_topdir)
 
 from cinder.openstack.common import gettextutils
-gettextutils.install('cinder', lazy=False)
+gettextutils.install('cinder')
 
 # Need to register global_opts
 from cinder.common import config  # noqa
index a714db6240470ef12a872a9b3a9f17472d06d1c0..7cb4d399df881ffd32df7db00c44dddd6d3af1d7 100755 (executable)
@@ -18,7 +18,7 @@ import os
 from oslo.config import cfg
 
 from cinder.openstack.common import gettextutils
-gettextutils.install('cinder', lazy=False)
+gettextutils.install('cinder')
 
 from cinder.db.sqlalchemy import migrate_repo
 from cinder import version
index 4957a747070777aa81bed1736ba408db2a647277..e87fcfb30d0e4ce0be45b9236b1edce74a421f09 100644 (file)
@@ -19,11 +19,10 @@ gettext for openstack-common modules.
 
 Usual usage in an openstack.common module:
 
-    from cinder.openstack.common.gettextutils import _
+    from /home/jsbryant/cinder-dev/gettextutilsSync/cinder/.openstack.common.gettextutils import _
 """
 
 import copy
-import functools
 import gettext
 import locale
 from logging import handlers
@@ -42,7 +41,7 @@ class TranslatorFactory(object):
     """Create translator functions
     """
 
-    def __init__(self, domain, lazy=False, localedir=None):
+    def __init__(self, domain, localedir=None):
         """Establish a set of translation functions for the domain.
 
         :param domain: Name of translation domain,
@@ -55,7 +54,6 @@ class TranslatorFactory(object):
         :type localedir: str
         """
         self.domain = domain
-        self.lazy = lazy
         if localedir is None:
             localedir = os.environ.get(domain.upper() + '_LOCALEDIR')
         self.localedir = localedir
@@ -75,16 +73,19 @@ class TranslatorFactory(object):
         """
         if domain is None:
             domain = self.domain
-        if self.lazy:
-            return functools.partial(Message, domain=domain)
-        t = gettext.translation(
-            domain,
-            localedir=self.localedir,
-            fallback=True,
-        )
-        if six.PY3:
-            return t.gettext
-        return t.ugettext
+        t = gettext.translation(domain,
+                                localedir=self.localedir,
+                                fallback=True)
+        # Use the appropriate method of the translation object based
+        # on the python version.
+        m = t.gettext if six.PY3 else t.ugettext
+
+        def f(msg):
+            """oslo.i18n.gettextutils translation function."""
+            if USE_LAZY:
+                return Message(msg, domain=domain)
+            return m(msg)
+        return f
 
     @property
     def primary(self):
@@ -120,7 +121,7 @@ class TranslatorFactory(object):
 # module within each application.
 
 # Create the global translation functions.
-_translators = TranslatorFactory('cinder')
+_translators = TranslatorFactory('/home/jsbryant/cinder-dev/gettextutilsSync/cinder/')
 
 # The primary translation function using the well-known name "_"
 _ = _translators.primary
@@ -147,19 +148,11 @@ def enable_lazy():
     your project is importing _ directly instead of using the
     gettextutils.install() way of importing the _ function.
     """
-    # FIXME(dhellmann): This function will be removed in oslo.i18n,
-    # because the TranslatorFactory makes it superfluous.
-    global _, _LI, _LW, _LE, _LC, USE_LAZY
-    tf = TranslatorFactory('cinder', lazy=True)
-    _ = tf.primary
-    _LI = tf.log_info
-    _LW = tf.log_warning
-    _LE = tf.log_error
-    _LC = tf.log_critical
+    global USE_LAZY
     USE_LAZY = True
 
 
-def install(domain, lazy=False):
+def install(domain):
     """Install a _() function using the given translation domain.
 
     Given a translation domain, install a _() function using gettext's
@@ -170,26 +163,14 @@ def install(domain, lazy=False):
     a translation-domain-specific environment variable (e.g.
     NOVA_LOCALEDIR).
 
+    Note that to enable lazy translation, enable_lazy must be
+    called.
+
     :param domain: the translation domain
-    :param lazy: indicates whether or not to install the lazy _() function.
-                 The lazy _() introduces a way to do deferred translation
-                 of messages by installing a _ that builds Message objects,
-                 instead of strings, which can then be lazily translated into
-                 any available locale.
     """
-    if lazy:
-        from six import moves
-        tf = TranslatorFactory(domain, lazy=True)
-        moves.builtins.__dict__['_'] = tf.primary
-    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)
+    from six import moves
+    tf = TranslatorFactory(domain)
+    moves.builtins.__dict__['_'] = tf.primary
 
 
 class Message(six.text_type):
@@ -201,7 +182,7 @@ class Message(six.text_type):
     """
 
     def __new__(cls, msgid, msgtext=None, params=None,
-                domain='cinder', *args):
+                domain='/home/jsbryant/cinder-dev/gettextutilsSync/cinder/', *args):
         """Create a new Message object.
 
         In order for translation to work gettext requires a message ID, this
index 269ffc8e344554654377cc07f130b4a682bf134d..812bc4b35f95a0ed754bef8c9849a85dddcb0aa5 100644 (file)
@@ -190,7 +190,8 @@ class ExceptionTest(test.TestCase):
         # also we don't want to install the _() system-wide and
         # potentially break other test cases, so we do it here for this
         # test suite only.
-        gettextutils.install('', lazy=True)
+        gettextutils.install('')
+        gettextutils.enable_lazy()
         from cinder.api.middleware import fault
         return fault.FaultWrapper(inner_app)