Changing locale at runtime

Hello,

I have a problem with changing of the current locale of my MDI
application at runtime. Switching my own translation isn't a problem,
but switching it for the standard gui elements does not work as
expected. E.g. switching the menu of my MDI window does not change the
translation of the 'window' menu.
I did provide the wxstd.mo files in my setup and actually after
providing them my 'window' menu always shows up in german translation.
Before installing the wxstd files I always got englich menu entries.

Below is the outline of my code. Is there anything wrong with it or
did I miss an important part? Is it possible at all to switch the
language at runtime for all ui elements?

class I18NApp(wx.App):
    def OnInit(self):
        ...
        self.locale = None
        self.UpdateLanguage(wx.LANGUAGE_DEFAULT, False)
        ...

def UpdateLanguage(self, lang, update_ui):
        if self.locale:
            assert sys.getrefcount(self.locale) <= 2
            del self.locale

        self.locale = wx.Locale(lang)
        if self.locale.IsOk():
            APPDIR = self.GetAppDir()
            LOCALEDIR = os.path.join(APPDIR, "locale")
            LOCALEDOMAIN = self.GetGenericAppName()

            wx.Locale.AddCatalogLookupPathPrefix(LOCALEDIR)
            self.locale.AddCatalog(LOCALEDOMAIN)
            self.locale.AddCatalog('wxstd')

            if update_ui and self.frame:
                self.frame.UpdateUI() # rebuild menubar etc.
        else:
            self.locale = None

wx.GetApp().UpdateLanguage(lang, True) is called on language change
request.

Any help is really appreciated.

Best,
Johannes

Hi,

···

On 09/12/2011 01:09 PM, Johannes wrote:

Hello,

I have a problem with changing of the current locale of my MDI
application at runtime. Switching my own translation isn't a problem,
but switching it for the standard gui elements does not work as
expected. E.g. switching the menu of my MDI window does not change the
translation of the 'window' menu.

IIRC that type of change requires a restart of the application, as the labels/texts are set when the widget is created.

Werner

An additional remark. I just tested using wx.FRAME_NO_WINDOW_MENU

style = wx.DEFAULT_FRAME_STYLE | wx.FRAME_NO_WINDOW_MENU
self.frame = AppFrame(None, name = app, title = app, style = style)

but that yields to menus not showing their item text anymore after
opening an additional child mdi frame.

However, I hope to find a solution with using the window menu, so.

Best,
Johannes

You may be able to find some ideas in the group archives as this has been discussed before, although I think most people just end up prompting the user to restart the application, or they build a new instance of their main windows.

···

On 9/12/11 4:09 AM, Johannes wrote:

Hello,

I have a problem with changing of the current locale of my MDI
application at runtime. Switching my own translation isn't a problem,
but switching it for the standard gui elements does not work as
expected. E.g. switching the menu of my MDI window does not change the
translation of the 'window' menu.
I did provide the wxstd.mo files in my setup and actually after
providing them my 'window' menu always shows up in german translation.
Before installing the wxstd files I always got englich menu entries.

Below is the outline of my code. Is there anything wrong with it or
did I miss an important part? Is it possible at all to switch the
language at runtime for all ui elements?

--
Robin Dunn
Software Craftsman