Problem using gettext in a python script

Thread Starter

someonesdad

Joined Jul 7, 2009
1,583
I am trying to write a simple example script in python that demonstrates that I can use the translation features of the python gettext module. I don't speak a foreign language, but I'm trying to write an application that is properly internationalized so users around the world can localize it to their language by creating a .po/.mo file pair with the use of gettext.

I have not found any clear documentation on how to do this, so I'm struggling. I keep getting an error message from python (which I'll give below). If anyone can point me to some good documentation or help me understand what I'm doing wrong, I'd appreciate it. Here's one of the web pages I'm using for guidance (I'm using wxPython for my application, so this was a "natural" find):

http://wiki.wxpython.org/index.cgi/Internationalization

Here's my script in main.py (I'm using the 2.6.4 version of python):

Rich (BB code):
import os.path, sys
import gettext

appname = "main"
dir = "./locale"
gettext.install(appname, dir, unicode=True)

languages = {
    "english" : gettext.translation(appname, dir, languages=["en"]),
    "german"  : gettext.translation(appname, dir, languages=["de"]),
}

# Start by using English
languages["english"].install()
print _("Hi, this message better be in English")

# Switch to German
languages["german"].install()
print _("This is a second message that should be in German")
Here's the process I use to construct my mo file (I'm on a Win XP system using cygwin):

1. I run c:/Python26/Tools/i18n/pygettext.py in the python distribution directory with the command line "python c:/Python26/Tools/i18n/pygettext.py -o main.pot main.py".

2. I have the locale directory under the directory I'm working in; it has the following structure and files:

Rich (BB code):
./RCS:

./locale:
de/  en/

./locale/de:
LC_MESSAGES/

./locale/de/LC_MESSAGES:
main.mo

./locale/en:
LC_MESSAGES/

./locale/en/LC_MESSAGES:
main.mo
3. I create two .po files, one for English and one for German. The English one is:

Rich (BB code):
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2010-02-16 21:28+MST\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: ENCODING\n"
"Generated-By: pygettext.py 1.5\n"


#: main.py:22
msgid "Hi, this message better be in English"
msgstr "en:  Hi, this message better be in English"

#: main.py:24
msgid "This is a second message that should be in German"
msgstr "en:  This is a second message that should be in German"
The German one is the same with "en" changed to "de" in the .po file. Note line numbers won't quite match because I removed some cruft from the beginning of the main.py script.

4. I run "python c:/Python26/Tools/i18n/msgfmt.py main.po" on these two .po files to produce the .mo files. These get copied to the appropriate directory given above in 2. When I run the script, I get the following traceback:

Rich (BB code):
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    gettext.install(appname, dir, unicode=True)
  File "c:\python26\lib\gettext.py", line 493, in install
    t = translation(domain, localedir, fallback=True, codeset=codeset)
  File "c:\python26\lib\gettext.py", line 478, in translation
    t = _translations.setdefault(key, class_(open(mofile, 'rb')))
  File "c:\python26\lib\gettext.py", line 180, in __init__
    self._parse(fp)
  File "c:\python26\lib\gettext.py", line 337, in _parse
    tmsg = unicode(tmsg, self._charset)
LookupError: unknown encoding: CHARSET
Can anyone educate me on what I'm doing wrong?
 
Top