[openstack-dev] _() consensus??

Xu He Jie xuhj at linux.vnet.ibm.com
Mon Jan 7 09:27:28 UTC 2013


On 2013年01月07日 16:16, Mark McLoughlin wrote:
> On Mon, 2013-01-07 at 15:58 +0800, Xu He Jie wrote:
>> On 2013年01月07日 15:27, Mark McLoughlin wrote:
>>> On Sat, 2013-01-05 at 16:43 +0800, Zhongyue Luo wrote:
>>>> Hi all,
>>>>
>>>> I understand that i18n is an important feature to customers and that having
>>>> gettextutils wrapped on all log messages will benefit
>>>> in packing custom .pot files to ship with a product.
>>> If we want to i18n log messages, I think they should be in a separate
>>> translation domain from the messages we return to users of the REST API.
>>>
>>> The problem with having them both in the same translation domain is that
>>> translators have no way of prioritizing the REST API messages nor do
>>> administrators have any way of disabling the translation of log messages
>>> without the translation of the REST API messages.
>>>
>>> Something like this:
>>>
>>>     import gettext
>>>
>>>     gettext.install('nova', unicode=True)
>>>     t = gettext.translation('nova-logmsgs', fallback=True)
>>>
>>>     import __builtin__
>>>     __builtin__.__dict__['L_'] = t.ugettext
>>>
>>>     print _('This is a user-visible message')
>> User-visible message's translation domain should be decided by user's
>> locale.
>> If we wrapped message with '_()', those message will be translated as
>> server side locale.
>>
>> So we should remove all '_()' from exception message and we should find
>> a way for
>> get user locale from user's request and translated it.
> That's a very good point. Care to file a bug on that?
>
> It still makes sense to have REST API messages and log messages be in
> separate translation domains, though.
>
> Cheers,
> Mark.
>
>
Sorry, I forget that the message still need wrapped by '_()'. Because 
babel need scan
'_()' to find message that need translation.

And I guess all the user-visible message was exception message in REST API
So as example, the code probably looks like:
in Controller:
def index(self, req):
      try:
           res = self._get_(resources)(...)
      except Exception as e:
          LOG.exception(e.message)
          user_locale = get_user_locale_from_request(req)
          t = gettext.tranlsation('xxx-usermsg', languages=user_locale)
          res = {'Error': t.ugettext(e.message.orignal)}
      return res

The exception message can log at server side:
LOG.exception(e.message)

And the exception message also can return to user as user-visible 
message too:
t = gettext.tranlsation('xxx-usermsg', languages=user_locale)
          res = {'Error': t.ugettext(e.message.orignal)}

We can wrapper the user-visible message by 'L_()'. And 'L_' should be an 
object. It can return
server side translation and also can return orignal message that need be 
translated as user request.

It's complex, I'm not sure this is best method. But It looks like those 
two problems are relative. Maybe we can merge two problems into same bp 
or bug?

Thanks,
Alex.




More information about the OpenStack-dev mailing list