From 5e7ef210c01d3db8c79b969da3aeda50d57c4923 Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Mon, 1 Apr 2013 02:15:32 +0100 Subject: Add NOVA_LOCALEDIR env variable 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 --- nova/openstack/common/gettextutils.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'nova/openstack') diff --git a/nova/openstack/common/gettextutils.py b/nova/openstack/common/gettextutils.py index b9993b491..2d2e94a7c 100644 --- a/nova/openstack/common/gettextutils.py +++ b/nova/openstack/common/gettextutils.py @@ -24,10 +24,27 @@ Usual usage in an openstack.common module: """ 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) -- cgit