apply unicode() when gettext()(or ngettext()) received string argument and translations have it's own charset. #22

Closed
wants to merge 3 commits into
from

Projects

None yet

2 participants

@longfin
longfin commented Mar 17, 2012

If target translations contain own charset(ex. utf-8), GNUTranslations convert it's key to unicode.

# gettext.py

def _parse(self, fp):
# ...
            if '\x00' in msg:
                # Plural forms
                msgid1, msgid2 = msg.split('\x00')
                tmsg = tmsg.split('\x00')
                if self._charset:
                    msgid1 = unicode(msgid1, self._charset)
                    tmsg = [unicode(x, self._charset) for x in tmsg]
                for i in range(len(tmsg)):
                    catalog[(msgid1, i)] = tmsg[i]
            else:
                if self._charset:
                    msg = unicode(msg, self._charset)
                    tmsg = unicode(tmsg, self._charset)
                catalog[msg] = tmsg

I can't access them via flask-babel's _() in jinja2 because argument of that is utf-8 encoded string, not unicode. so I changed init_app() to install different version of functions to app.jinja_env.

Please check and comment if it's not necessary. thanks.

@dahlia
Contributor
dahlia commented Mar 17, 2012

Though it is a trivial thing, if you del temporarily defined methods (__ugettext and __ungettext) after these are installed to the Jinja environment it would be better.

@longfin
longfin commented Mar 17, 2012

@dahlia Thanks for advise.

@longfin longfin closed this Jan 23, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment