[pygtk] Using gettext, where am I doing mistake

Jarek Zgoda jzgoda at o2.pl
Tue Apr 29 03:56:45 WST 2008


Vláďa pisze:
> Hello,
> 
> I'm trying to translate my own application written in PyGTK on Windows.
> This is what I did:
> 
> First of all my test application *translate.py* stored in c:\Python:
> 
> #!/usr/bin/env python
> import pygtk
> pygtk.require('2.0')
> import gtk
> import gettext
> import locale
> 
> gettext.install('translate','locale')
> locale.setlocale(locale.LC_ALL, '')
> print locale.getdefaultlocale()
> 
> class HelloWorld:
> 
>    def destroy(self, widget, data=None):
>        gtk.main_quit()
> 
>    def __init__(self):
>        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
>        self.window.connect("destroy", self.destroy)
>        self.window.set_border_width(10)
>        self.button = gtk.Button(_("Hello world"))
>        self.button.connect("clicked", self.destroy, None)
>        self.window.add(self.button)
>        self.window.show_all()
> 
>    def main(self):
>        gtk.main()
> 
> if __name__ == "__main__":
>    translate = HelloWorld()
>    translate.main()
> 
> I installed gettext for Windows and according to
> /http://faq.pygtk.org/index.py?req=show&file=faq22.002.htp/ did next steps:
> 1) Extract strings to a .pot file using xgettext
> 2) Create  cs.po file using msginit
> 3) Translated the cs.po file with PSPad.
> 4) Compile the cs.po file to cs.mo using msgfmt
> 5) Copy the cs.mo to /c:\Python\locale\cs_CZ\LC_MESSAGES\translate.mo/
> 
> Now I expected my test application to start with the Czech translation I
> made. But it still shows the original "Hello World" string.  The
> function locale.getdefaultlocale()[0] returns "cs_CZ" so I do believe my
> language is set correctly.
> 
> Do you have any ideas what am I doing wrong?

You have to set locale *before* installing gettext. Also on Windows, you
have to check if LANG environment is set and is usable (if not, you can
set it for current process).

    if os.name == 'nt':
        # windows hack for locale setting
        lang = os.getenv('LANG')
        if lang is None:
            default_lang, default_enc = locale.getdefaultlocale()
            if default_lang:
                lang = default_lang
        if lang:
            os.environ['LANG'] = lang

Then you should install gettext.

-- 
Jarek Zgoda
http://zgodowie.org/

"We read Knuth so you don't have to" - Tim Peters


More information about the pygtk mailing list