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