on-the-fly translation with gettext

Mike Driscoll kyosohma at gmail.com
Tue Sep 16 15:26:17 EDT 2008


On Sep 16, 4:57 am, Benjamin Sigonneau <benja... at dromaludaire.info>
wrote:
> 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 (seehttp://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

Try moving another window across your application after you change
languages and see if it gets updated. Sometimes you need to refresh
the layout of your application to get it to work...at least, in
wxPython you do.

Mike



More information about the Python-list mailing list