wxPython Internationalization

mojocojo2000 at gmail.com mojocojo2000 at gmail.com
Tue Jul 24 02:31:57 EDT 2007


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




More information about the Python-list mailing list