Merge "Sync gettextutils.py from oslo-incubator"

This commit is contained in:
Jenkins 2014-07-14 19:24:49 +00:00 committed by Gerrit Code Review
commit cfe8f2dec1
5 changed files with 30 additions and 48 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View 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

View 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)