]>
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$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
c7f3b78b | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
017fcf32 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
c801d85f KB |
19 | |
20 | // For compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/wx.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/intl.h" | |
c7f3b78b | 32 | #include "wx/file.h" |
c801d85f KB |
33 | #include "wx/log.h" |
34 | ||
618f2efa | 35 | #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) |
47908e25 RR |
36 | #include "mondrian.xpm" |
37 | #endif | |
38 | ||
017fcf32 VZ |
39 | // ---------------------------------------------------------------------------- |
40 | // private classes | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
c801d85f KB |
43 | // Define a new application type |
44 | class MyApp: public wxApp | |
45 | { | |
46 | public: | |
aec18ff7 | 47 | virtual bool OnInit(); |
c7f3b78b | 48 | |
c801d85f | 49 | protected: |
aec18ff7 | 50 | wxLocale m_locale; // locale we'll be using |
c801d85f KB |
51 | }; |
52 | ||
53 | // Define a new frame type | |
54 | class MyFrame: public wxFrame | |
aec18ff7 | 55 | { |
c7f3b78b | 56 | public: |
017fcf32 | 57 | MyFrame(wxLocale& m_locale); |
c7f3b78b VZ |
58 | |
59 | public: | |
aec18ff7 MB |
60 | void OnQuit(wxCommandEvent& event); |
61 | void OnAbout(wxCommandEvent& event); | |
62 | void OnPlay(wxCommandEvent& event); | |
63 | void OnOpen(wxCommandEvent& event); | |
849a28d0 VS |
64 | void OnTest1(wxCommandEvent& event); |
65 | void OnTest2(wxCommandEvent& event); | |
66 | void OnTest3(wxCommandEvent& event); | |
f13880a9 | 67 | |
aec18ff7 MB |
68 | DECLARE_EVENT_TABLE() |
69 | ||
70 | wxLocale& m_locale; | |
c801d85f KB |
71 | }; |
72 | ||
017fcf32 VZ |
73 | // ---------------------------------------------------------------------------- |
74 | // constants | |
75 | // ---------------------------------------------------------------------------- | |
76 | ||
c801d85f | 77 | // ID for the menu commands |
c7f3b78b VZ |
78 | enum |
79 | { | |
f13880a9 | 80 | INTERNAT_TEXT = wxID_HIGHEST + 1, |
017fcf32 | 81 | INTERNAT_TEST, |
849a28d0 VS |
82 | INTERNAT_TEST_1, |
83 | INTERNAT_TEST_2, | |
84 | INTERNAT_TEST_3, | |
017fcf32 | 85 | INTERNAT_OPEN |
c7f3b78b | 86 | }; |
c801d85f | 87 | |
017fcf32 | 88 | // ---------------------------------------------------------------------------- |
be5a51fb | 89 | // wxWidgets macros |
017fcf32 VZ |
90 | // ---------------------------------------------------------------------------- |
91 | ||
c801d85f | 92 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
f13880a9 | 93 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) |
aec18ff7 | 94 | EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) |
017fcf32 VZ |
95 | EVT_MENU(INTERNAT_TEST, MyFrame::OnPlay) |
96 | EVT_MENU(INTERNAT_OPEN, MyFrame::OnOpen) | |
849a28d0 VS |
97 | EVT_MENU(INTERNAT_TEST_1, MyFrame::OnTest1) |
98 | EVT_MENU(INTERNAT_TEST_2, MyFrame::OnTest2) | |
99 | EVT_MENU(INTERNAT_TEST_3, MyFrame::OnTest3) | |
c801d85f KB |
100 | END_EVENT_TABLE() |
101 | ||
102 | IMPLEMENT_APP(MyApp) | |
103 | ||
017fcf32 VZ |
104 | // ============================================================================ |
105 | // implementation | |
106 | // ============================================================================ | |
107 | ||
108 | // ---------------------------------------------------------------------------- | |
109 | // MyApp | |
110 | // ---------------------------------------------------------------------------- | |
c801d85f | 111 | |
071cc2be | 112 | // `Main program' equivalent, creating windows and returning main app frame |
fd323a5e | 113 | bool MyApp::OnInit() |
c801d85f | 114 | { |
017fcf32 VZ |
115 | long lng = -1; |
116 | ||
117 | if ( argc == 2 ) | |
aec18ff7 | 118 | { |
017fcf32 | 119 | // the parameter must be the lang index |
5c0ee4a8 MB |
120 | wxString tmp(argv[1]); |
121 | tmp.ToLong(&lng); | |
017fcf32 VZ |
122 | } |
123 | ||
a2f26963 VZ |
124 | static const wxLanguage langIds[] = |
125 | { | |
126 | wxLANGUAGE_DEFAULT, | |
127 | wxLANGUAGE_FRENCH, | |
128 | wxLANGUAGE_GERMAN, | |
129 | wxLANGUAGE_RUSSIAN, | |
b28b097d | 130 | wxLANGUAGE_BULGARIAN, |
849a28d0 | 131 | wxLANGUAGE_CZECH, |
f13880a9 | 132 | wxLANGUAGE_POLISH, |
b61261df | 133 | wxLANGUAGE_SWEDISH, |
996994c7 | 134 | #if wxUSE_UNICODE || defined(__WXMOTIF__) |
a2f26963 | 135 | wxLANGUAGE_JAPANESE, |
996994c7 MB |
136 | #endif |
137 | #if wxUSE_UNICODE | |
65dc921d VS |
138 | wxLANGUAGE_GEORGIAN, |
139 | #endif | |
a2f26963 | 140 | wxLANGUAGE_ENGLISH, |
65dc921d | 141 | wxLANGUAGE_ENGLISH_US |
a2f26963 VZ |
142 | }; |
143 | ||
017fcf32 VZ |
144 | if ( lng == -1 ) |
145 | { | |
a2f26963 VZ |
146 | // note that it makes no sense to translate these strings, they are |
147 | // shown before we set the locale anyhow | |
148 | const wxString langNames[] = | |
017fcf32 | 149 | { |
a2f26963 | 150 | _T("System default"), |
017fcf32 VZ |
151 | _T("French"), |
152 | _T("German"), | |
153 | _T("Russian"), | |
b28b097d | 154 | _T("Bulgarian"), |
849a28d0 | 155 | _T("Czech"), |
f13880a9 | 156 | _T("Polish"), |
b61261df | 157 | _T("Swedish"), |
996994c7 | 158 | #if wxUSE_UNICODE || defined(__WXMOTIF__) |
65dc921d | 159 | _T("Japanese"), |
996994c7 MB |
160 | #endif |
161 | #if wxUSE_UNICODE | |
96c0a516 | 162 | _T("Georgian"), |
65dc921d | 163 | #endif |
017fcf32 VZ |
164 | _T("English"), |
165 | _T("English (U.S.)") | |
166 | }; | |
167 | ||
a2f26963 VZ |
168 | // the arrays should be in sync |
169 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds), | |
170 | LangArraysMismatch ); | |
171 | ||
017fcf32 VZ |
172 | lng = wxGetSingleChoiceIndex |
173 | ( | |
174 | _T("Please choose language:"), | |
f13880a9 | 175 | _T("Language"), |
a2f26963 VZ |
176 | WXSIZEOF(langNames), |
177 | langNames | |
017fcf32 VZ |
178 | ); |
179 | } | |
180 | ||
a2f26963 VZ |
181 | if ( lng != -1 ) |
182 | m_locale.Init(langIds[lng]); | |
aec18ff7 | 183 | |
de93d71f VZ |
184 | // normally this wouldn't be necessary as the catalog files would be found |
185 | // in the default locations, but under Windows then the program is not | |
186 | // installed the catalogs are in the parent directory (because the binary | |
187 | // is in a subdirectory of samples/internat) where we wouldn't find them by | |
188 | // default | |
189 | wxLocale::AddCatalogLookupPathPrefix(wxT(".")); | |
190 | wxLocale::AddCatalogLookupPathPrefix(wxT("..")); | |
017fcf32 | 191 | |
aec18ff7 | 192 | // Initialize the catalogs we'll be using |
017fcf32 VZ |
193 | m_locale.AddCatalog(wxT("internat")); |
194 | ||
195 | // this catalog is installed in standard location on Linux systems and | |
196 | // shows that you may make use of the standard message catalogs as well | |
197 | // | |
198 | // if it's not installed on your system, it is just silently ignored | |
8e97b17b | 199 | #ifdef __LINUX__ |
aec18ff7 | 200 | { |
017fcf32 VZ |
201 | wxLogNull noLog; |
202 | m_locale.AddCatalog(_T("fileutils")); | |
aec18ff7 | 203 | } |
8e97b17b | 204 | #endif |
aec18ff7 MB |
205 | |
206 | // Create the main frame window | |
017fcf32 | 207 | MyFrame *frame = new MyFrame(m_locale); |
aec18ff7 MB |
208 | |
209 | // Give it an icon | |
210 | frame->SetIcon(wxICON(mondrian)); | |
211 | ||
212 | // Make a menubar | |
213 | wxMenu *file_menu = new wxMenu; | |
214 | file_menu->Append(wxID_ABOUT, _("&About...")); | |
215 | file_menu->AppendSeparator(); | |
f13880a9 | 216 | file_menu->Append(wxID_EXIT, _("E&xit")); |
aec18ff7 MB |
217 | |
218 | wxMenu *test_menu = new wxMenu; | |
017fcf32 VZ |
219 | test_menu->Append(INTERNAT_OPEN, _("&Open bogus file")); |
220 | test_menu->Append(INTERNAT_TEST, _("&Play a game")); | |
849a28d0 VS |
221 | test_menu->AppendSeparator(); |
222 | test_menu->Append(INTERNAT_TEST_1, _("&1 _() (gettext)")); | |
223 | test_menu->Append(INTERNAT_TEST_2, _("&2 _N() (ngettext)")); | |
4c3590e4 | 224 | test_menu->Append(INTERNAT_TEST_3, _("&3 wxTRANSLATE() (gettext_noop)")); |
aec18ff7 MB |
225 | |
226 | wxMenuBar *menu_bar = new wxMenuBar; | |
227 | menu_bar->Append(file_menu, _("&File")); | |
228 | menu_bar->Append(test_menu, _("&Test")); | |
229 | frame->SetMenuBar(menu_bar); | |
230 | ||
231 | // Show the frame | |
f13880a9 | 232 | frame->Show(true); |
aec18ff7 MB |
233 | SetTopWindow(frame); |
234 | ||
f13880a9 | 235 | return true; |
c801d85f KB |
236 | } |
237 | ||
017fcf32 VZ |
238 | // ---------------------------------------------------------------------------- |
239 | // MyFrame | |
240 | // ---------------------------------------------------------------------------- | |
241 | ||
242 | // main frame constructor | |
243 | MyFrame::MyFrame(wxLocale& locale) | |
244 | : wxFrame(NULL, | |
f13880a9 WS |
245 | wxID_ANY, |
246 | _("International wxWidgets App")), | |
017fcf32 | 247 | m_locale(locale) |
c7f3b78b | 248 | { |
aec18ff7 | 249 | // Empty |
c7f3b78b | 250 | } |
c801d85f | 251 | |
bd7d06f2 | 252 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) |
c801d85f | 253 | { |
f13880a9 | 254 | Close(true); |
c801d85f KB |
255 | } |
256 | ||
bd7d06f2 | 257 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 258 | { |
aec18ff7 | 259 | wxString localeInfo; |
2b5f62a0 VZ |
260 | wxString locale = m_locale.GetLocale(); |
261 | wxString sysname = m_locale.GetSysName(); | |
262 | wxString canname = m_locale.GetCanonicalName(); | |
263 | ||
f13880a9 | 264 | localeInfo.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"), |
2b5f62a0 | 265 | locale.c_str(), sysname.c_str(), canname.c_str() ); |
aec18ff7 | 266 | |
925e9792 WS |
267 | wxMessageDialog dlg( |
268 | this, | |
269 | wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart")) | |
270 | + wxT("\n\n") | |
271 | + localeInfo, | |
272 | _("About Internat"), | |
273 | wxOK | wxICON_INFORMATION | |
274 | ); | |
275 | dlg.ShowModal(); | |
c801d85f KB |
276 | } |
277 | ||
bd7d06f2 | 278 | void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event)) |
c7f3b78b | 279 | { |
085c26ac VZ |
280 | wxString str = wxGetTextFromUser |
281 | ( | |
282 | _("Enter your number:"), | |
283 | _("Try to guess my number!"), | |
284 | wxEmptyString, | |
285 | this | |
286 | ); | |
287 | ||
288 | if ( str.empty() ) | |
289 | { | |
290 | // cancelled | |
291 | return; | |
292 | } | |
aec18ff7 | 293 | |
085c26ac VZ |
294 | long num; |
295 | if ( !str.ToLong(&num) || num < 0 ) | |
296 | { | |
aec18ff7 | 297 | str = _("You've probably entered an invalid number."); |
085c26ac VZ |
298 | } |
299 | else if ( num == 9 ) | |
300 | { | |
301 | // this message is not translated (not in catalog) because we used _T() | |
302 | // and not _() around it | |
2b5f62a0 | 303 | str = _T("You've found a bug in this program!"); |
085c26ac VZ |
304 | } |
305 | else if ( num == 17 ) | |
aec18ff7 | 306 | { |
085c26ac VZ |
307 | str.clear(); |
308 | ||
309 | // string must be split in two -- otherwise the translation would't be | |
310 | // found | |
aec18ff7 MB |
311 | str << _("Congratulations! you've won. Here is the magic phrase:") |
312 | << _("cannot create fifo `%s'"); | |
313 | } | |
085c26ac VZ |
314 | else |
315 | { | |
316 | // this is a more implicit way to write _() but note that if you use it | |
317 | // you must ensure that the strings get extracted in the message | |
318 | // catalog as by default xgettext won't do it (it only knows of _(), | |
319 | // not wxGetTranslation()) | |
320 | str = wxGetTranslation(_T("Bad luck! try again...")); | |
321 | } | |
aec18ff7 MB |
322 | |
323 | wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION); | |
c7f3b78b VZ |
324 | } |
325 | ||
326 | void MyFrame::OnOpen(wxCommandEvent&) | |
327 | { | |
849a28d0 VS |
328 | // open a bogus file -- the error message should be also translated if |
329 | // you've got wxstd.mo somewhere in the search path | |
aec18ff7 | 330 | wxFile file(wxT("NOTEXIST.ING")); |
071cc2be | 331 | } |
085c26ac | 332 | |
849a28d0 VS |
333 | void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event)) |
334 | { | |
335 | const wxChar* title = _("Testing _() (gettext)"); | |
336 | wxTextEntryDialog d(this, _("Please enter text to translate"), | |
f13880a9 | 337 | title, wxTRANSLATE("default value")); |
849a28d0 VS |
338 | if (d.ShowModal() == wxID_OK) |
339 | { | |
f13880a9 WS |
340 | wxString v = d.GetValue(); |
341 | wxString s(title); | |
342 | s << _T("\n") << v << _T(" -> ") | |
343 | << wxGetTranslation(v.c_str()) << _T("\n"); | |
344 | wxMessageBox(s); | |
849a28d0 VS |
345 | } |
346 | } | |
347 | ||
348 | void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event)) | |
349 | { | |
350 | const wxChar* title = _("Testing _N() (ngettext)"); | |
351 | wxTextEntryDialog d(this, | |
f13880a9 WS |
352 | _("Please enter range for plural forms of \"n files deleted\" phrase"), |
353 | title, _T("0-10")); | |
849a28d0 VS |
354 | if (d.ShowModal() == wxID_OK) |
355 | { | |
f13880a9 WS |
356 | int first, last; |
357 | wxSscanf(d.GetValue(), _T("%d-%d"), &first, &last); | |
358 | wxString s(title); | |
359 | s << _T("\n"); | |
360 | for (int n = first; n <= last; ++n) | |
849a28d0 | 361 | { |
f13880a9 | 362 | s << n << _T(" ") << |
15d06954 | 363 | wxPLURAL("file deleted", "files deleted", n) << |
552973e0 | 364 | _T("\n"); |
f13880a9 | 365 | } |
849a28d0 VS |
366 | wxMessageBox(s); |
367 | } | |
368 | } | |
369 | ||
370 | void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event)) | |
371 | { | |
372 | const wxChar* lines[] = | |
373 | { | |
f13880a9 WS |
374 | wxTRANSLATE("line 1"), |
375 | wxTRANSLATE("line 2"), | |
376 | wxTRANSLATE("line 3"), | |
849a28d0 | 377 | }; |
4c3590e4 | 378 | wxString s(_("Testing wxTRANSLATE() (gettext_noop)")); |
849a28d0 VS |
379 | s << _T("\n"); |
380 | for (size_t i = 0; i < WXSIZEOF(lines); ++i) | |
381 | { | |
f13880a9 | 382 | s << lines[i] << _T(" -> ") << wxGetTranslation(lines[i]) << _T("\n"); |
849a28d0 VS |
383 | } |
384 | wxMessageBox(s); | |
385 | } | |
386 | ||
387 |