Add support for API message localization
Using the lazy gettext functionality from oslo gettextutils, it is possible to use the Accept-Language header to translate an exception message to the user requested locale and return that translation from the API. Implements bp user-locale-api Change-Id: I446fdf4b9843155e0c818c271114b1e2e4f43b64
This commit is contained in:
13
nova/test.py
13
nova/test.py
@@ -27,6 +27,7 @@ import eventlet
|
||||
eventlet.monkey_patch(os=False)
|
||||
|
||||
import copy
|
||||
import gettext
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
@@ -191,6 +192,17 @@ class MoxStubout(fixtures.Fixture):
|
||||
self.addCleanup(self.mox.VerifyAll)
|
||||
|
||||
|
||||
class TranslationFixture(fixtures.Fixture):
|
||||
"""Use gettext NullTranslation objects in tests."""
|
||||
|
||||
def setUp(self):
|
||||
super(TranslationFixture, self).setUp()
|
||||
nulltrans = gettext.NullTranslations()
|
||||
gettext_fixture = fixtures.MonkeyPatch('gettext.translation',
|
||||
lambda *x, **y: nulltrans)
|
||||
self.gettext_patcher = self.useFixture(gettext_fixture)
|
||||
|
||||
|
||||
class TestingException(Exception):
|
||||
pass
|
||||
|
||||
@@ -216,6 +228,7 @@ class TestCase(testtools.TestCase):
|
||||
self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
|
||||
self.useFixture(fixtures.NestedTempfile())
|
||||
self.useFixture(fixtures.TempHomeDir())
|
||||
self.useFixture(TranslationFixture())
|
||||
|
||||
if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
|
||||
os.environ.get('OS_STDOUT_CAPTURE') == '1'):
|
||||
|
@@ -43,6 +43,7 @@ from oslo.config import cfg
|
||||
|
||||
from nova import exception
|
||||
from nova.openstack.common import excutils
|
||||
from nova.openstack.common import gettextutils
|
||||
from nova.openstack.common.gettextutils import _
|
||||
from nova.openstack.common import importutils
|
||||
from nova.openstack.common import lockutils
|
||||
@@ -476,6 +477,8 @@ def utf8(value):
|
||||
"""
|
||||
if isinstance(value, unicode):
|
||||
return value.encode('utf-8')
|
||||
elif isinstance(value, gettextutils.Message):
|
||||
return unicode(value).encode('utf-8')
|
||||
assert isinstance(value, str)
|
||||
return value
|
||||
|
||||
|
Reference in New Issue
Block a user