Problem using gettext in a python script

Discussion in 'Programmer's Corner' started by someonesdad, Feb 21, 2010.

  1. someonesdad

    Thread Starter Senior Member

    Jul 7, 2009
    1,585
    141
    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):

    Code ( (Unknown Language)):
    1.  
    2. import os.path, sys
    3. import gettext
    4.  
    5. appname = "main"
    6. dir = "./locale"
    7. gettext.install(appname, dir, unicode=True)
    8.  
    9. languages = {
    10.     "english" : gettext.translation(appname, dir, languages=["en"]),
    11.     "german"  : gettext.translation(appname, dir, languages=["de"]),
    12. }
    13.  
    14. # Start by using English
    15. languages["english"].install()
    16. print _("Hi, this message better be in English")
    17.  
    18. # Switch to German
    19. languages["german"].install()
    20. print _("This is a second message that should be in German")
    21.  
    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:

    Code ( (Unknown Language)):
    1.  
    2. ./RCS:
    3.  
    4. ./locale:
    5. de/  en/
    6.  
    7. ./locale/de:
    8. LC_MESSAGES/
    9.  
    10. ./locale/de/LC_MESSAGES:
    11. main.mo
    12.  
    13. ./locale/en:
    14. LC_MESSAGES/
    15.  
    16. ./locale/en/LC_MESSAGES:
    17. main.mo
    18.  
    3. I create two .po files, one for English and one for German. The English one is:

    Code ( (Unknown Language)):
    1.  
    2. # SOME DESCRIPTIVE TITLE.
    3. # Copyright (C) YEAR ORGANIZATION
    4. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
    5. #
    6. msgid ""
    7. msgstr ""
    8. "Project-Id-Version: PACKAGE VERSION\n"
    9. "POT-Creation-Date: 2010-02-16 21:28+MST\n"
    10. "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
    11. "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
    12. "Language-Team: LANGUAGE <LL@li.org>\n"
    13. "MIME-Version: 1.0\n"
    14. "Content-Type: text/plain; charset=CHARSET\n"
    15. "Content-Transfer-Encoding: ENCODING\n"
    16. "Generated-By: pygettext.py 1.5\n"
    17.  
    18.  
    19. #: main.py:22
    20. msgid "Hi, this message better be in English"
    21. msgstr "en:  Hi, this message better be in English"
    22.  
    23. #: main.py:24
    24. msgid "This is a second message that should be in German"
    25. msgstr "en:  This is a second message that should be in German"
    26.  
    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:

    Code ( (Unknown Language)):
    1.  
    2. Traceback (most recent call last):
    3.   File "main.py", line 6, in <module>
    4.     gettext.install(appname, dir, unicode=True)
    5.   File "c:\python26\lib\gettext.py", line 493, in install
    6.     t = translation(domain, localedir, fallback=True, codeset=codeset)
    7.   File "c:\python26\lib\gettext.py", line 478, in translation
    8.     t = _translations.setdefault(key, class_(open(mofile, 'rb')))
    9.   File "c:\python26\lib\gettext.py", line 180, in __init__
    10.     self._parse(fp)
    11.   File "c:\python26\lib\gettext.py", line 337, in _parse
    12.     tmsg = unicode(tmsg, self._charset)
    13. LookupError: unknown encoding: CHARSET
    14.  
    Can anyone educate me on what I'm doing wrong?
     
Loading...