Skip to content

Commit

Permalink
Add NOVA_LOCALEDIR env variable
Browse files Browse the repository at this point in the history
Part of fixing bug #995287

Syncs these two commits from oslo-incubator:

  Support overriding oslo localedir too
  Add a gettextutils.install() helper function

to get a new gettextutils.install() function which allows the default
localedir to be overwritten via an environment variable.

Note that gettextutils.install() must be called before any other nova
modules are imported since some modules attempt to translate strings
at import time (e.g. the 'message' attributes on classes in
nova.exception). This is broken and inefficient, but fixing it involves
adding something like spinx's l_() function and would be very invaisve.

Also, note that calling gettextutils.install() in nova.cmd.__init__
means that no program which uses a different translation domain should
ever import any of the modules under nova.cmd.

Change-Id: I86562b3a65d371673bb21f7179eecc7602bc0775
  • Loading branch information
markmc committed Apr 10, 2013
1 parent 05f5106 commit 5e7ef21
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
8 changes: 6 additions & 2 deletions doc/source/devref/il8n.rst
Expand Up @@ -24,10 +24,14 @@ in nova/tests/test_localization.py.

The ``_()`` function is brought into the global scope by doing::

import gettext
gettext.install("nova", unicode=1)
from nova.openstack.common import gettextutils
gettextutils.install('nova')

These lines are needed in any toplevel script before any nova modules are
imported. If this code is missing, it may result in an error that looks like::

NameError: name '_' is not defined

The gettextutils.install() function also queries the NOVA_LOCALEDIR environment
variable to allow overriding the default localedir with a specific custom
location for Nova's message catalog.
4 changes: 2 additions & 2 deletions nova/cmd/__init__.py
Expand Up @@ -16,5 +16,5 @@
import eventlet
eventlet.monkey_patch(os=False)

import gettext
gettext.install('nova', unicode=1)
from nova.openstack.common import gettextutils
gettextutils.install('nova')
23 changes: 20 additions & 3 deletions nova/openstack/common/gettextutils.py
Expand Up @@ -24,10 +24,27 @@
"""

import gettext
import os


t = gettext.translation('nova', 'locale', fallback=True)
_localedir = os.environ.get('nova'.upper() + '_LOCALEDIR')
_t = gettext.translation('nova', localedir=_localedir, fallback=True)


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,
localedir=os.environ.get(domain.upper() + '_LOCALEDIR'),
unicode=True)

0 comments on commit 5e7ef21

Please sign in to comment.