on-the-fly translation with gettext

Benjamin Sigonneau benjamin at dromaludaire.info
Tue Sep 16 05:57:19 EDT 2008


Hi all,


I'm a complete beginner in python and in GUI designing, yet I'm writing
a GUI using python and Tkinter.  I need it to be available both in french
and english, so I read the Fine Manual and gave a try to gettext.


So far, I've managed to translate my app with the following snippet:

| import gettext
| def install_lang (lang):
|     try:
|         t = gettext.translation(domain = 'mydomain', localedir = 'locale',
|                                 languages = [lang])
|         t.install()
|     except IOError:
|         import __builtin__
|         __builtin__.__dict__['_'] = lambda x: x
 

When I issue an

    install_lang('en')

at the beginning of my program, it is translated in english and,
conversely, an

    install_lang('fr')

at the beginning makes it available in french.


However, I'd like to let the user show the language on-the-fly during
execution.  Having read the Python Library Reference, sec. 21.1.3.3 (see
http://docs.python.org/lib/node740.html), I added a menu with two
radiobuttons and I merely set them up to call install_lang:

| app_lang = StringVar()
| app_lang.set('en')
| langmenu = Menu(root)
| langmenu.add_radiobutton(label = "English", variable = app_lang, value = 'en',
|                          command = lambda l = 'en': install_lang(l))
| langmenu.add_radiobutton(label = "French", variable = app_lang, value = 'fr',
|                          command = lambda l = 'fr': install_lang(l))

 
However, there is no magic.  The language of the application remains
unchanged.  If it was defined to be english at startup, so will it
remain.  Conversely, it will stay in french if this was the language at
startup.


Does somebody has any idea of what I'm doing wrong, and how to fix it?

Thanks.

-- 
benjamin



More information about the Python-list mailing list