sys.path.insert(0, possible_topdir)
from cinder.openstack.common import gettextutils
-gettextutils.install('cinder')
+gettextutils.install('cinder', lazy=True)
from cinder.common import config # Need to register global_opts
from cinder.openstack.common import log as logging
# eventlet is updated/released to fix the root issue
import eventlet
-
eventlet.monkey_patch()
import os
sys.path.insert(0, possible_topdir)
from cinder.openstack.common import gettextutils
-gettextutils.install('cinder')
+gettextutils.install('cinder', lazy=True)
from cinder.common import config # Need to register global_opts
from cinder.openstack.common import log as logging
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
+# Copyright 2013 IBM Corp.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
import webob
from cinder import exception
+from cinder.openstack.common import gettextutils
from cinder.openstack.common import jsonutils
from cinder.openstack.common import log as logging
from cinder import utils
return content_type
+ def best_match_language(self):
+ """Determines best available locale from the Accept-Language header."""
+ all_languages = gettextutils.get_available_languages('cinder')
+ return self.accept_language.best_match(all_languages,
+ default_match='en_US')
+
class ActionDispatcher(object):
"""Maps method name to local methods through action name."""
def __call__(self, req):
"""Generate a WSGI response based on the exception passed to ctor."""
# Replace the body with fault details.
+ locale = req.best_match_language()
code = self.wrapped_exc.status_int
fault_name = self._fault_names.get(code, "computeFault")
+ explanation = self.wrapped_exc.explanation
fault_data = {
fault_name: {
'code': code,
- 'message': self.wrapped_exc.explanation}}
+ 'message': gettextutils.get_localized_message(explanation,
+ locale)}}
if code == 413:
retry = self.wrapped_exc.headers['Retry-After']
fault_data[fault_name]['retryAfter'] = retry
@webob.dec.wsgify(RequestClass=Request)
def __call__(self, request):
- """
- Return the wrapped exception with a serialized body conforming to our
- error format.
- """
+ """Serializes the wrapped exception conforming to our error format."""
content_type = request.best_match_content_type()
metadata = {"attributes": {"overLimitFault": "code"}}
+ def translate(msg):
+ locale = request.best_match_language()
+ return gettextutils.get_localized_message(msg, locale)
+
+ self.content['overLimitFault']['message'] = \
+ translate(self.content['overLimitFault']['message'])
+ self.content['overLimitFault']['details'] = \
+ translate(self.content['overLimitFault']['details'])
+
xml_serializer = XMLDictSerializer(metadata, XMLNS_V1)
serializer = {
'application/xml': xml_serializer,
from xml.dom import minidom
-import webob
import webob.dec
import webob.exc
from cinder.api import common
from cinder.api.openstack import wsgi
+from cinder.openstack.common import gettextutils
from cinder.openstack.common import jsonutils
from cinder import test
self.assertTrue('resizeNotAllowed' not in resp.body)
self.assertTrue('forbidden' in resp.body)
+ def test_raise_localized_explanation(self):
+ params = ('blah', )
+ expl = gettextutils.Message("String with params: %s" % params, 'test')
+
+ def _mock_translation(msg, locale):
+ return "Mensaje traducido"
+
+ self.stubs.Set(gettextutils,
+ "get_localized_message", _mock_translation)
+
+ @webob.dec.wsgify
+ def raiser(req):
+ raise wsgi.Fault(webob.exc.HTTPNotFound(explanation=expl))
+
+ req = webob.Request.blank('/.xml')
+ resp = req.get_response(raiser)
+ self.assertEqual(resp.content_type, "application/xml")
+ self.assertEqual(resp.status_int, 404)
+ self.assertTrue(("Mensaje traducido") in resp.body)
+ self.stubs.UnsetAll()
+
def test_fault_has_status_int(self):
"""Ensure the status_int is set correctly on faults"""
fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='what?'))
result = request.best_match_content_type()
self.assertEqual(result, "application/json")
+ def test_best_match_language(self):
+ # Here we test that we are actually invoking language negotiation
+ # by webob and also that the default locale always available is en-US
+ request = wsgi.Request.blank('/')
+ accepted = 'unknown-lang'
+ request.headers = {'Accept-Language': accepted}
+
+ def fake_best_match(self, offers, default_match=None):
+ return default_match
+
+ self.stubs.SmartSet(request.accept_language,
+ 'best_match', fake_best_match)
+
+ self.assertEqual(request.best_match_language(), 'en_US')
+
class ActionDispatcherTest(test.TestCase):
def test_dispatch(self):
from cinder import exception
from cinder.openstack.common import excutils
+from cinder.openstack.common import gettextutils
from cinder.openstack.common import importutils
from cinder.openstack.common import lockutils
from cinder.openstack.common import log as logging
"""
if isinstance(value, unicode):
return value.encode('utf-8')
+ elif isinstance(value, gettextutils.Message):
+ return unicode(value).encode('utf-8')
elif isinstance(value, str):
return value
else:
backlog=backlog)
self._server = eventlet.spawn(self._start)
(self._host, self._port) = self._socket.getsockname()[0:2]
- LOG.info(_("Started %(name)s on %(_host)s:%(_port)s") % self.__dict__)
+ LOG.info(_("Started %(name)s on %(host)s:%(port)s") %
+ {'name': self.name, 'host': self.host, 'port': self.port})
@property
def host(self):