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:
@@ -32,3 +32,6 @@ os.environ['EVENTLET_NO_GREENDNS'] = 'yes'
|
|||||||
import eventlet
|
import eventlet
|
||||||
|
|
||||||
eventlet.monkey_patch(os=False)
|
eventlet.monkey_patch(os=False)
|
||||||
|
|
||||||
|
from nova.openstack.common import gettextutils
|
||||||
|
gettextutils.enable_lazy()
|
||||||
|
|||||||
13
nova/test.py
13
nova/test.py
@@ -27,6 +27,7 @@ import eventlet
|
|||||||
eventlet.monkey_patch(os=False)
|
eventlet.monkey_patch(os=False)
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
import gettext
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
@@ -191,6 +192,17 @@ class MoxStubout(fixtures.Fixture):
|
|||||||
self.addCleanup(self.mox.VerifyAll)
|
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):
|
class TestingException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -216,6 +228,7 @@ class TestCase(testtools.TestCase):
|
|||||||
self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
|
self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
|
||||||
self.useFixture(fixtures.NestedTempfile())
|
self.useFixture(fixtures.NestedTempfile())
|
||||||
self.useFixture(fixtures.TempHomeDir())
|
self.useFixture(fixtures.TempHomeDir())
|
||||||
|
self.useFixture(TranslationFixture())
|
||||||
|
|
||||||
if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
|
if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
|
||||||
os.environ.get('OS_STDOUT_CAPTURE') == '1'):
|
os.environ.get('OS_STDOUT_CAPTURE') == '1'):
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ from oslo.config import cfg
|
|||||||
|
|
||||||
from nova import exception
|
from nova import exception
|
||||||
from nova.openstack.common import excutils
|
from nova.openstack.common import excutils
|
||||||
|
from nova.openstack.common import gettextutils
|
||||||
from nova.openstack.common.gettextutils import _
|
from nova.openstack.common.gettextutils import _
|
||||||
from nova.openstack.common import importutils
|
from nova.openstack.common import importutils
|
||||||
from nova.openstack.common import lockutils
|
from nova.openstack.common import lockutils
|
||||||
@@ -476,6 +477,8 @@ def utf8(value):
|
|||||||
"""
|
"""
|
||||||
if isinstance(value, unicode):
|
if isinstance(value, unicode):
|
||||||
return value.encode('utf-8')
|
return value.encode('utf-8')
|
||||||
|
elif isinstance(value, gettextutils.Message):
|
||||||
|
return unicode(value).encode('utf-8')
|
||||||
assert isinstance(value, str)
|
assert isinstance(value, str)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user