wxPython Internationalization

M

mojocojo2000

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
 
K

kyosohma

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
 

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

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,750
Members
45,545
Latest member
rapter____0

Latest Threads

Top