Fix translate static messages in response

Messages created statically (during import) were not being
translated in responses when the Accept-Language header was
used to set the expected language in the response. The static
messages were being created before the _ built-in had been
installed by gettextutils.install().

Change-Id: Ie56b1d3a836bc5f2262d7af68f803a08ebdf016f
Resolves-Bug: #1215192
This commit is contained in:
Brant Knudson 2013-08-21 18:30:56 -05:00
parent 15ffd4f017
commit 2fe72a0d57
7 changed files with 51 additions and 16 deletions

View File

@ -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')

View File

@ -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,

View File

@ -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')

View File

@ -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

View File

@ -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)

View File

@ -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()

View File

@ -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)