summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Odden <mrodden@us.ibm.com>2013-08-07 09:09:07 +0000
committerMatt Odden <mrodden@us.ibm.com>2013-08-14 05:55:49 +0000
commit17df920eeb2b4f14a02a5ed6a5566c82835a24e0 (patch)
tree6031fc599369afcd11abf79294acf397305b7016
parent4c02e0a1181c669bcf19571edbe0c6d58cbc5a4f (diff)
downloadoslo-17df920eeb2b4f14a02a5ed6a5566c82835a24e0.tar.gz
oslo-17df920eeb2b4f14a02a5ed6a5566c82835a24e0.tar.xz
oslo-17df920eeb2b4f14a02a5ed6a5566c82835a24e0.zip
Allow mapping _ to lazy gettext path
Since nova has switched from using gettext.install for importing the _ function to explicitly importing the _ function from gettextutils, we need to be able to enable our deferred translation code path on the gettextutils._ function. Change-Id: I7fec00810ea57c985e1f43c644913a9980b35655
-rw-r--r--openstack/common/gettextutils.py18
-rw-r--r--tests/unit/test_gettext.py36
2 files changed, 53 insertions, 1 deletions
diff --git a/openstack/common/gettextutils.py b/openstack/common/gettextutils.py
index bbf8fe9..321fdd0 100644
--- a/openstack/common/gettextutils.py
+++ b/openstack/common/gettextutils.py
@@ -38,10 +38,26 @@ _localedir = os.environ.get('oslo'.upper() + '_LOCALEDIR')
_t = gettext.translation('oslo', localedir=_localedir, fallback=True)
_AVAILABLE_LANGUAGES = []
+USE_LAZY = False
+
+
+def enable_lazy():
+ """Convenience function for configuring _() to use lazy gettext
+
+ Call this at the start of execution to enable the gettextutils._
+ function to use lazy gettext functionality. This is useful if
+ your project is importing _ directly instead of using the
+ gettextutils.install() way of importing the _ function.
+ """
+ global USE_LAZY
+ USE_LAZY = True
def _(msg):
- return _t.ugettext(msg)
+ if USE_LAZY:
+ return Message(msg, 'oslo')
+ else:
+ return _t.ugettext(msg)
def install(domain, lazy=False):
diff --git a/tests/unit/test_gettext.py b/tests/unit/test_gettext.py
index d9cb9b8..ef16ac3 100644
--- a/tests/unit/test_gettext.py
+++ b/tests/unit/test_gettext.py
@@ -33,6 +33,42 @@ LOG = logging.getLogger(__name__)
class GettextTest(utils.BaseTestCase):
+ def setUp(self):
+ super(GettextTest, self).setUp()
+ # remember so we can reset to it later
+ self._USE_LAZY = gettextutils.USE_LAZY
+
+ def tearDown(self):
+ # reset to value before test
+ gettextutils.USE_LAZY = self._USE_LAZY
+ super(GettextTest, self).tearDown()
+
+ def test_enable_lazy(self):
+ gettextutils.USE_LAZY = False
+
+ gettextutils.enable_lazy()
+ # assert now enabled
+ self.assertTrue(gettextutils.USE_LAZY)
+
+ def test_underscore_non_lazy(self):
+ # set lazy off
+ gettextutils.USE_LAZY = False
+
+ self.mox.StubOutWithMock(gettextutils._t, 'ugettext')
+ gettextutils._t.ugettext('blah').AndReturn('translated blah')
+ self.mox.ReplayAll()
+
+ result = gettextutils._('blah')
+ self.assertEqual('translated blah', result)
+
+ def test_underscore_lazy(self):
+ # set lazy off
+ gettextutils.USE_LAZY = False
+
+ gettextutils.enable_lazy()
+ result = gettextutils._('blah')
+ self.assertIsInstance(result, gettextutils.Message)
+
def test_gettext_does_not_blow_up(self):
LOG.info(gettextutils._('test'))