From a5b15fb04af3260455bd9beee3b7ae691ca1c436 Mon Sep 17 00:00:00 2001 From: Sergey Lukjanov Date: Wed, 9 Jul 2014 16:16:27 +0400 Subject: [PATCH] Use oslo.i18n Replace initialization of gettext / oslo-incubator's gettextutils with oslo.i18n lib initialization. Implements blueprint sahara-oslo-i18n Change-Id: I6626cc22c7d38c0074930e5baf7866ff1dd04028 --- sahara/cli/sahara_all.py | 10 ++++--- sahara/cli/sahara_api.py | 10 ++++--- sahara/cli/sahara_engine.py | 9 +++++-- sahara/i18n.py | 54 +++++++++++++++++++++++++++++++++++++ sahara/tests/__init__.py | 21 +++++++++++++++ 5 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 sahara/i18n.py diff --git a/sahara/cli/sahara_all.py b/sahara/cli/sahara_all.py index 262dd8214c..1e85f52a5a 100644 --- a/sahara/cli/sahara_all.py +++ b/sahara/cli/sahara_all.py @@ -18,13 +18,12 @@ from sahara.utils import patches patches.patch_all() -import gettext import os import sys import eventlet from eventlet import wsgi -from oslo.config import cfg +from oslo import i18n # If ../sahara/__init__.py exists, add ../ to Python search path, so that @@ -37,7 +36,11 @@ if os.path.exists(os.path.join(possible_topdir, '__init__.py')): sys.path.insert(0, possible_topdir) -gettext.install('sahara', unicode=1) + +# NOTE(slukjanov): i18n.enable_lazy() must be called before +# sahara.utils.i18n._() is called to ensure it has the desired +# lazy lookup behavior. +i18n.enable_lazy() import sahara.main as server @@ -55,5 +58,6 @@ def main(): server.setup_sahara_api('all-in-one') server.setup_sahara_engine() + from oslo.config import cfg wsgi.server(eventlet.listen((cfg.CONF.host, cfg.CONF.port), backlog=500), app, log=logging.WritableLogger(LOG), debug=False) diff --git a/sahara/cli/sahara_api.py b/sahara/cli/sahara_api.py index f1e94633f0..f0d59e3857 100644 --- a/sahara/cli/sahara_api.py +++ b/sahara/cli/sahara_api.py @@ -18,13 +18,12 @@ from sahara.utils import patches patches.patch_all() -import gettext import os import sys import eventlet from eventlet import wsgi -from oslo.config import cfg +from oslo import i18n # If ../sahara/__init__.py exists, add ../ to Python search path, so that @@ -37,7 +36,11 @@ if os.path.exists(os.path.join(possible_topdir, '__init__.py')): sys.path.insert(0, possible_topdir) -gettext.install('sahara', unicode=1) + +# NOTE(slukjanov): i18n.enable_lazy() must be called before +# sahara.utils.i18n._() is called to ensure it has the desired +# lazy lookup behavior. +i18n.enable_lazy() import sahara.main as server @@ -54,5 +57,6 @@ def main(): server.setup_sahara_api('distributed') + from oslo.config import cfg wsgi.server(eventlet.listen((cfg.CONF.host, cfg.CONF.port), backlog=500), app, log=logging.WritableLogger(LOG), debug=False) diff --git a/sahara/cli/sahara_engine.py b/sahara/cli/sahara_engine.py index 4e21d37f9b..143d2592bf 100644 --- a/sahara/cli/sahara_engine.py +++ b/sahara/cli/sahara_engine.py @@ -18,10 +18,11 @@ from sahara.utils import patches patches.patch_all() -import gettext import os import sys +from oslo import i18n + # If ../sahara/__init__.py exists, add ../ to Python search path, so that # it will override what happens to be installed in /usr/(local/)lib/python... @@ -33,7 +34,11 @@ if os.path.exists(os.path.join(possible_topdir, '__init__.py')): sys.path.insert(0, possible_topdir) -gettext.install('sahara', unicode=1) + +# NOTE(slukjanov): i18n.enable_lazy() must be called before +# sahara.utils.i18n._() is called to ensure it has the desired +# lazy lookup behavior. +i18n.enable_lazy() import sahara.main as server diff --git a/sahara/i18n.py b/sahara/i18n.py new file mode 100644 index 0000000000..6aeca66bc6 --- /dev/null +++ b/sahara/i18n.py @@ -0,0 +1,54 @@ +# Copyright (c) 2014 Mirantis Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# It's based on oslo.i18n usage in OpenStack Keystone project and +# recommendations from http://docs.openstack.org/developer/oslo.i18n/usage.html + +from oslo import i18n + + +_translators = i18n.TranslatorFactory(domain='sahara') + +# The primary translation function using the well-known name "_" +_ = _translators.primary + +# Translators for log levels. +# +# The abbreviated names are meant to reflect the usual use of a short +# name like '_'. The "L" is for "log" and the other letter comes from +# the level. +_LI = _translators.log_info +_LW = _translators.log_warning +_LE = _translators.log_error +_LC = _translators.log_critical + + +# Due to the fact that some modules in oslo-incubator synced to the +# sahara/openstack/common are using its own gettextutils we're monkey patching +# our copy of oslo-incubator's gettextutils to use our initialized oslo.i18n +# versions. + +# FIXME(slukjanov): Remove the monkey patch when oslo-incubator will fully use +# oslo.i18n and this code will be synced to sahara. The +# sahara/openstack/common/gettextutils.py after that + +from sahara.openstack.common import gettextutils + + +gettextutils._ = _ +gettextutils._LI = _LI +gettextutils._LW = _LW +gettextutils._LE = _LE +gettextutils._LC = _LC diff --git a/sahara/tests/__init__.py b/sahara/tests/__init__.py index e69de29bb2..d2316ab131 100644 --- a/sahara/tests/__init__.py +++ b/sahara/tests/__init__.py @@ -0,0 +1,21 @@ +# Copyright (c) 2014 Mirantis Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from oslo import i18n + +# NOTE(slukjanov): i18n.enable_lazy() must be called before +# sahara.utils.i18n._() is called to ensure it has the desired +# lazy lookup behavior. +i18n.enable_lazy()