]> git.saurik.com Git - wxWidgets.git/blob - samples/internat/internat.cpp
fix doxygen warnings
[wxWidgets.git] / samples / internat / internat.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: internat.cpp
3 // Purpose: Demonstrates internationalisation (i18n) support
4 // Author: Vadim Zeitlin/Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // NOTE: don't miss the "readme.txt" file which comes with this sample!
13
14
15
16 // ============================================================================
17 // declarations
18 // ============================================================================
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/wx.h"
33 #endif
34
35 #include "wx/intl.h"
36 #include "wx/file.h"
37 #include "wx/log.h"
38 #include "wx/cmdline.h"
39
40 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
41 #include "mondrian.xpm"
42 #endif
43
44 // ----------------------------------------------------------------------------
45 // private classes
46 // ----------------------------------------------------------------------------
47
48 // Define a new application type
49 class MyApp: public wxApp
50 {
51 public:
52 MyApp() { m_lang = wxLANGUAGE_UNKNOWN; }
53
54 virtual void OnInitCmdLine(wxCmdLineParser& parser);
55 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
56 virtual bool OnInit();
57
58 protected:
59 wxLanguage m_lang; // language specified by user
60 wxLocale m_locale; // locale we'll be using
61 };
62
63 // Define a new frame type
64 class MyFrame: public wxFrame
65 {
66 public:
67 MyFrame(wxLocale& m_locale);
68
69 public:
70 void OnTestLocaleAvail(wxCommandEvent& event);
71 void OnAbout(wxCommandEvent& event);
72 void OnQuit(wxCommandEvent& event);
73
74 void OnPlay(wxCommandEvent& event);
75 void OnOpen(wxCommandEvent& event);
76 void OnTest1(wxCommandEvent& event);
77 void OnTest2(wxCommandEvent& event);
78 void OnTest3(wxCommandEvent& event);
79
80 DECLARE_EVENT_TABLE()
81
82 wxLocale& m_locale;
83 };
84
85 // ----------------------------------------------------------------------------
86 // constants
87 // ----------------------------------------------------------------------------
88
89 // ID for the menu commands
90 enum
91 {
92 INTERNAT_TEST = wxID_HIGHEST + 1,
93 INTERNAT_PLAY,
94 INTERNAT_TEST_1,
95 INTERNAT_TEST_2,
96 INTERNAT_TEST_3
97 };
98
99 // language data
100 static const wxLanguage langIds[] =
101 {
102 wxLANGUAGE_DEFAULT,
103 wxLANGUAGE_FRENCH,
104 wxLANGUAGE_ITALIAN,
105 wxLANGUAGE_GERMAN,
106 wxLANGUAGE_RUSSIAN,
107 wxLANGUAGE_BULGARIAN,
108 wxLANGUAGE_CZECH,
109 wxLANGUAGE_POLISH,
110 wxLANGUAGE_SWEDISH,
111 #if wxUSE_UNICODE || defined(__WXMOTIF__)
112 wxLANGUAGE_JAPANESE,
113 #endif
114 #if wxUSE_UNICODE
115 wxLANGUAGE_GEORGIAN,
116 wxLANGUAGE_ENGLISH,
117 wxLANGUAGE_ENGLISH_US,
118 wxLANGUAGE_ARABIC,
119 wxLANGUAGE_ARABIC_EGYPT
120 #endif
121 };
122
123 // note that it makes no sense to translate these strings, they are
124 // shown before we set the locale anyhow
125 const wxString langNames[] =
126 {
127 "System default",
128 "French",
129 "Italian",
130 "German",
131 "Russian",
132 "Bulgarian",
133 "Czech",
134 "Polish",
135 "Swedish",
136 #if wxUSE_UNICODE || defined(__WXMOTIF__)
137 "Japanese",
138 #endif
139 #if wxUSE_UNICODE
140 "Georgian",
141 "English",
142 "English (U.S.)",
143 "Arabic",
144 "Arabic (Egypt)"
145 #endif
146 };
147
148 // the arrays must be in sync
149 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
150 LangArraysMismatch );
151
152 // ----------------------------------------------------------------------------
153 // wxWidgets macros
154 // ----------------------------------------------------------------------------
155
156 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
157 EVT_MENU(INTERNAT_TEST, MyFrame::OnTestLocaleAvail)
158 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
159 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
160
161 EVT_MENU(INTERNAT_PLAY, MyFrame::OnPlay)
162 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
163 EVT_MENU(INTERNAT_TEST_1, MyFrame::OnTest1)
164 EVT_MENU(INTERNAT_TEST_2, MyFrame::OnTest2)
165 EVT_MENU(INTERNAT_TEST_3, MyFrame::OnTest3)
166 END_EVENT_TABLE()
167
168 IMPLEMENT_APP(MyApp)
169
170 // ============================================================================
171 // implementation
172 // ============================================================================
173
174 // ----------------------------------------------------------------------------
175 // MyApp
176 // ----------------------------------------------------------------------------
177
178 // command line arguments handling
179 void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
180 {
181 parser.AddParam(_("locale"),
182 wxCMD_LINE_VAL_STRING,
183 wxCMD_LINE_PARAM_OPTIONAL);
184
185 wxApp::OnInitCmdLine(parser);
186 }
187
188 bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
189 {
190 if ( !wxApp::OnCmdLineParsed(parser) )
191 return false;
192
193 if ( parser.GetParamCount() )
194 {
195 const wxString loc = parser.GetParam();
196 const wxLanguageInfo * const lang = wxLocale::FindLanguageInfo(loc);
197 if ( !lang )
198 {
199 wxLogError(_("Locale \"%s\" is unknown."), loc);
200 return false;
201 }
202
203 m_lang = static_cast<wxLanguage>(lang->Language);
204 }
205
206 return true;
207 }
208
209 // `Main program' equivalent, creating windows and returning main app frame
210 bool MyApp::OnInit()
211 {
212 if ( !wxApp::OnInit() )
213 return false;
214
215 if ( m_lang == wxLANGUAGE_UNKNOWN )
216 {
217 int lng = wxGetSingleChoiceIndex
218 (
219 _("Please choose language:"),
220 _("Language"),
221 WXSIZEOF(langNames),
222 langNames
223 );
224 m_lang = lng == -1 ? wxLANGUAGE_DEFAULT : langIds[lng];
225 }
226
227 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
228 // false just because it failed to load wxstd catalog
229 if ( !m_locale.Init(m_lang, wxLOCALE_CONV_ENCODING) )
230 {
231 wxLogWarning(_("This language is not supported by the system."));
232
233 // continue nevertheless
234 }
235
236 // normally this wouldn't be necessary as the catalog files would be found
237 // in the default locations, but when the program is not installed the
238 // catalogs are in the build directory where we wouldn't find them by
239 // default
240 wxLocale::AddCatalogLookupPathPrefix(".");
241
242 // Initialize the catalogs we'll be using
243 if (!m_locale.AddCatalog("internat"))
244 wxLogError(_("Couldn't find/load the 'internat' catalog."));
245
246 // Now try to add wxstd.mo so that loading "NOTEXIST.ING" file will produce
247 // a localized error message:
248 m_locale.AddCatalog("wxstd.mo");
249 // NOTE: it's not an error if we couldn't find it!
250
251 // this catalog is installed in standard location on Linux systems and
252 // shows that you may make use of the standard message catalogs as well
253 //
254 // if it's not installed on your system, it is just silently ignored
255 #ifdef __LINUX__
256 {
257 wxLogNull noLog;
258 m_locale.AddCatalog("fileutils");
259 }
260 #endif
261
262 // Create the main frame window
263 MyFrame *frame = new MyFrame(m_locale);
264
265 // Show the frame
266 frame->Show(true);
267 SetTopWindow(frame);
268
269 return true;
270 }
271
272 // ----------------------------------------------------------------------------
273 // MyFrame
274 // ----------------------------------------------------------------------------
275
276 // main frame constructor
277 MyFrame::MyFrame(wxLocale& locale)
278 : wxFrame(NULL,
279 wxID_ANY,
280 _("International wxWidgets App")),
281 m_locale(locale)
282 {
283 SetIcon(wxICON(mondrian));
284
285 // Make a menubar
286 wxMenu *file_menu = new wxMenu;
287 file_menu->Append(INTERNAT_TEST, _("&Test locale availability...\tCtrl-T"));
288 file_menu->AppendSeparator();
289
290 // since wxID_ABOUT and wxID_EXIT are stock IDs they will automatically get
291 // translated help strings; nice isn't it?
292 file_menu->Append(wxID_ABOUT, _("&About..."));
293 file_menu->AppendSeparator();
294 file_menu->Append(wxID_EXIT, _("E&xit"));
295
296 wxMenu *test_menu = new wxMenu;
297 test_menu->Append(wxID_OPEN, _("&Open bogus file"), _("Shows a wxWidgets localized error message"));
298 test_menu->Append(INTERNAT_PLAY, _("&Play a game"), _("A little game; hint: 17 is a lucky number for many"));
299 test_menu->AppendSeparator();
300 test_menu->Append(INTERNAT_TEST_1, _("&1 _() (gettext)"), _("Tests the _() macro"));
301 test_menu->Append(INTERNAT_TEST_2, _("&2 _N() (ngettext)"), _("Tests the _N() macro"));
302 test_menu->Append(INTERNAT_TEST_3, _("&3 wxTRANSLATE() (gettext_noop)"), _("Tests the wxTRANSLATE() macro"));
303
304 wxMenuBar *menu_bar = new wxMenuBar;
305 menu_bar->Append(file_menu, _("&File"));
306 menu_bar->Append(test_menu, _("&Test"));
307 SetMenuBar(menu_bar);
308
309 // this demonstrates RTL support in wxStatusBar:
310 CreateStatusBar(1);
311
312 // this demonstrates RTL layout mirroring for Arabic locales
313 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
314 sizer->Add(new wxStaticText(this, wxID_ANY, _("First")),
315 wxSizerFlags().Border());
316 sizer->Add(new wxStaticText(this, wxID_ANY, _("Second")),
317 wxSizerFlags().Border());
318 SetSizer(sizer);
319 }
320
321 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
322 {
323 Close(true);
324 }
325
326 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
327 {
328 wxString localeInfo;
329 wxString locale = m_locale.GetLocale();
330 wxString sysname = m_locale.GetSysName();
331 wxString canname = m_locale.GetCanonicalName();
332
333 localeInfo.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
334 locale.c_str(), sysname.c_str(), canname.c_str() );
335
336 wxMessageDialog dlg(
337 this,
338 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
339 + "\n\n"
340 + localeInfo,
341 _("About Internat"),
342 wxOK | wxICON_INFORMATION
343 );
344 dlg.ShowModal();
345 }
346
347 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
348 {
349 wxString str = wxGetTextFromUser
350 (
351 _("Enter your number:"),
352 _("Try to guess my number!"),
353 wxEmptyString,
354 this
355 );
356
357 if ( str.empty() )
358 {
359 // cancelled
360 return;
361 }
362
363 long num;
364 if ( !str.ToLong(&num) || num < 0 )
365 {
366 str = _("You've probably entered an invalid number.");
367 }
368 else if ( num == 9 )
369 {
370 // this message is not translated (not in catalog) because we used _T()
371 // and not _() around it
372 str = _T("You've found a bug in this program!");
373 }
374 else if ( num == 17 )
375 {
376 str.clear();
377
378 // string must be split in two -- otherwise the translation would't be
379 // found
380 str << _("Congratulations! you've won. Here is the magic phrase:")
381 << _("cannot create fifo `%s'");
382 }
383 else
384 {
385 // this is a more implicit way to write _() but note that if you use it
386 // you must ensure that the strings get extracted in the message
387 // catalog as by default xgettext won't do it; it only knows of _(),
388 // not of wxTRANSLATE(). As internat's readme.txt says you should thus
389 // call xgettext with -kwxTRANSLATE.
390 str = wxGetTranslation(wxTRANSLATE("Bad luck! try again..."));
391
392 // note also that if we want 'str' to contain a localized string
393 // we need to use wxGetTranslation explicitely as wxTRANSLATE just
394 // tells xgettext to extract the string but has no effect on the
395 // runtime of the program!
396 }
397
398 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
399 }
400
401 void MyFrame::OnTestLocaleAvail(wxCommandEvent& WXUNUSED(event))
402 {
403 static wxString s_locale;
404 wxString locale = wxGetTextFromUser
405 (
406 _("Enter the locale to test"),
407 wxGetTextFromUserPromptStr,
408 s_locale,
409 this
410 );
411 if ( locale.empty() )
412 return;
413
414 s_locale = locale;
415 const wxLanguageInfo * const info = wxLocale::FindLanguageInfo(s_locale);
416 if ( !info )
417 {
418 wxLogError(_("Locale \"%s\" is unknown."), s_locale.c_str());
419 return;
420 }
421
422 if ( wxLocale::IsAvailable(info->Language) )
423 wxLogMessage(_("Locale \"%s\" is available."), s_locale.c_str());
424 else
425 wxLogWarning(_("Locale \"%s\" is not available."), s_locale.c_str());
426 }
427
428 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
429 {
430 // open a bogus file -- the error message should be also translated if
431 // you've got wxstd.mo somewhere in the search path (see MyApp::OnInit)
432 wxFile file("NOTEXIST.ING");
433 }
434
435 void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
436 {
437 const wxString& title = _("Testing _() (gettext)");
438
439 // NOTE: using the wxTRANSLATE() macro here we won't show a localized
440 // string in the text entry dialog; we'll simply show the un-translated
441 // string; however if the user press "ok" without altering the text,
442 // since the "default value" string has been extracted by xgettext
443 // the wxGetTranslation call later will manage to return a localized
444 // string
445 wxTextEntryDialog d(this, _("Please enter text to translate"),
446 title, wxTRANSLATE("default value"));
447
448 if (d.ShowModal() == wxID_OK)
449 {
450 wxString v = d.GetValue();
451 wxString s(title);
452 s << "\n" << v << " -> "
453 << wxGetTranslation(v.c_str()) << "\n";
454 wxMessageBox(s);
455 }
456 }
457
458 void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
459 {
460 const wxString& title = _("Testing _N() (ngettext)");
461 wxTextEntryDialog d(this,
462 _("Please enter range for plural forms of \"n files deleted\" phrase"),
463 title, "0-10");
464
465 if (d.ShowModal() == wxID_OK)
466 {
467 int first, last;
468 wxSscanf(d.GetValue(), "%d-%d", &first, &last);
469 wxString s(title);
470 s << "\n";
471 for (int n = first; n <= last; ++n)
472 {
473 s << n << " " <<
474 wxPLURAL("file deleted", "files deleted", n) <<
475 "\n";
476 }
477 wxMessageBox(s);
478 }
479 }
480
481 void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
482 {
483 const char* lines[] =
484 {
485 wxTRANSLATE("line 1"),
486 wxTRANSLATE("line 2"),
487 wxTRANSLATE("line 3"),
488 };
489
490 wxString s(_("Testing wxTRANSLATE() (gettext_noop)"));
491 s << "\n";
492 for (size_t i = 0; i < WXSIZEOF(lines); ++i)
493 {
494 s << lines[i] << " -> " << wxGetTranslation(lines[i]) << "\n";
495 }
496 wxMessageBox(s);
497 }
498
499