wxPython Internationalization

Discussion in 'Python' started by mojocojo2000@gmail.com, Jul 24, 2007.

  1. Guest

    I know this may have a very simple answer, nonetheless. I am wishing
    to find the cleanest and most pythonic way of implementing the
    following:

    I am creating a internationalized application in wxPython and have
    sorted the directory structure in the following manner:

    start.py
    <app_name>
    <tools>
    metamenus.py
    main.py
    <data>
    [various files]
    <images>
    [various files]

    Here's the code for the files shown above:

    # start.py

    if __name__ == '__main__':
    import <app_name>.main
    app = <app_name>.MyApp(0)
    app.MainLoop()

    # main.py

    import wx
    from metamenus import MenuBarEx

    class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title,
    wx.DefaultPosition, wx.Size(380, 250))

    menu = \
    [[
    [_('&File')],
    [_('Open a new document')],
    [_('Save the document')]
    ],
    [
    [_('&Edit')]
    ],
    [
    [_('&View')]
    ],
    [
    [_('&Quit\tCtrl+Q')],
    [_('Quit the Application')],
    [_('&Help')]
    ]]

    self.mainmenu = MenuBarEx(self, menu)

    def OnQuit(self, event):
    self.Close()

    class MyApp(wx.App):
    def OnInit(self):
    self.frame = MyFrame(None, -1, 'Title')
    self.frame.Raise()
    self.frame.Refresh()
    self.frame.Show(True)
    return True

    So here's my question. I wish to add the following code so that the _
    method will become of use.

    Here is the code http://wiki.wxpython.org/Internationalization
    recommends:

    import wx
    import os
    import platform
    import codecs
    import sys
    import gettext

    def main():
    # initialise language settings:
    path = sys.path[0].decode(sys.getfilesystemencoding())
    try:
    langIni = codecs.open(os.path.join(path,u'language.ini'),'r',
    'utf-8')
    except IOError:
    language = u'en' #defaults to english
    pass
    else:
    language = langIni.read()

    locales = {
    u'en' : (wx.LANGUAGE_ENGLISH, u'en_US.UTF-8'),
    u'es' : (wx.LANGUAGE_SPANISH, u'es_ES.UTF-8'),
    u'fr' : (wx.LANGUAGE_FRENCH, u'fr_FR.UTF-8'),
    }
    mylocale = wx.Locale(locales[language][0], wx.LOCALE_LOAD_DEFAULT)
    langdir = os.path.join(path,u'locale')
    Lang = gettext.translation(u'messages', langdir,
    languages=[language])
    Lang.install(unicode=1)

    if platform.system() == 'Linux':
    try:
    # to get some language settings to display properly:
    os.environ['LANG'] = locales[language][1]

    except (ValueError, KeyError):
    pass


    #-> Code to launch application goes here. <-#



    if __name__ == '__main__':
    if 'unicode' not in wx.PlatformInfo:
    print "\nInstalled version: %s\nYou need a unicode build of
    wxPython to run this application.\n"%version
    else:
    main()

    So my question is, where would the best place be to implement the code
    above? In start.py, main.py, in a separate file altogether? The
    problem I seem to be having is needing an instance of wx.App before
    the code recommended by the http://wiki.wxpython.org/Internationalization
    can become of use.

    I've come up with solutions to my dilemma but none seem to be
    especially clean. Any recommendations would be great. Also if any
    clarifications are needed let me know.

    Thanks
    , Jul 24, 2007
    #1
    1. Advertising

  2. Guest

    On Jul 24, 1:31 am, "" <>
    wrote:
    > I know this may have a very simple answer, nonetheless. I am wishing
    > to find the cleanest and most pythonic way of implementing the
    > following:
    >
    > I am creating a internationalized application in wxPython and have
    > sorted the directory structure in the following manner:
    >
    > start.py
    > <app_name>
    > <tools>
    > metamenus.py
    > main.py
    > <data>
    > [various files]
    > <images>
    > [various files]
    >
    > Here's the code for the files shown above:
    >
    > # start.py
    >
    > if __name__ == '__main__':
    > import <app_name>.main
    > app = <app_name>.MyApp(0)
    > app.MainLoop()
    >
    > # main.py
    >
    > import wx
    > from metamenus import MenuBarEx
    >
    > class MyFrame(wx.Frame):
    > def __init__(self, parent, id, title):
    > wx.Frame.__init__(self, parent, id, title,
    > wx.DefaultPosition, wx.Size(380, 250))
    >
    > menu = \
    > [[
    > [_('&File')],
    > [_('Open a new document')],
    > [_('Save the document')]
    > ],
    > [
    > [_('&Edit')]
    > ],
    > [
    > [_('&View')]
    > ],
    > [
    > [_('&Quit\tCtrl+Q')],
    > [_('Quit the Application')],
    > [_('&Help')]
    > ]]
    >
    > self.mainmenu = MenuBarEx(self, menu)
    >
    > def OnQuit(self, event):
    > self.Close()
    >
    > class MyApp(wx.App):
    > def OnInit(self):
    > self.frame = MyFrame(None, -1, 'Title')
    > self.frame.Raise()
    > self.frame.Refresh()
    > self.frame.Show(True)
    > return True
    >
    > So here's my question. I wish to add the following code so that the _
    > method will become of use.
    >
    > Here is the codehttp://wiki.wxpython.org/Internationalization
    > recommends:
    >
    > import wx
    > import os
    > import platform
    > import codecs
    > import sys
    > import gettext
    >
    > def main():
    > # initialise language settings:
    > path = sys.path[0].decode(sys.getfilesystemencoding())
    > try:
    > langIni = codecs.open(os.path.join(path,u'language.ini'),'r',
    > 'utf-8')
    > except IOError:
    > language = u'en' #defaults to english
    > pass
    > else:
    > language = langIni.read()
    >
    > locales = {
    > u'en' : (wx.LANGUAGE_ENGLISH, u'en_US.UTF-8'),
    > u'es' : (wx.LANGUAGE_SPANISH, u'es_ES.UTF-8'),
    > u'fr' : (wx.LANGUAGE_FRENCH, u'fr_FR.UTF-8'),
    > }
    > mylocale = wx.Locale(locales[language][0], wx.LOCALE_LOAD_DEFAULT)
    > langdir = os.path.join(path,u'locale')
    > Lang = gettext.translation(u'messages', langdir,
    > languages=[language])
    > Lang.install(unicode=1)
    >
    > if platform.system() == 'Linux':
    > try:
    > # to get some language settings to display properly:
    > os.environ['LANG'] = locales[language][1]
    >
    > except (ValueError, KeyError):
    > pass
    >
    > #-> Code to launch application goes here. <-#
    >
    > if __name__ == '__main__':
    > if 'unicode' not in wx.PlatformInfo:
    > print "\nInstalled version: %s\nYou need a unicode build of
    > wxPython to run this application.\n"%version
    > else:
    > main()
    >
    > So my question is, where would the best place be to implement the code
    > above? In start.py, main.py, in a separate file altogether? The
    > problem I seem to be having is needing an instance of wx.App before
    > the code recommended by thehttp://wiki.wxpython.org/Internationalization
    > can become of use.
    >
    > I've come up with solutions to my dilemma but none seem to be
    > especially clean. Any recommendations would be great. Also if any
    > clarifications are needed let me know.
    >
    > Thanks


    You should post this question to the wxPython user's group. They'll be
    able to give better advice. See http://www.wxpython.org/maillist.php

    Mike
    , Jul 24, 2007
    #2
    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. Anand
    Replies:
    1
    Views:
    352
    Cliff Wells
    Jul 23, 2003
  2. ulysses
    Replies:
    4
    Views:
    764
    Werner Schiendl
    Oct 22, 2003
  3. Logan
    Replies:
    5
    Views:
    762
    David Bolen
    Dec 11, 2003
  4. Ruben Charles
    Replies:
    6
    Views:
    498
    =?ISO-8859-1?Q?Michael_Str=F6der?=
    Oct 25, 2005
  5. Robin Dunn
    Replies:
    0
    Views:
    516
    Robin Dunn
    Mar 28, 2006
Loading...

Share This Page