2323"""
2424
2525import copy
26- import functools
2726import gettext
2827import locale
2928from logging import handlers
@@ -42,7 +41,7 @@ class TranslatorFactory(object):
4241 """Create translator functions
4342 """
4443
45- def __init__ (self , domain , lazy = False , localedir = None ):
44+ def __init__ (self , domain , localedir = None ):
4645 """Establish a set of translation functions for the domain.
4746
4847 :param domain: Name of translation domain,
@@ -55,7 +54,6 @@ def __init__(self, domain, lazy=False, localedir=None):
5554 :type localedir: str
5655 """
5756 self .domain = domain
58- self .lazy = lazy
5957 if localedir is None :
6058 localedir = os .environ .get (domain .upper () + '_LOCALEDIR' )
6159 self .localedir = localedir
@@ -75,16 +73,19 @@ def _make_translation_func(self, domain=None):
7573 """
7674 if domain is None :
7775 domain = self .domain
78- if self .lazy :
79- return functools .partial (Message , domain = domain )
80- t = gettext .translation (
81- domain ,
82- localedir = self .localedir ,
83- fallback = True ,
84- )
85- if six .PY3 :
86- return t .gettext
87- return t .ugettext
76+ t = gettext .translation (domain ,
77+ localedir = self .localedir ,
78+ fallback = True )
79+ # Use the appropriate method of the translation object based
80+ # on the python version.
81+ m = t .gettext if six .PY3 else t .ugettext
82+
83+ def f (msg ):
84+ """oslo.i18n.gettextutils translation function."""
85+ if USE_LAZY :
86+ return Message (msg , domain = domain )
87+ return m (msg )
88+ return f
8889
8990 @property
9091 def primary (self ):
@@ -147,19 +148,11 @@ def enable_lazy():
147148 your project is importing _ directly instead of using the
148149 gettextutils.install() way of importing the _ function.
149150 """
150- # FIXME(dhellmann): This function will be removed in oslo.i18n,
151- # because the TranslatorFactory makes it superfluous.
152- global _ , _LI , _LW , _LE , _LC , USE_LAZY
153- tf = TranslatorFactory ('openstackclient' , lazy = True )
154- _ = tf .primary
155- _LI = tf .log_info
156- _LW = tf .log_warning
157- _LE = tf .log_error
158- _LC = tf .log_critical
151+ global USE_LAZY
159152 USE_LAZY = True
160153
161154
162- def install (domain , lazy = False ):
155+ def install (domain ):
163156 """Install a _() function using the given translation domain.
164157
165158 Given a translation domain, install a _() function using gettext's
@@ -170,26 +163,14 @@ def install(domain, lazy=False):
170163 a translation-domain-specific environment variable (e.g.
171164 NOVA_LOCALEDIR).
172165
166+ Note that to enable lazy translation, enable_lazy must be
167+ called.
168+
173169 :param domain: the translation domain
174- :param lazy: indicates whether or not to install the lazy _() function.
175- The lazy _() introduces a way to do deferred translation
176- of messages by installing a _ that builds Message objects,
177- instead of strings, which can then be lazily translated into
178- any available locale.
179170 """
180- if lazy :
181- from six import moves
182- tf = TranslatorFactory (domain , lazy = True )
183- moves .builtins .__dict__ ['_' ] = tf .primary
184- else :
185- localedir = '%s_LOCALEDIR' % domain .upper ()
186- if six .PY3 :
187- gettext .install (domain ,
188- localedir = os .environ .get (localedir ))
189- else :
190- gettext .install (domain ,
191- localedir = os .environ .get (localedir ),
192- unicode = True )
171+ from six import moves
172+ tf = TranslatorFactory (domain )
173+ moves .builtins .__dict__ ['_' ] = tf .primary
193174
194175
195176class Message (six .text_type ):
@@ -373,8 +354,8 @@ def get_available_languages(domain):
373354 'zh_Hant_HK' : 'zh_HK' ,
374355 'zh_Hant' : 'zh_TW' ,
375356 'fil' : 'tl_PH' }
376- for (locale , alias ) in six .iteritems (aliases ):
377- if locale in language_list and alias not in language_list :
357+ for (locale_ , alias ) in six .iteritems (aliases ):
358+ if locale_ in language_list and alias not in language_list :
378359 language_list .append (alias )
379360
380361 _AVAILABLE_LANGUAGES [domain ] = language_list
0 commit comments