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