Example #1
0
def translate_exception(exc, locale):
    """Translates all translatable elements of the given exception."""
    if isinstance(exc, exception.HeatException):
        exc.message = i18n.translate(exc.message, locale)
    else:
        exc.message = i18n.translate(six.text_type(exc), locale)

    if isinstance(exc, webob.exc.HTTPError):
        exc.explanation = i18n.translate(exc.explanation, locale)
        exc.detail = i18n.translate(getattr(exc, 'detail', ''), locale)
    return exc
Example #2
0
def translate_exception(exc, locale):
    """Translates all translatable elements of the given exception."""
    if isinstance(exc, exception.RoboticeException):
        exc.message = i18n.translate(exc.message, locale)
    else:
        exc.message = i18n.translate(six.text_type(exc), locale)

    if isinstance(exc, webob.exc.HTTPError):
        exc.explanation = i18n.translate(exc.explanation, locale)
        exc.detail = i18n.translate(getattr(exc, 'detail', ''), locale)
    return exc
Example #3
0
def render_exception(error, context=None, request=None, user_locale=None):
    """Forms a WSGI response based on the current error."""

    error_message = error.args[0]
    message = i18n.translate(error_message, desired_locale=user_locale)
    if message is error_message:
        # translate() didn't do anything because it wasn't a Message,
        # convert to a string.
        message = six.text_type(message)

    body = {"error": {"code": error.code, "title": error.title, "message": message}}
    headers = []
    if isinstance(error, exception.AuthPluginException):
        body["error"]["identity"] = error.authentication
    elif isinstance(error, exception.Unauthorized):
        url = CONF.public_endpoint
        if not url:
            if request:
                context = {"host_url": request.host_url}
            if context:
                url = Application.base_url(context, "public")
            else:
                url = "http://localhost:%d" % CONF.public_port
        else:
            url = url % CONF

        headers.append(("WWW-Authenticate", 'Keystone uri="%s"' % url))
    return render_response(status=(error.code, error.title), body=body, headers=headers)
Example #4
0
    def __call__(self, req):
        """Respond to a request for all Neutron API versions."""
        version_objs = [
            {
                "id": "v2.0",
                "status": "CURRENT",
            },
        ]

        if req.path != '/':
            language = req.best_match_language()
            msg = _('Unknown API version specified')
            msg = i18n.translate(msg, language)
            return webob.exc.HTTPNotFound(explanation=msg)

        builder = versions_view.get_view_builder(req)
        versions = [builder.build(version) for version in version_objs]
        response = dict(versions=versions)
        metadata = {}

        content_type = req.best_match_content_type()
        body = (wsgi.Serializer(metadata=metadata).serialize(
            response, content_type))

        response = webob.Response()
        response.content_type = content_type
        response.body = body

        return response
Example #5
0
def render_exception(error, context=None, request=None, user_locale=None):
    """Forms a WSGI response based on the current error."""

    error_message = error.args[0]
    message = i18n.translate(error_message, desired_locale=user_locale)
    if message is error_message:
        # translate() didn't do anything because it wasn't a Message,
        # convert to a string.
        message = six.text_type(message)

    body = {'error': {
        'code': error.code,
        'title': error.title,
        'message': message,
    }}
    headers = []
    if isinstance(error, exception.AuthPluginException):
        body['error']['identity'] = error.authentication
    elif isinstance(error, exception.Unauthorized):
        url = CONF.public_endpoint
        if not url:
            if request:
                context = {'host_url': request.host_url}
            if context:
                url = Application.base_url(context, 'public')
            else:
                url = 'http://localhost:%d' % CONF.public_port
        else:
            url = url % CONF

        headers.append(('WWW-Authenticate', 'Keystone uri="%s"' % url))
    return render_response(status=(error.code, error.title),
                           body=body,
                           headers=headers)
Example #6
0
    def __call__(self, req):
        """Respond to a request for all Neutron API versions."""
        version_objs = [
            {
                "id": "v2.0",
                "status": "CURRENT",
            },
        ]

        if req.path != '/':
            language = req.best_match_language()
            msg = _('Unknown API version specified')
            msg = i18n.translate(msg, language)
            return webob.exc.HTTPNotFound(explanation=msg)

        builder = versions_view.get_view_builder(req)
        versions = [builder.build(version) for version in version_objs]
        response = dict(versions=versions)
        metadata = {}

        content_type = req.best_match_content_type()
        body = (wsgi.Serializer(metadata=metadata).
                serialize(response, content_type))

        response = webob.Response()
        response.content_type = content_type
        response.body = body

        return response
Example #7
0
def translate_exception(exc, locale):
    """Translates all translatable elements of the given exception."""
    if isinstance(exc, exception.HeatException):
        exc.message = i18n.translate(exc.message, locale)
    else:
        exc.message = i18n.translate(six.text_type(exc), locale)

    if isinstance(exc, webob.exc.HTTPError):
        # If the explanation is not a Message, that means that the
        # explanation is the default, generic and not translatable explanation
        # from webop.exc. Since the explanation is the error shown when the
        # exception is converted to a response, let's actually swap it with
        # message, since message is what gets passed in at construction time
        # in the API
        if not isinstance(exc.explanation, i18n._message.Message):
            exc.explanation = six.text_type(exc)
            exc.detail = ''
        else:
            exc.explanation = \
                i18n.translate(exc.explanation, locale)
            exc.detail = i18n.translate(exc.detail, locale)
    return exc
Example #8
0
    def _dispatch(req):
        """Dispatch a Request.

        Called by self._router after matching the incoming request to a route
        and putting the information into req.environ. Either returns 404
        or the routed WSGI app's response.
        """
        match = req.environ['wsgiorg.routing_args'][1]
        if not match:
            language = req.best_match_language()
            msg = _('The resource could not be found.')
            msg = i18n.translate(msg, language)
            return webob.exc.HTTPNotFound(explanation=msg)
        app = match['controller']
        return app
Example #9
0
    def _dispatch(req):
        """Dispatch a Request.

        Called by self._router after matching the incoming request to a route
        and putting the information into req.environ. Either returns 404
        or the routed WSGI app's response.
        """
        match = req.environ['wsgiorg.routing_args'][1]
        if not match:
            language = req.best_match_language()
            msg = _('The resource could not be found.')
            msg = i18n.translate(msg, language)
            return webob.exc.HTTPNotFound(explanation=msg)
        app = match['controller']
        return app
Example #10
0
def render_exception(error, request=None, user_locale=None):
    """Forms a WSGI response based on the current error."""

    error_message = error.args[0]
    message = i18n.translate(error_message, desired_locale=user_locale)
    if message is error_message:
        # translate() didn't do anything because it wasn't a Message,
        # convert to a string.
        message = six.text_type(message)

    body = {'error': {
        'code': error.code,
        'title': error.title,
        'message': message,
    }}
    headers = []
    return render_response(status=(error.code, error.title),
                           body=body,
                           headers=headers)
Example #11
0
File: i18n.py Project: Qeas/cinder
def translate(value, user_locale=None):
    return i18n.translate(value, user_locale)
Example #12
0
def translate(value, user_locale):
    return i18n.translate(value, user_locale)
Example #13
0
 def test_translate(self):
     i18n.translate(u'string')
 def test_translate(self):
     i18n.translate(u'string')