on-the-fly translation with gettext

Discussion in 'Python' started by Benjamin Sigonneau, Sep 16, 2008.

  1. 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
    Benjamin Sigonneau, Sep 16, 2008
    #1
    1. Advertising

  2. On Sep 16, 4:57 am, Benjamin Sigonneau <>
    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
    Mike Driscoll, Sep 16, 2008
    #2
    1. Advertising

  3. Mike Driscoll <> writes:

    > On Sep 16, 4:57 am, Benjamin Sigonneau <>
    > 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.
    >>

    > [...]
    >> 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.


    --
    benjamin
    Benjamin Sigonneau, Sep 17, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AndyW
    Replies:
    1
    Views:
    1,004
    Kevin Spencer
    Jun 30, 2005
  2. Daniel Leidert
    Replies:
    0
    Views:
    675
    Daniel Leidert
    May 28, 2005
  3. tiewknvc9
    Replies:
    2
    Views:
    846
    tiewknvc9
    Jan 18, 2006
  4. sapient
    Replies:
    6
    Views:
    363
    Peter Otten
    Apr 8, 2010
  5. Brian

    Fly outmenu on the fly

    Brian, Apr 8, 2005, in forum: Javascript
    Replies:
    0
    Views:
    103
    Brian
    Apr 8, 2005
Loading...

Share This Page