]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/I18N.py
fix test code
[wxWidgets.git] / wxPython / demo / I18N.py
1 #Boa:FramePanel:LanguageSelectPanel
2 import os, sys
3 import wx
4 from wx.lib import langlistctrl
5 from Main import opj
6
7
8 # Normally you would just set _ to be a reference to the
9 # wx.GetTranslation function, and then wrap all you literal strings in
10 # _() function calls. Then everytime you use one of your literals, it
11 # would first pass through the translation function and try to load a
12 # translated version of the string from the current message catalogs.
13 # For this example, since we are changinb language on the fly, and
14 # since we are only translating the label for one widget, we'll not do
15 # it the automatic way and we'll be more explicit. See the setup in
16 # __init__() and the translation done in updateLanguage() below.
17
18 _ = wx.GetTranslation
19
20 exampleStrings = [
21 'the quick brown fox jumps over the lazy dog', # demo string
22 'Tip of the Day', # wx built in translation
23 'Warning', # wx built in translation
24 ]
25
26
27 [wxID_LANGUAGESELECTPANEL, wxID_LANGUAGESELECTPANELENGLISHBASECH,
28 wxID_LANGUAGESELECTPANELLANGCTRLCONTAINER,
29 wxID_LANGUAGESELECTPANELLANGFILTERRB, wxID_LANGUAGESELECTPANELSTATICLINE1,
30 wxID_LANGUAGESELECTPANELSTATICTEXT1, wxID_LANGUAGESELECTPANELSTATICTEXT2,
31 wxID_LANGUAGESELECTPANELSTATICTEXT3, wxID_LANGUAGESELECTPANELTRANSLATEDST,
32 ] = [wx.NewId() for _init_ctrls in range(9)]
33
34 class LanguageSelectPanel(wx.Panel):
35 def _init_coll_boxSizer3_Items(self, parent):
36 # generated method, don't edit
37
38 parent.AddWindow(self.langCtrlContainer, 1, border=0, flag=wx.GROW)
39 parent.AddSpacer(wx.Size(8, 8), border=0, flag=0)
40 parent.AddWindow(self.langFilterRB, 0, border=0, flag=0)
41
42 def _init_coll_flexGridSizer1_Growables(self, parent):
43 # generated method, don't edit
44
45 parent.AddGrowableRow(1)
46 parent.AddGrowableCol(0)
47
48 def _init_coll_boxSizer1_Items(self, parent):
49 # generated method, don't edit
50
51 parent.AddWindow(self.staticText1, 0, border=8, flag=wx.ALL)
52 parent.AddSizer(self.boxSizer3, 1, border=8, flag=wx.ALL | wx.GROW)
53 parent.AddSizer(self.boxSizer2, 0, border=8, flag=wx.GROW | wx.ALL)
54
55 def _init_coll_boxSizer2_Items(self, parent):
56 # generated method, don't edit
57
58 parent.AddWindow(self.staticText2, 0, border=8, flag=wx.ALL)
59 parent.AddWindow(self.englishBaseCh, 0, border=8, flag=wx.GROW | wx.ALL)
60 parent.AddWindow(self.staticLine1, 0, border=8, flag=wx.GROW | wx.ALL)
61 parent.AddWindow(self.staticText3, 0, border=8, flag=wx.ALL)
62 parent.AddWindow(self.translatedST, 0, border=8, flag=wx.GROW | wx.ALL)
63
64 def _init_sizers(self):
65 # generated method, don't edit
66 self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)
67
68 self.flexGridSizer1 = wx.FlexGridSizer(cols=2, hgap=8, rows=0, vgap=8)
69
70 self.boxSizer3 = wx.BoxSizer(orient=wx.HORIZONTAL)
71
72 self.boxSizer2 = wx.BoxSizer(orient=wx.VERTICAL)
73
74 self._init_coll_boxSizer1_Items(self.boxSizer1)
75 self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
76 self._init_coll_boxSizer3_Items(self.boxSizer3)
77 self._init_coll_boxSizer2_Items(self.boxSizer2)
78
79 self.SetSizer(self.boxSizer1)
80
81 def _init_ctrls(self, prnt):
82 # generated method, don't edit
83 wx.Panel.__init__(self, id=wxID_LANGUAGESELECTPANEL,
84 name='LanguageSelectPanel', parent=prnt,
85 style=wx.RESIZE_BORDER | wx.DEFAULT_DIALOG_STYLE)
86
87 self.staticText1 = wx.StaticText(id=wxID_LANGUAGESELECTPANELSTATICTEXT1,
88 label='Choose a language that will be used for example translation.',
89 name='staticText1', parent=self, style=0)
90
91 self.langCtrlContainer = wx.Panel(id=wxID_LANGUAGESELECTPANELLANGCTRLCONTAINER,
92 name='langCtrlContainer', parent=self, style=wx.TAB_TRAVERSAL)
93 self.langCtrlContainer.SetBackgroundColour(wx.Colour(255, 255, 255))
94 self.langCtrlContainer.Bind(wx.EVT_SIZE, self.OnLangCtrlContainerSize)
95
96 self.langFilterRB = wx.RadioBox(choices=['Translated example languages',
97 'Available languages on your system', 'All languages'],
98 id=wxID_LANGUAGESELECTPANELLANGFILTERRB, label='Filter',
99 majorDimension=1, name='langFilterRB', parent=self,
100 style=wx.RA_SPECIFY_COLS)
101 self.langFilterRB.Bind(wx.EVT_RADIOBOX, self.OnLangFilterRBRadiobox,
102 id=wxID_LANGUAGESELECTPANELLANGFILTERRB)
103
104 self.staticText2 = wx.StaticText(id=wxID_LANGUAGESELECTPANELSTATICTEXT2,
105 label='English Text:', name='staticText2', parent=self,
106 style=0)
107
108 self.staticText3 = wx.StaticText(id=wxID_LANGUAGESELECTPANELSTATICTEXT3,
109 label='Translated Text:', name='staticText3', parent=self,
110 style=0)
111
112 self.englishBaseCh = wx.Choice(choices=self.choices,
113 id=wxID_LANGUAGESELECTPANELENGLISHBASECH, name='englishBaseCh',
114 parent=self, style=0)
115 self.englishBaseCh.Bind(wx.EVT_CHOICE, self.OnLangSelectAndTranslate,
116 id=wxID_LANGUAGESELECTPANELENGLISHBASECH)
117
118 self.staticLine1 = wx.StaticLine(id=wxID_LANGUAGESELECTPANELSTATICLINE1,
119 name='staticLine1', parent=self, style=0)
120
121 self.translatedST = wx.StaticText(id=wxID_LANGUAGESELECTPANELTRANSLATEDST,
122 label='', name='translatedST', parent=self, style=0)
123
124 self._init_sizers()
125
126 def __init__(self, parent, log):
127 self.choices = []
128 self.choices = exampleStrings
129
130 self._init_ctrls(parent)
131
132 self.log = log
133
134 lang = wx.LANGUAGE_DEFAULT
135 filter = 'demo'
136 langs = (wx.LANGUAGE_AFRIKAANS, wx.LANGUAGE_ENGLISH, wx.LANGUAGE_DEFAULT,
137 wx.LANGUAGE_SPANISH, wx.LANGUAGE_GERMAN, wx.LANGUAGE_ITALIAN,
138 wx.LANGUAGE_FRENCH)
139
140
141 # usually you would define wx.Locale in your wx.App.OnInit class.
142 # for the demo we just define it in this module
143 self.locale = None
144 wx.Locale.AddCatalogLookupPathPrefix(opj('data/locale'))
145 self.updateLanguage(wx.LANGUAGE_DEFAULT)
146
147
148 self.filterMap = {'demo': langlistctrl.LC_ONLY,
149 'available': langlistctrl.LC_AVAILABLE,
150 'all': langlistctrl.LC_ALL}
151
152 self.filterIdxMap = {0: 'demo',
153 1: 'available',
154 2: 'all'}
155 self.langs = langs
156 self.langCtrl = langlistctrl.LanguageListCtrl(self.langCtrlContainer, -1,
157 filter=self.filterMap[filter], only=langs, select=lang)
158
159 self.langCtrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnLangSelectAndTranslate)
160 self.langCtrl.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnClearTranslatedText)
161
162 self.OnLangCtrlContainerSize()
163
164 self.englishBaseCh.Select(0)
165 self.OnLangSelectAndTranslate()
166
167
168 def updateLanguage(self, lang):
169 # Make *sure* any existing locale is deleted before the new
170 # one is created. The old C++ object needs to be deleted
171 # before the new one is created, and if we just assign a new
172 # instance to the old Python variable, the old C++ locale will
173 # not be destroyed soon enough, likely causing a crash.
174 if self.locale:
175 assert sys.getrefcount(self.locale) <= 2
176 del self.locale
177
178 # create a locale object for this language
179 self.locale = wx.Locale(lang)
180 if self.locale.IsOk():
181 self.locale.AddCatalog('wxpydemo')
182 else:
183 self.locale = None
184
185 def translateExample(self):
186 self.translatedST.SetLabel(_(self.englishBaseCh.GetStringSelection()))
187
188 def OnLangCtrlContainerSize(self, event=None):
189 if event: event.Skip()
190 self.langCtrl.SetSize(self.langCtrlContainer.GetSize())
191
192 def OnLangFilterRBRadiobox(self, event):
193 self.langCtrl.SetUpFilter(
194 self.filterMap[self.filterIdxMap[self.langFilterRB.GetSelection()]],
195 self.langs)
196
197 def OnLangSelectAndTranslate(self, event=None):
198 lang = self.langCtrl.GetLanguage()
199
200 if lang is not None:
201 # set to the selected language
202 self.updateLanguage(lang)
203
204 self.translateExample()
205
206 # set back to default
207 self.updateLanguage(wx.LANGUAGE_DEFAULT)
208
209 def OnClearTranslatedText(self, event):
210 self.translatedST.SetLabel('')
211
212
213 def runTest(frame, nb, log):
214 win = LanguageSelectPanel(nb, log)
215 return win
216
217 #-------------------------------------------------------------------------------
218
219 overview = """<html><body>
220 <h2>Internationalization (I18N)</h2>
221 <p>
222 This demo demonstrates how to setup and use the wx.Locale object to translate text.
223 <p>
224 It also shows the langlistctrl.LanguageListCtrl that can be used to display
225 languages with their associated countries flags, e.g. for setting the language
226 in your application.
227
228 </body></html>
229 """
230
231 if __name__ == '__main__':
232 import sys,os
233 import run
234 run.main(['', os.path.basename(sys.argv[0])])