[ZPT] Re: [Nuxeo-localizer] Re: Fix for UnicodeError: ASCII decoding error: ordinal not in range(128)

Vladimir Iliev vlado@vintech.bg
27 Jan 2003 10:36:04 +0200


Hi Myroslav,

> In my case it worked. I didn't tested it thoroughly but frontpage 
> rendered translated. Did you put LOCALIZER_USE_ZOPE_UNICODE in export 
> either?

In Your case it worked because Your content encoding is utf-8, right?
My content is windows-1251 encoded and i hitted the same bug but in
StringIO. After few hours of hacking i got it using that in
TranslationService.Domain:

...

from types import UnicodeType
from Localizer import get_request

...

    def translate(self, msgid, mapping=None,
                  context=None, target_language=None):
        """Translate a msgid, maybe doing ${keyword} substitution.

        msgid is the message id to be translated.
        mapping is a set of mapping to be applied on ${keywords}.
        """
        # msgid can be '${name} was born in ${country}'.
        # mapping can be {'country': 'Antarctica', 'name': 'Lomax'}.
        # context must be adaptable to IUserPreferredLanguages.
        mc = self.getMessageCatalog(lang=target_language)
        text = mc.queryMessage(msgid.strip()) # defaults to None
        if isinstance(text, UnicodeType):
            response = get_request().RESPONSE
            text = response._encode_unicode(text)
        return self._interpolate(text, mapping)


-- 
Vladimir Iliev
http://www.vintech.bg