[openstack-dev] Translate REST API return message to a different language as log translation

Ying Chun Guo guoyingc at cn.ibm.com
Thu Nov 15 16:28:53 UTC 2012



Hi, all

When fault happens, the REST API response messages will contain a string as
an explanation. Currently, the string of explanation is translated based on
server side locale. I got a new requirement: to translate REST API response
messages based on the requested language from client side.

For example, if the client is in China and sends a request with a parameter
"Accept-Language: zh-CN" in HTTP header, the explanation in the REST API
response should be translated into Chinese. If the client is in France and
sends a request with a parameter "Accept-Language: fr-FR" in header, the
explanation in the REST API response should be translated into French.
Regardless of the response messages translation, the log in server side
will be translated based on server side locale setting.

In order to fulfil this request, I think, the most difficult thing is to
separate the logic of LOG and API response. Because currently, the API
response will be written to the LOG exactly as it is. (Refer to
ResourceExceptionHandler.__exit__(self, ex_type, ex_value, ex_traceback) in
Nova/nova/api/openstack/wsgi.py) But I need the response message in the
language specified by client, and the LOG in the language specified by
server locale.

I have two solutions for this requirement.

One is to maintain a map between server locale translation and client
requested translation in gettextutils.py. When raising a
webob.exc.HTTPException, still use the server locale translation as the
explanation, which will be written to log. When composing the response JSON
message (Refer to Fault.__call__(self, req) in
Nova/nova/api/openstack/wsgi.py), get the client requested translation from
the map and put it in the response JSON object.

The other is to remove log writing from API response logic, which means,
removing the log statements in ResourceExceptionHandler.__exit__(self,
ex_type, ex_value, ex_traceback) in Nova/nova/api/openstack/wsgi.py. Add
explicit LOG statement before raising a webob.exc.HTTPException. For
example:

msg = _('Invalid is_public filter [%s]') % req.params['is_public']
raise webob.exc.HTTPBadRequest(explanation=msg)

will be changed to:

msg = _gettext('Invalid is_public filter [%s]')
LOG.error(_(msg) % req.params['is_public'])
raise webob.exc.HTTPBadRequest(explanation=_(msg,req.get_accept_language())
% req.params['is_public'])

Method _gettext(string) will do nothing to the string, just for string
extraction. Method _(msg) will be changed to accept the second parameter
_(msg, locale).

Please let me know your opinion to these two solutions. Which one do you
prefer? Or do you have better solutions?

Regards
Ying Chun Guo (Daisy)
China Standards and Open Source Team
Emerging Technology Institute (ETI)
IBM China Development Lab
Tel:(86-10)82453491
Email: guoyingc at cn.ibm.com
Address: 1F Tower B, Diamond Building 19 Zhongguancun Software Park,
8 Dongbeiwang West Road, Haidian District, Beijing, P.R.C.100193
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstack.org/pipermail/openstack-dev/attachments/20121116/75e1ff8e/attachment.html>


More information about the OpenStack-dev mailing list