1 #Boa:FramePanel:LanguageSelectPanel
4 from wx
.lib
import langlistctrl
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.
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
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)]
34 class LanguageSelectPanel(wx
.Panel
):
35 def _init_coll_boxSizer3_Items(self
, parent
):
36 # generated method, don't edit
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)
42 def _init_coll_flexGridSizer1_Growables(self
, parent
):
43 # generated method, don't edit
45 parent
.AddGrowableRow(1)
46 parent
.AddGrowableCol(0)
48 def _init_coll_boxSizer1_Items(self
, parent
):
49 # generated method, don't edit
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
)
55 def _init_coll_boxSizer2_Items(self
, parent
):
56 # generated method, don't edit
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
)
64 def _init_sizers(self
):
65 # generated method, don't edit
66 self
.boxSizer1
= wx
.BoxSizer(orient
=wx
.VERTICAL
)
68 self
.flexGridSizer1
= wx
.FlexGridSizer(cols
=2, hgap
=8, rows
=0, vgap
=8)
70 self
.boxSizer3
= wx
.BoxSizer(orient
=wx
.HORIZONTAL
)
72 self
.boxSizer2
= wx
.BoxSizer(orient
=wx
.VERTICAL
)
74 self
._init
_coll
_boxSizer
1_Items
(self
.boxSizer1
)
75 self
._init
_coll
_flexGridSizer
1_Growables
(self
.flexGridSizer1
)
76 self
._init
_coll
_boxSizer
3_Items
(self
.boxSizer3
)
77 self
._init
_coll
_boxSizer
2_Items
(self
.boxSizer2
)
79 self
.SetSizer(self
.boxSizer1
)
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
)
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)
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
)
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
)
104 self
.staticText2
= wx
.StaticText(id=wxID_LANGUAGESELECTPANELSTATICTEXT2
,
105 label
='English Text:', name
='staticText2', parent
=self
,
108 self
.staticText3
= wx
.StaticText(id=wxID_LANGUAGESELECTPANELSTATICTEXT3
,
109 label
='Translated Text:', name
='staticText3', parent
=self
,
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
)
118 self
.staticLine1
= wx
.StaticLine(id=wxID_LANGUAGESELECTPANELSTATICLINE1
,
119 name
='staticLine1', parent
=self
, style
=0)
121 self
.translatedST
= wx
.StaticText(id=wxID_LANGUAGESELECTPANELTRANSLATEDST
,
122 label
='', name
='translatedST', parent
=self
, style
=0)
126 def __init__(self
, parent
, log
):
128 self
.choices
= exampleStrings
130 self
._init
_ctrls
(parent
)
134 lang
= wx
.LANGUAGE_DEFAULT
136 langs
= (wx
.LANGUAGE_AFRIKAANS
, wx
.LANGUAGE_ENGLISH
, wx
.LANGUAGE_DEFAULT
,
137 wx
.LANGUAGE_SPANISH
, wx
.LANGUAGE_GERMAN
, wx
.LANGUAGE_ITALIAN
,
141 # usually you would define wx.Locale in your wx.App.OnInit class.
142 # for the demo we just define it in this module
144 wx
.Locale
.AddCatalogLookupPathPrefix(opj('data/locale'))
145 self
.updateLanguage(wx
.LANGUAGE_DEFAULT
)
148 self
.filterMap
= {'demo': langlistctrl
.LC_ONLY
,
149 'available': langlistctrl
.LC_AVAILABLE
,
150 'all': langlistctrl
.LC_ALL
}
152 self
.filterIdxMap
= {0: 'demo',
156 self
.langCtrl
= langlistctrl
.LanguageListCtrl(self
.langCtrlContainer
, -1,
157 filter=self
.filterMap
[filter], only
=langs
, select
=lang
)
159 self
.langCtrl
.Bind(wx
.EVT_LIST_ITEM_SELECTED
, self
.OnLangSelectAndTranslate
)
160 self
.langCtrl
.Bind(wx
.EVT_LIST_ITEM_DESELECTED
, self
.OnClearTranslatedText
)
162 self
.OnLangCtrlContainerSize()
164 self
.englishBaseCh
.Select(0)
165 self
.OnLangSelectAndTranslate()
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.
175 assert sys
.getrefcount(self
.locale
) <= 2
178 # create a locale object for this language
179 self
.locale
= wx
.Locale(lang
)
180 if self
.locale
.IsOk():
181 self
.locale
.AddCatalog('wxpydemo')
185 def translateExample(self
):
186 self
.translatedST
.SetLabel(_(self
.englishBaseCh
.GetStringSelection()))
188 def OnLangCtrlContainerSize(self
, event
=None):
189 if event
: event
.Skip()
190 self
.langCtrl
.SetSize(self
.langCtrlContainer
.GetSize())
192 def OnLangFilterRBRadiobox(self
, event
):
193 self
.langCtrl
.SetUpFilter(
194 self
.filterMap
[self
.filterIdxMap
[self
.langFilterRB
.GetSelection()]],
197 def OnLangSelectAndTranslate(self
, event
=None):
198 lang
= self
.langCtrl
.GetLanguage()
201 # set to the selected language
202 self
.updateLanguage(lang
)
204 self
.translateExample()
206 # set back to default
207 self
.updateLanguage(wx
.LANGUAGE_DEFAULT
)
209 def OnClearTranslatedText(self
, event
):
210 self
.translatedST
.SetLabel('')
213 def runTest(frame
, nb
, log
):
214 win
= LanguageSelectPanel(nb
, log
)
217 #-------------------------------------------------------------------------------
219 overview
= """<html><body>
220 <h2>Internationalization (I18N)</h2>
222 This demo demonstrates how to setup and use the wx.Locale object to translate text.
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
231 if __name__
== '__main__':
234 run
.main(['', os
.path
.basename(sys
.argv
[0])])