Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'string.': 0.04; 'subject:Python': 0.05; 'encoded': 0.05; 'method.': 0.05; 'things.': 0.05; 'locale': 0.07; 'raises': 0.07; 'returned.': 0.07; 'strings.': 0.07; 'utf-8': 0.07; 'python': 0.09; "'''": 0.09; 'charset': 0.09; 'determines': 0.09; 'encode': 0.09; 'encoding.': 0.09; 'gettext': 0.09; 'method:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:problems': 0.09; 'terry': 0.09; 'translations': 0.09; 'windows,': 0.09; 'suggest': 0.11; 'language': 0.14; 'encoding': 0.15; 'charset,': 0.16; 'encodings,': 0.16; 'fallback': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'screen,': 0.16; 'set,': 0.16; 'subject:3.3': 0.16; 'subject:Unicode': 0.16; 'translation': 0.16; 'wrote:': 0.17; 'bytes': 0.17; 'instance': 0.17; 'instance,': 0.17; 'skip:{ 20': 0.17; 'string,': 0.17; 'unicode': 0.17; 'jan': 0.18; 'windows': 0.19; 'module': 0.19; 'equivalent': 0.20; 'otherwise,': 0.20; 'preferred': 0.20; 'skip:" 30': 0.20; 'import': 0.21; 'default,': 0.22; 'oriented': 0.22; 'pre': 0.22; 'seems': 0.23; 'command': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'disk': 0.27; 'header:X-Complaints-To:1': 0.28; 'trouble': 0.28; 'read,': 0.29; "i'm": 0.29; 'returned': 0.30; 'window': 0.30; 'file': 0.32; 'print': 0.32; 'getting': 0.33; 'idle': 0.33; 'handle': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'entry': 0.33; 'screen': 0.34; 'done': 0.34; 'doing': 0.35; 'pm,': 0.35; 'table': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'skip:g 30': 0.36; 'should': 0.36; 'does': 0.37; 'previous': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'forwarded': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'subject:, ': 0.61; 'national': 0.61; 'policy': 0.62; 'different': 0.63; 'multilingual': 0.65; 'response.': 0.67; 'further,': 0.71; 'yourself,': 0.75; 'prompt': 0.78; 'received:fios.verizon.net': 0.84; 'catalog': 0.93 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 21:26:22 -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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356920828 news.xs4all.nl 6841 [2001:888:2000:d::a6]:33086 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35829 On 12/30/2012 8:48 PM, Terry Reedy wrote: > 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. Addition to previous response. >> import gettext >> >> t =3D gettext.translation("greeting", "locale", ["pt"]) Reading further, I see that this returns a GNUTranslations instance >> _ =3D t.lgettext So this calls its method: ''' GNUTranslations.gettext(message) Look up the message id in the catalog and return the corresponding=20 message string, as a Unicode string. If there is no entry in the catalog = for the message id, and a fallback has been set, the look up is=20 forwarded to the fallback=E2=80=99s gettext() method. Otherwise, the mess= age id=20 is returned. GNUTranslations.lgettext(message) Equivalent to gettext(), but the translation is returned as a bytestring = encoded in the selected output charset, or in the preferred system=20 encoding if no encoding was explicitly set with set_output_charset(). ''' So if you want the unicode translation to be utf-8 encoded, either use=20 =2Egettext and encode it yourself, or use "t.set_output_charset('utf-8')"= =20 to have it done automatically. >> print("_charset =3D {0}\n".format(t._charset)) >> print(_("hello")) But since you are printing to screen, I suggest using .gettext and let=20 print do the encoding to the screen encoding. If that still raises an=20 encoding error, then the problem is the console emulator. On windows,=20 for instance, IDLE windows handle the entire BMP charset while the=20 stupid Windows Command Prompt window does not (certainly not by default, = and not yet, as far I know). The encoding of the translations file on disk determines how the bytes=20 of the translation table should be *decoded when read, to create unicode = strings. It does not determine how those strings should be *encoded*=20 when sent to a particular destination. That may depend on the=20 destination. Multilingual international sites used to encode pages in=20 different limited national encodings, according to the language and=20 destination. Now many encode and send *everything* as utf-8. I think=20 this is the proper policy now. .lgettext seems oriented to the older,=20 pre utf-8, national locale encoding way of doing things. --=20 Terry Jan Reedy