This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: gettext has problems with .mo files that use non-ASCII chars
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: avantman42, loewis
Priority: normal Keywords:

Created on 2006-11-08 13:23 by avantman42, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg30479 - (view) Author: Russell Phillips (avantman42) Date: 2006-11-08 13:23
Hi,

I'm trying to use gettext to internationalise my
project [1], but I'm getting the following error
message with some translations:

"Traceback (most recent call last):
file PanicButton.py line 36 in ?
file Gettext.pyc line 177 in _init_
file Gettext.pyc line 274 in _parse
struct.error : unpack str size does not match format"

The snippet of code that loads the .mo file is below
(full file is at [2]):

<code>
# Code to find & install l10n file
import gettext, os, locale, glob
loc = locale.getdefaultlocale ()
sLocale = loc [0]

#Use translation file with same name as locale if it exists
if (os.path.exists (os.path.join ('locale', sLocale +
'.mo'))):
    sLang = os.path.join ('locale', sLocale + '.mo')
else:
    #find a .mo file that matches the first part (first
three
characters) of the locale
    sMoFiles = glob.glob (os.path.join ('locale',
sLocale [:3] +
'*.mo'))
    if (len (sMoFiles) > 0):
        sLang = sMoFiles [0]
    else:
        #Could not find exact or partial match for
locale - use default
translation file (British English)
        sLang = os.path.join ('locale', 'en_GB.mo')

lan = gettext.GNUTranslations (open (sLang))
lan.install ()
# End of code to find & install l10n file
</code>

The problem only seems to appear when the translated
file uses non-ASCII characters.

Full sourcecode is available via the SF.net project
page [1], if required. The .po and .mo files are in the
locale directory [3]. The only .mo file that does not
have problems is en_GB.mo

Russ

[1] http://sourceforge.net/projects/panicbutton
[2]
http://panicbutton.cvs.sourceforge.net/panicbutton/panicbutton/PanicButton.py?view=log
[3]
http://panicbutton.cvs.sourceforge.net/panicbutton/panicbutton/locale/
msg30480 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2006-11-08 21:45
Logged In: YES 
user_id=21627

I can't reproduce the problem. If I understand your
description correctly, then this line
should fail

py> gettext.GNUTranslations(open("de_DE.mo"))
<gettext.GNUTranslations instance at 0xb7cef6ec>

However, it doesn't fail for me, neither with Python 2.4.4
or 2.5.

Are you, by any chance, using Microsoft Windows? If so, you
should open
the file in binary mode (you should actually do so on all
systems; .mo
files are binary):

gettext.GNUTranslations (open (sLang, 'rb'))

Also, I'm puzzled by your traceback. It says

file Gettext.pyc line 177 in _init_
file Gettext.pyc line 274 in _parse

However, Python does not include a file named Gettext.py[c],
only gettext.py 
(lower case 'g'). Where did you get Gettext.pyc from?
msg30481 - (view) Author: Russell Phillips (avantman42) Date: 2006-11-09 10:59
Logged In: YES 
user_id=316750

Got it working. Thank you so much!

I am using Windows, and I'd tried open (sLang, 'rb') and it
didn't make a difference, but that was when I was using
Python 2.4. I'm not sure why it works now and didn't then,
but I'm just happy to have it working.

The Gettext.pyc file came with the Windows Python distribution.

Russ
History
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44210
2006-11-08 13:23:37avantman42create