Remove gettext.install from ceilometer/__init__.py

The gettext.install() installs a builtin _() function for translation
purpose. If it is called multiple times, the last call will win and the
translation domain set by the last call will be used.

When the ceilometer.compute.nova_notifier driver used within nova, the
gettext.install() call in ceilometer/__init__.py would change the
previous translation domain set by nova. This is not what we want.

We have to remove this shortcut, and put the gettext.install() call
early on in a top level script.

Instead of directly calling gettext.install(), we leverage the
gettextutils.install() in openstack.common, so we can specifiy the
localedir for ceilometer through the environment variable
CEILOMETER_LOCALEDIR.

This is part of the blueprint gettext-i18n-issue.

Change-Id: Icb2dcfb319778042cf569dcb607f579d1e0fda3a
This commit is contained in:
Lianhao Lu 2013-04-03 15:36:50 +08:00
parent 9d77054863
commit 6bd416dfaa
9 changed files with 42 additions and 7 deletions

View File

@ -22,6 +22,10 @@ eventlet.monkey_patch()
import sys import sys
from oslo.config import cfg from oslo.config import cfg
from ceilometer.openstack.common import gettextutils
gettextutils.install('ceilometer')
from ceilometer.central import manager from ceilometer.central import manager
from ceilometer.service import prepare_service from ceilometer.service import prepare_service
from ceilometer.openstack.common import service from ceilometer.openstack.common import service

View File

@ -22,6 +22,10 @@ eventlet.monkey_patch()
import sys import sys
from oslo.config import cfg from oslo.config import cfg
from ceilometer.openstack.common import gettextutils
gettextutils.install('ceilometer')
from ceilometer.compute import manager from ceilometer.compute import manager
from ceilometer.service import prepare_service from ceilometer.service import prepare_service
from ceilometer.openstack.common import service from ceilometer.openstack.common import service

View File

@ -24,6 +24,9 @@ from wsgiref import simple_server
from oslo.config import cfg from oslo.config import cfg
from ceilometer.openstack.common import gettextutils
gettextutils.install('ceilometer')
from ceilometer.api import app from ceilometer.api import app
from ceilometer import service from ceilometer import service

View File

@ -23,6 +23,9 @@ import sys
from oslo.config import cfg from oslo.config import cfg
from ceilometer.openstack.common import gettextutils
gettextutils.install('ceilometer')
from ceilometer.collector import service as coll_service from ceilometer.collector import service as coll_service
from ceilometer.service import prepare_service from ceilometer.service import prepare_service
from ceilometer.openstack.common import service from ceilometer.openstack.common import service

View File

@ -22,6 +22,9 @@ import sys
from oslo.config import cfg from oslo.config import cfg
from ceilometer.openstack.common import gettextutils
gettextutils.install('ceilometer')
from ceilometer import service from ceilometer import service
from ceilometer import storage from ceilometer import storage

View File

@ -26,6 +26,9 @@ import sys
from oslo.config import cfg from oslo.config import cfg
from stevedore import dispatch from stevedore import dispatch
from ceilometer.openstack.common import gettextutils
gettextutils.install('ceilometer')
from ceilometer import counter from ceilometer import counter
from ceilometer import pipeline from ceilometer import pipeline
from ceilometer import service from ceilometer import service

View File

@ -13,7 +13,3 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
# #
import gettext
gettext.install('ceilometer', unicode=1)

View File

@ -24,10 +24,27 @@ Usual usage in an openstack.common module:
""" """
import gettext import gettext
import os
_localedir = os.environ.get('ceilometer'.upper() + '_LOCALEDIR')
t = gettext.translation('ceilometer', 'locale', fallback=True) _t = gettext.translation('ceilometer', localedir=_localedir, fallback=True)
def _(msg): def _(msg):
return t.ugettext(msg) return _t.ugettext(msg)
def install(domain):
"""Install a _() function using the given translation domain.
Given a translation domain, install a _() function using gettext's
install() function.
The main difference from gettext.install() is that we allow
overriding the default localedir (e.g. /usr/share/locale) using
a translation-domain-specific environment variable (e.g.
NOVA_LOCALEDIR).
"""
gettext.install(domain,
os.environ.get(domain.upper() + '_LOCALEDIR'),
unicode=True)

View File

@ -21,6 +21,7 @@
Built on top of pep8.py Built on top of pep8.py
""" """
import gettext
import imp import imp
import inspect import inspect
import logging import logging
@ -645,6 +646,7 @@ imports_on_separate_lines_N301_compliant = r"""
""" """
if __name__ == "__main__": if __name__ == "__main__":
gettext.install('ceilometer', unicode=True)
#include nova path #include nova path
sys.path.append(os.getcwd()) sys.path.append(os.getcwd())
#Run once tests (not per line) #Run once tests (not per line)