diff options
author | Doug Hellmann <doug.hellmann@dreamhost.com> | 2014-05-07 12:17:55 -0700 |
---|---|---|
committer | Doug Hellmann <doug.hellmann@dreamhost.com> | 2014-06-04 09:50:36 -0700 |
commit | 38acb78b01aee8e2df9b5a2d9d12e5ce1a1ab44f (patch) | |
tree | 2cedbefd760931005e8910779362b98ba461be9e | |
parent | d601e1c55ba44e4eaacdf7277fd4cbff1cbd755e (diff) | |
download | oslo-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.py | 9 | ||||
-rw-r--r-- | tests/test_locale_dir_variable.py | 32 |
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) |