diff --git a/bin/keystone-all b/bin/keystone-all index 187a2ee15..b0ad224ea 100755 --- a/bin/keystone-all +++ b/bin/keystone-all @@ -20,10 +20,20 @@ if os.path.exists(os.path.join(possible_topdir, from paste import deploy import pbr.version +from keystone.openstack.common import gettextutils + +# NOTE(blk-u): +# gettextutils.install() must run to set _ before importing any modules that +# contain static translated strings. +# +# Configure gettextutils for deferred translation of messages +# so that error messages in responses can be translated according to the +# Accept-Language in the request rather than the Keystone server locale. +gettextutils.install('keystone', lazy=True) + from keystone.common import environment from keystone.common import utils from keystone import config -from keystone.openstack.common import gettextutils from keystone.openstack.common import importutils @@ -67,11 +77,6 @@ def serve(*servers): if __name__ == '__main__': - # NOTE(blk-u): Configure gettextutils for deferred translation of messages - # so that error messages in responses can be translated according to the - # Accept-Language in the request rather than the Keystone server locale. - gettextutils.install('keystone', lazy=True) - dev_conf = os.path.join(possible_topdir, 'etc', 'keystone.conf') diff --git a/bin/keystone-manage b/bin/keystone-manage index 3eedea3b4..57bed58d8 100755 --- a/bin/keystone-manage +++ b/bin/keystone-manage @@ -13,13 +13,17 @@ if os.path.exists(os.path.join(possible_topdir, '__init__.py')): sys.path.insert(0, possible_topdir) +from keystone.openstack.common import gettextutils + +# NOTE(blk-u): gettextutils.install() must run to set _ before importing any +# modules that contain static translated strings. +gettextutils.install('keystone') + from keystone import cli from keystone.common import environment -from keystone.openstack.common import gettextutils if __name__ == '__main__': - gettextutils.install('keystone') environment.use_stdlib() dev_conf = os.path.join(possible_topdir, diff --git a/httpd/keystone.py b/httpd/keystone.py index 39b5595a2..f6ed0a0e8 100644 --- a/httpd/keystone.py +++ b/httpd/keystone.py @@ -2,16 +2,22 @@ import os from paste import deploy -from keystone.common import environment -from keystone import config from keystone.openstack.common import gettextutils -from keystone.openstack.common import log as logging -# NOTE(blk-u): Configure gettextutils for deferred translation of messages +# NOTE(blk-u): +# gettextutils.install() must run to set _ before importing any modules that +# contain static translated strings. +# +# Configure gettextutils for deferred translation of messages # so that error messages in responses can be translated according to the # Accept-Language in the request rather than the Keystone server locale. gettextutils.install('keystone', lazy=True) +from keystone.common import environment +from keystone import config +from keystone.openstack.common import log as logging + + LOG = logging.getLogger(__name__) CONF = config.CONF CONF(project='keystone') diff --git a/keystone/exception.py b/keystone/exception.py index c0edc263b..3bef16714 100644 --- a/keystone/exception.py +++ b/keystone/exception.py @@ -15,7 +15,6 @@ # under the License. from keystone.common import config -from keystone.openstack.common.gettextutils import _ # noqa from keystone.openstack.common import log as logging diff --git a/keystone/tests/__init__.py b/keystone/tests/__init__.py index e69de29bb..a7faa0a26 100644 --- a/keystone/tests/__init__.py +++ b/keystone/tests/__init__.py @@ -0,0 +1,12 @@ + +from keystone.openstack.common import gettextutils + + +# NOTE(blk-u): +# gettextutils.install() must run to set _ before importing any modules that +# contain static translated strings. +# +# Configure gettextutils for deferred translation of messages +# so that error messages in responses can be translated according to the +# Accept-Language in the request rather than the Keystone server locale. +gettextutils.install('keystone', lazy=True) diff --git a/keystone/tests/core.py b/keystone/tests/core.py index cba6cbf8e..6abe6e244 100644 --- a/keystone/tests/core.py +++ b/keystone/tests/core.py @@ -23,7 +23,6 @@ import StringIO import sys import time -import gettext from lxml import etree import mox import nose.exc @@ -31,8 +30,6 @@ from paste import deploy import stubout import unittest2 as unittest -gettext.install('keystone', unicode=1) - from keystone.common import environment environment.use_eventlet() diff --git a/keystone/tests/test_wsgi.py b/keystone/tests/test_wsgi.py index 0dfa94674..cc95fb446 100644 --- a/keystone/tests/test_wsgi.py +++ b/keystone/tests/test_wsgi.py @@ -298,3 +298,15 @@ class LocalizedResponseTest(test.TestCase): 'title': 'Not Found'}} self.assertEqual(exp, result) + + def test_static_translated_string_is_Message(self): + # Statically created message strings are Message objects so that they + # are lazy-translated. + self.assertIsInstance(exception.Unauthorized.message_format, + gettextutils.Message) + + def test_dynamic_translated_string_is_Message(self): + # Dynamically created message strings are Message objects so that they + # are lazy-translated. + self.assertIsInstance(_('The resource could not be found.'), + gettextutils.Message)