From 17df920eeb2b4f14a02a5ed6a5566c82835a24e0 Mon Sep 17 00:00:00 2001 From: Matt Odden Date: Wed, 7 Aug 2013 09:09:07 +0000 Subject: 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 --- openstack/common/gettextutils.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'openstack/common') 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): -- cgit