summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug.hellmann@dreamhost.com>2014-05-07 12:17:55 -0700
committerDoug Hellmann <doug.hellmann@dreamhost.com>2014-06-04 09:50:36 -0700
commit38acb78b01aee8e2df9b5a2d9d12e5ce1a1ab44f (patch)
tree2cedbefd760931005e8910779362b98ba461be9e
parentd601e1c55ba44e4eaacdf7277fd4cbff1cbd755e (diff)
downloadoslo-i18n-38acb78b01aee8e2df9b5a2d9d12e5ce1a1ab44f.tar.gz
Handle . and - in translation domains
Refactor the bit of code used to build the translation domain variable name so it can be tested more easily. If a translation domain includes '.' or '-' in the name, replace them with '_' when building the environment variable name. bp graduate-oslo-i18n Change-Id: I149bf82b9802f88d33d8a5b82dc32817305ca2fe
-rw-r--r--oslo/i18n/gettextutils.py9
-rw-r--r--tests/test_locale_dir_variable.py32
2 files changed, 40 insertions, 1 deletions
diff --git a/oslo/i18n/gettextutils.py b/oslo/i18n/gettextutils.py
index 43f45ee..30eb273 100644
--- a/oslo/i18n/gettextutils.py
+++ b/oslo/i18n/gettextutils.py
@@ -33,6 +33,13 @@ _AVAILABLE_LANGUAGES = {}
USE_LAZY = False
+def _get_locale_dir_variable_name(domain):
+ """Convert a translation domain name to a variable for specifying
+ a separate locale dir.
+ """
+ return domain.upper().replace('.', '_').replace('-', '_') + '_LOCALEDIR'
+
+
class TranslatorFactory(object):
"""Create translator functions
"""
@@ -52,7 +59,7 @@ class TranslatorFactory(object):
self.domain = domain
self.lazy = lazy
if localedir is None:
- localedir = os.environ.get(domain.upper() + '_LOCALEDIR')
+ localedir = os.environ.get(_get_locale_dir_variable_name(domain))
self.localedir = localedir
def _make_translation_func(self, domain=None):
diff --git a/tests/test_locale_dir_variable.py b/tests/test_locale_dir_variable.py
new file mode 100644
index 0000000..59daab9
--- /dev/null
+++ b/tests/test_locale_dir_variable.py
@@ -0,0 +1,32 @@
+# All Rights Reserved.
+#
+# 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 oslotest import base as test_base
+import testscenarios.testcase
+
+from oslo.i18n import gettextutils
+
+
+class LocaleDirVariableTest(testscenarios.testcase.WithScenarios,
+ test_base.BaseTestCase):
+
+ scenarios = [
+ ('simple', {'domain': 'simple', 'expected': 'SIMPLE_LOCALEDIR'}),
+ ('with_dot', {'domain': 'one.two', 'expected': 'ONE_TWO_LOCALEDIR'}),
+ ('with_dash', {'domain': 'one-two', 'expected': 'ONE_TWO_LOCALEDIR'}),
+ ]
+
+ def test_make_variable_name(self):
+ var = gettextutils._get_locale_dir_variable_name(self.domain)
+ self.assertEqual(self.expected, var)