on-the-fly translation with gettext

B

Benjamin Sigonneau

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.
 
M

Mike Driscoll

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.

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
 
B

Benjamin Sigonneau

Mike Driscoll said:
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.
[...]
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:
[...]

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.

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.


I tried it, but unfortunately my problem remains. Anyway, thanks for
the advice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top