summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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)