Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feed.xsnews.nl!border-2.ams.xsnews.nl!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.04; 'messages.': 0.04; 'output': 0.04; 'subject:Python': 0.05; 'encoded': 0.05; 'python3': 0.05; 'utf-8': 0.07; 'python': 0.09; 'bytes)': 0.09; 'charset': 0.09; 'encode': 0.09; 'eof': 0.09; 'gettext': 0.09; 'mkdir': 0.09; 'msgid': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'seen,': 0.09; 'subject:problems': 0.09; "system's": 0.09; 'terry': 0.09; 'bug': 0.10; 'looked': 0.10; ';-)': 0.11; 'itself.': 0.11; 'encoding': 0.15; 'code?': 0.16; 'codec': 0.16; 'detects': 0.16; 'folder,': 0.16; 'line).': 0.16; 'msgstr': 0.16; 'ordinal': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:3.3': 0.16; 'subject:Unicode': 0.16; 'unicodeerror': 0.16; 'url:py': 0.16; 'version?': 0.16; 'translation': 0.16; 'string': 0.17; 'wrote:': 0.17; 'bytes': 0.17; 'skip:{ 20': 0.17; 'tries': 0.17; 'unicode': 0.17; 'jan': 0.18; '(or': 0.18; 'module': 0.19; 'equivalent': 0.20; 'preferred': 0.20; 'skip:" 30': 0.20; 'import': 0.21; '(on': 0.22; 'doc': 0.22; 'posts': 0.23; 'seems': 0.23; 'downloaded': 0.24; 'device': 0.24; 'linux': 0.24; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; '(most': 0.27; 'separate': 0.27; 'translated': 0.27; 'header:X-Complaints-To:1': 0.28; 'trouble': 0.28; 'run': 0.28; 'character': 0.29; 'included': 0.29; 'probably': 0.29; 'this.': 0.29; "i'm": 0.29; 'maybe': 0.29; 'returned': 0.30; 'error': 0.30; 'url:python': 0.32; 'file': 0.32; "skip:' 20": 0.32; 'print': 0.32; 'getting': 0.33; 'skip:! 10': 0.33; 'traceback': 0.33; 'handle': 0.33; 'problem': 0.33; 'anyone': 0.33; 'to:addr:python- list': 0.33; "can't": 0.34; 'skip:b 20': 0.34; 'follows:': 0.35; 'said,': 0.35; 'pm,': 0.35; 'received:org': 0.36; 'tool': 0.36; 'but': 0.36; 'url:org': 0.36; 'skip:g 30': 0.36; 'correctly': 0.37; 'itself': 0.37; 'skip:t 40': 0.37; 'two': 0.37; 'why': 0.37; 'previous': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'subject:, ': 0.61; 'worth': 0.63; 'want,': 0.65; "'preferred": 0.84; 'distinguish': 0.84; 'isolated': 0.84; 'received:fios.verizon.net': 0.84; 'url:cpython': 0.84; 'apparent': 0.91; 'look.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Python 3.3, gettext and Unicode problems Date: Sun, 30 Dec 2012 20:48:08 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 102 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356918530 news.xs4all.nl 6880 [2001:888:2000:d::a6]:55882 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35828 On 12/30/2012 7:39 PM, Marcel Rodrigues wrote: > I'm using Python 3.3 (CPython) and am having trouble getting the > standard gettext module to handle Unicode messages. I have never even looked at the doc before, but I will take a look. > My problem can be isolated as follows: > > I have 3 files in a folder: greeting.py, greeting.po and msgfmt.py. > > -- greeting.py -- > import gettext > > t =3D gettext.translation("greeting", "locale", ["pt"]) > _ =3D t.lgettext gettext.lgettext(message) Equivalent to gettext(), but the translation is returned in the=20 preferred system encoding, if no other encoding was explicitly set with=20 bind_textdomain_codeset(). Giving that 'preferred system encoding' apparent means=20 'locale.getpreferredencoding' and that seems to not be what you want,=20 why are you using the 'l' version? > > print("_charset =3D {0}\n".format(t._charset)) > print(_("hello")) A strong suggestion: whenever you want to print a string and the=20 computation of the string (or bytes) involves encoding/decoding,=20 separate the computation and the printing (on two separate line). s =3D _("hello") print(s) The reason is that printing also requires encoding for the output device = and that process can also generate a UnicodeError that may be hard to=20 distinguish from an error in the computation of s itself. > -- EOF -- > > -- greeting.po -- > msgid "" > msgstr "" > "Project-Id-Version: 1.0\n" > "MIME-Version: 1.0\n" > "Content-Type: text/plain; charset=3DUTF-8\n" > "Content-Transfer-Encoding: 8bit\n" > > msgid "hello" > msgstr "ol=C3=A1" > -- EOF -- > > msgfmt.py was downloaded from > http://hg.python.org/cpython/file/9e6ead98762e/Tools/i18n/msgfmt.py, > since this tool apparently isn't included in the python3 package > available on Arch Linux official repositories. > > It's probably also worth noting that the file greeting.po is encoded > itself as UTF-8. > > From that folder, I run the following commands: > > $ mkdir -p locale/pt/LC_MESSAGES > $ python msgfmt.py -o !$/greeting.mo greeting.po > $ python greeting.py > > The output is: > _charset =3D UTF-8 > > Traceback (most recent call last): > File "greeting.py", line 7, in > print(_("hello")) > File "/usr/lib/python3.3/gettext.py", line 314, in lgettext > return tmsg.encode(locale.getpreferredencoding()) > UnicodeEncodeError: 'ascii' codec can't encode character '\xe1' in > position 2: ordinal not in range(128) In particular, we have seen, in previous posts here, this exact error=20 generated during printing rather than during the string computation and=20 posters have wasted time looking for the error in the string or bytes=20 computation itself. > My interpretation of this output is that even though gettext correctly > detects the MO file charset as UTF-8, it tries to encode the translated= > message with the system's "preferred encoding", which happens to be ASC= II. Just as you seem to have requested ;-) > Anyone know why this happens? Is this a bug on my code? Maybe I have > misunderstood gettext... You used lgettext (l =3D locale). As I said, I am new to this. --=20 Terry Jan Reedy