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