summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--openstack/common/gettextutils.py17
-rw-r--r--tests/unit/test_gettext.py18
2 files changed, 33 insertions, 2 deletions
diff --git a/openstack/common/gettextutils.py b/openstack/common/gettextutils.py
index 1e66538..efc6ec2 100644
--- a/openstack/common/gettextutils.py
+++ b/openstack/common/gettextutils.py
@@ -24,6 +24,7 @@ Usual usage in an openstack.common module:
"""
import gettext
+import os
t = gettext.translation('oslo', 'locale', fallback=True)
@@ -31,3 +32,19 @@ t = gettext.translation('oslo', 'locale', fallback=True)
def _(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)
diff --git a/tests/unit/test_gettext.py b/tests/unit/test_gettext.py
index cedc25c..3a86782 100644
--- a/tests/unit/test_gettext.py
+++ b/tests/unit/test_gettext.py
@@ -17,7 +17,9 @@
import logging
-from openstack.common.gettextutils import _
+import mock
+
+from openstack.common import gettextutils
from tests import utils
@@ -27,4 +29,16 @@ LOG = logging.getLogger(__name__)
class GettextTest(utils.BaseTestCase):
def test_gettext_does_not_blow_up(self):
- LOG.info(_('test'))
+ LOG.info(gettextutils._('test'))
+
+ def test_gettext_install_looks_up_localedir(self):
+ with mock.patch('os.environ.get') as environ_get:
+ with mock.patch('gettext.install') as gettext_install:
+ environ_get.return_value = '/foo/bar'
+
+ gettextutils.install('blaa')
+
+ environ_get.assert_called_once_with('BLAA_LOCALEDIR')
+ gettext_install.assert_called_once_with('blaa',
+ localedir='/foo/bar',
+ unicode=True)