[pygtk] Using gettext, where am I doing mistake
Vláďa
vladovi at atlas.cz
Tue Apr 29 20:54:02 WST 2008
Jarek Zgoda napsal(a):
> 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.
>
>
Thank you for this suggestion, unfortunately it didn't change anything.
The function os.getenv('LANG') returns 'cs' for my system. So I do
believe the localization file should be placed in
locale\cs\LC_MESSAGES\translate.mo. But it doesn't work neither. Do you
have any other idea what could be wrong?
A working sample application would be a huge help for me.
Thank you for your time and support.
More information about the pygtk
mailing list