From 4fdcbff96790753a4c1a508600e5d78b2c3b7172 Mon Sep 17 00:00:00 2001 From: "Jay S. Bryant" Date: Mon, 7 Jul 2014 15:21:58 -0500 Subject: [PATCH] Sync gettextutils.py from oslo-incubator 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 | 2 +- bin/cinder-api | 2 +- cinder/db/sqlalchemy/migrate_repo/manage.py | 2 +- cinder/openstack/common/gettextutils.py | 69 ++++++++------------- cinder/tests/test_wsgi.py | 3 +- 5 files changed, 30 insertions(+), 48 deletions(-) diff --git a/bin/cinder-all b/bin/cinder-all index 1e5842d88..6f26901d7 100755 --- a/bin/cinder-all +++ b/bin/cinder-all @@ -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 diff --git a/bin/cinder-api b/bin/cinder-api index 7df821dfc..6e83e7678 100755 --- a/bin/cinder-api +++ b/bin/cinder-api @@ -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 diff --git a/cinder/db/sqlalchemy/migrate_repo/manage.py b/cinder/db/sqlalchemy/migrate_repo/manage.py index a714db624..7cb4d399d 100755 --- a/cinder/db/sqlalchemy/migrate_repo/manage.py +++ b/cinder/db/sqlalchemy/migrate_repo/manage.py @@ -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 diff --git a/cinder/openstack/common/gettextutils.py b/cinder/openstack/common/gettextutils.py index 4957a7470..e87fcfb30 100644 --- a/cinder/openstack/common/gettextutils.py +++ b/cinder/openstack/common/gettextutils.py @@ -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 diff --git a/cinder/tests/test_wsgi.py b/cinder/tests/test_wsgi.py index 269ffc8e3..812bc4b35 100644 --- a/cinder/tests/test_wsgi.py +++ b/cinder/tests/test_wsgi.py @@ -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) -- 2.45.2