]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: internat.cpp | |
c7f3b78b | 3 | // Purpose: Demonstrates internationalisation (i18n) support |
c801d85f KB |
4 | // Author: Vadim Zeitlin/Julian Smart |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c7f3b78b | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/intl.h" | |
c7f3b78b | 29 | #include "wx/file.h" |
c801d85f KB |
30 | #include "wx/log.h" |
31 | ||
618f2efa | 32 | #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) |
47908e25 RR |
33 | #include "mondrian.xpm" |
34 | #endif | |
35 | ||
c801d85f KB |
36 | // Define a new application type |
37 | class MyApp: public wxApp | |
38 | { | |
39 | public: | |
aec18ff7 | 40 | virtual bool OnInit(); |
c7f3b78b | 41 | |
c801d85f | 42 | protected: |
aec18ff7 | 43 | wxLocale m_locale; // locale we'll be using |
c801d85f KB |
44 | }; |
45 | ||
46 | // Define a new frame type | |
47 | class MyFrame: public wxFrame | |
aec18ff7 | 48 | { |
c7f3b78b | 49 | public: |
aec18ff7 MB |
50 | MyFrame(wxFrame *frame, const wxChar *title, |
51 | const wxPoint& pos, const wxSize& size, wxLocale& m_locale); | |
c7f3b78b VZ |
52 | |
53 | public: | |
aec18ff7 MB |
54 | void OnQuit(wxCommandEvent& event); |
55 | void OnAbout(wxCommandEvent& event); | |
56 | void OnPlay(wxCommandEvent& event); | |
57 | void OnOpen(wxCommandEvent& event); | |
58 | ||
59 | DECLARE_EVENT_TABLE() | |
60 | ||
61 | wxLocale& m_locale; | |
c801d85f KB |
62 | }; |
63 | ||
64 | // ID for the menu commands | |
c7f3b78b VZ |
65 | enum |
66 | { | |
aec18ff7 MB |
67 | MINIMAL_QUIT = 1, |
68 | MINIMAL_TEXT, | |
69 | MINIMAL_TEST, | |
70 | MINIMAL_OPEN | |
c7f3b78b | 71 | }; |
c801d85f KB |
72 | |
73 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
aec18ff7 MB |
74 | EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit) |
75 | EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) | |
76 | EVT_MENU(MINIMAL_TEST, MyFrame::OnPlay) | |
77 | EVT_MENU(MINIMAL_OPEN, MyFrame::OnOpen) | |
c801d85f KB |
78 | END_EVENT_TABLE() |
79 | ||
80 | IMPLEMENT_APP(MyApp) | |
81 | ||
82 | ||
071cc2be | 83 | // `Main program' equivalent, creating windows and returning main app frame |
fd323a5e | 84 | bool MyApp::OnInit() |
c801d85f | 85 | { |
aec18ff7 MB |
86 | const wxString langs[] = |
87 | { | |
88 | _T("(System default)"), | |
89 | _T("French"), | |
90 | _T("German"), | |
91 | _T("Russian"), | |
92 | _T("English"), | |
93 | _T("English (U.S.)") | |
94 | }; | |
95 | ||
96 | SetExitOnFrameDelete(FALSE); | |
97 | int lng = wxGetSingleChoiceIndex(_T("Please choose language:"), _T("Language"), | |
a6c1aa90 | 98 | WXSIZEOF(langs), langs); |
aec18ff7 MB |
99 | SetExitOnFrameDelete(TRUE); |
100 | ||
101 | switch (lng) | |
102 | { | |
103 | case 0 : m_locale.Init(wxLANGUAGE_DEFAULT); break; | |
104 | case 1 : m_locale.Init(wxLANGUAGE_FRENCH); break; | |
105 | case 2 : m_locale.Init(wxLANGUAGE_GERMAN); break; | |
106 | case 3 : m_locale.Init(wxLANGUAGE_RUSSIAN); break; | |
107 | case 4 : m_locale.Init(wxLANGUAGE_ENGLISH); break; | |
108 | case -1: | |
109 | case 5 : m_locale.Init(wxLANGUAGE_ENGLISH_US); break; | |
110 | } | |
111 | ||
112 | // Initialize the catalogs we'll be using | |
113 | /* not needed any more, done in wxLocale ctor | |
114 | m_locale.AddCatalog("wxstd"); // 1) for library messages | |
115 | */ | |
116 | m_locale.AddCatalog(wxT("internat")); // 2) our private one | |
117 | /* this catalog is installed in standard location on Linux systems, | |
c801d85f KB |
118 | it might not be installed on yours - just ignore the errrors |
119 | or comment out this line then */ | |
8e97b17b | 120 | #ifdef __LINUX__ |
aec18ff7 | 121 | { |
ae0eb25f VZ |
122 | wxLogNull noLog; |
123 | m_locale.AddCatalog("fileutils"); // 3) and another just for testing | |
aec18ff7 | 124 | } |
8e97b17b | 125 | #endif |
aec18ff7 MB |
126 | |
127 | // Create the main frame window | |
128 | MyFrame *frame = new MyFrame( (wxFrame *) NULL, | |
129 | _("International wxWindows App"), wxPoint(50, 50), wxSize(350, 60), | |
130 | m_locale); | |
131 | ||
132 | // Give it an icon | |
133 | frame->SetIcon(wxICON(mondrian)); | |
134 | ||
135 | // Make a menubar | |
136 | wxMenu *file_menu = new wxMenu; | |
137 | file_menu->Append(wxID_ABOUT, _("&About...")); | |
138 | file_menu->AppendSeparator(); | |
139 | file_menu->Append(MINIMAL_QUIT, _("E&xit")); | |
140 | ||
141 | wxMenu *test_menu = new wxMenu; | |
142 | test_menu->Append(MINIMAL_OPEN, _("&Open bogus file")); | |
143 | test_menu->Append(MINIMAL_TEST, _("&Play a game")); | |
144 | ||
145 | wxMenuBar *menu_bar = new wxMenuBar; | |
146 | menu_bar->Append(file_menu, _("&File")); | |
147 | menu_bar->Append(test_menu, _("&Test")); | |
148 | frame->SetMenuBar(menu_bar); | |
149 | ||
150 | // Show the frame | |
151 | frame->Show(TRUE); | |
152 | SetTopWindow(frame); | |
153 | ||
154 | return TRUE; | |
c801d85f KB |
155 | } |
156 | ||
157 | // My frame constructor | |
aec18ff7 MB |
158 | MyFrame::MyFrame(wxFrame *frame, const wxChar *title, |
159 | const wxPoint& pos, const wxSize& size, wxLocale& l) | |
160 | : wxFrame(frame, -1, title, pos, size), | |
d3f3e35f | 161 | m_locale(l) |
c7f3b78b | 162 | { |
aec18ff7 | 163 | // Empty |
c7f3b78b | 164 | } |
c801d85f | 165 | |
bd7d06f2 | 166 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) |
c801d85f | 167 | { |
aec18ff7 | 168 | Close(TRUE); |
c801d85f KB |
169 | } |
170 | ||
bd7d06f2 | 171 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 172 | { |
aec18ff7 MB |
173 | wxString localeInfo; |
174 | localeInfo.Printf( _("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"), | |
175 | m_locale.GetLocale(), | |
176 | m_locale.GetSysName().c_str(), | |
177 | m_locale.GetCanonicalName().c_str() ); | |
178 | ||
179 | wxMessageDialog(this, wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart")) | |
180 | + wxT("\n\n") + localeInfo, | |
181 | _("About Internat"), wxOK | wxICON_INFORMATION).ShowModal(); | |
c801d85f KB |
182 | } |
183 | ||
bd7d06f2 | 184 | void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event)) |
c7f3b78b | 185 | { |
aec18ff7 MB |
186 | wxString str = wxGetTextFromUser(_("Enter your number:"), |
187 | _("Try to guess my number!"), wxEmptyString, this); | |
188 | ||
189 | if ( str.IsEmpty() ) return; | |
190 | ||
191 | int num; | |
192 | wxSscanf(str, wxT("%d"), &num); | |
193 | if ( num == 0 ) | |
194 | str = _("You've probably entered an invalid number."); | |
195 | else if ( num == 9 ) // this message is not translated (not in catalog) | |
196 | str = "You've found a bug in this program!"; | |
197 | else if ( num != 17 ) // a more implicit way to write _() | |
198 | str = wxGetTranslation(wxT("Bad luck! try again...")); | |
199 | else | |
200 | { | |
201 | str.Empty(); | |
202 | // string must be split in two -- otherwise the translation won't be found | |
203 | str << _("Congratulations! you've won. Here is the magic phrase:") | |
204 | << _("cannot create fifo `%s'"); | |
205 | } | |
206 | ||
207 | wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION); | |
c7f3b78b VZ |
208 | } |
209 | ||
210 | void MyFrame::OnOpen(wxCommandEvent&) | |
211 | { | |
aec18ff7 MB |
212 | // open a bogus file -- the error message should be also translated if you've |
213 | // got wxstd.mo somewhere in the search path | |
214 | wxFile file(wxT("NOTEXIST.ING")); | |
071cc2be | 215 | } |