1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Demonstrates internationalisation (i18n) support
4 // Author: Vadim Zeitlin/Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // NOTE: don't miss the "readme.txt" file which comes with this sample!
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/cmdline.h"
40 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
41 #include "mondrian.xpm"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // Define a new application type
49 class MyApp
: public wxApp
52 MyApp() { m_lang
= wxLANGUAGE_UNKNOWN
; }
54 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
55 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
56 virtual bool OnInit();
59 wxLanguage m_lang
; // language specified by user
60 wxLocale m_locale
; // locale we'll be using
63 // Define a new frame type
64 class MyFrame
: public wxFrame
67 MyFrame(wxLocale
& m_locale
);
70 void OnTestLocaleAvail(wxCommandEvent
& event
);
71 void OnAbout(wxCommandEvent
& event
);
72 void OnQuit(wxCommandEvent
& event
);
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
);
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // ID for the menu commands
92 INTERNAT_TEST
= wxID_HIGHEST
+ 1,
100 static const wxLanguage langIds
[] =
107 wxLANGUAGE_BULGARIAN
,
111 #if wxUSE_UNICODE || defined(__WXMOTIF__)
117 wxLANGUAGE_ENGLISH_US
,
119 wxLANGUAGE_ARABIC_EGYPT
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
[] =
136 #if wxUSE_UNICODE || defined(__WXMOTIF__)
148 // the arrays must be in sync
149 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames
) == WXSIZEOF(langIds
),
150 LangArraysMismatch
);
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
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
)
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
)
170 // ============================================================================
172 // ============================================================================
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 // command line arguments handling
179 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
181 parser
.AddParam(_("locale"),
182 wxCMD_LINE_VAL_STRING
,
183 wxCMD_LINE_PARAM_OPTIONAL
);
185 wxApp::OnInitCmdLine(parser
);
188 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
190 if ( !wxApp::OnCmdLineParsed(parser
) )
193 if ( parser
.GetParamCount() )
195 const wxString loc
= parser
.GetParam();
196 const wxLanguageInfo
* const lang
= wxLocale::FindLanguageInfo(loc
);
199 wxLogError(_("Locale \"%s\" is unknown."), loc
);
203 m_lang
= static_cast<wxLanguage
>(lang
->Language
);
209 // `Main program' equivalent, creating windows and returning main app frame
212 if ( !wxApp::OnInit() )
215 if ( m_lang
== wxLANGUAGE_UNKNOWN
)
217 int lng
= wxGetSingleChoiceIndex
219 _("Please choose language:"),
224 m_lang
= lng
== -1 ? wxLANGUAGE_DEFAULT
: langIds
[lng
];
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
) )
231 wxLogWarning(_("This language is not supported by the system."));
233 // continue nevertheless
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
240 wxLocale::AddCatalogLookupPathPrefix(".");
242 // Initialize the catalogs we'll be using
243 const wxLanguageInfo
* pInfo
= wxLocale::GetLanguageInfo(m_lang
);
244 if (!m_locale
.AddCatalog("internat"))
245 wxLogError(_("Couldn't find/load the 'internat' catalog for locale '%s'."),
246 pInfo
? pInfo
->GetLocaleName() : _("unknown"));
248 // Now try to add wxstd.mo so that loading "NOTEXIST.ING" file will produce
249 // a localized error message:
250 m_locale
.AddCatalog("wxstd.mo");
251 // NOTE: it's not an error if we couldn't find it!
253 // this catalog is installed in standard location on Linux systems and
254 // shows that you may make use of the standard message catalogs as well
256 // if it's not installed on your system, it is just silently ignored
260 m_locale
.AddCatalog("fileutils");
264 // Create the main frame window
265 MyFrame
*frame
= new MyFrame(m_locale
);
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 // main frame constructor
279 MyFrame::MyFrame(wxLocale
& locale
)
282 _("International wxWidgets App")),
285 SetIcon(wxICON(mondrian
));
288 wxMenu
*file_menu
= new wxMenu
;
289 file_menu
->Append(INTERNAT_TEST
, _("&Test locale availability...\tCtrl-T"));
290 file_menu
->AppendSeparator();
292 // since wxID_ABOUT and wxID_EXIT are stock IDs they will automatically get
293 // translated help strings; nice isn't it?
294 file_menu
->Append(wxID_ABOUT
, _("&About..."));
295 file_menu
->AppendSeparator();
296 file_menu
->Append(wxID_EXIT
, _("E&xit"));
298 wxMenu
*test_menu
= new wxMenu
;
299 test_menu
->Append(wxID_OPEN
, _("&Open bogus file"), _("Shows a wxWidgets localized error message"));
300 test_menu
->Append(INTERNAT_PLAY
, _("&Play a game"), _("A little game; hint: 17 is a lucky number for many"));
301 test_menu
->AppendSeparator();
302 test_menu
->Append(INTERNAT_TEST_1
, _("&1 _() (gettext)"), _("Tests the _() macro"));
303 test_menu
->Append(INTERNAT_TEST_2
, _("&2 _N() (ngettext)"), _("Tests the _N() macro"));
304 test_menu
->Append(INTERNAT_TEST_3
, _("&3 wxTRANSLATE() (gettext_noop)"), _("Tests the wxTRANSLATE() macro"));
306 wxMenuBar
*menu_bar
= new wxMenuBar
;
307 menu_bar
->Append(file_menu
, _("&File"));
308 menu_bar
->Append(test_menu
, _("&Test"));
309 SetMenuBar(menu_bar
);
311 // this demonstrates RTL support in wxStatusBar:
314 // this demonstrates RTL layout mirroring for Arabic locales
315 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
316 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("First")),
317 wxSizerFlags().Border());
318 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("Second")),
319 wxSizerFlags().Border());
323 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
328 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
331 wxString locale
= m_locale
.GetLocale();
332 wxString sysname
= m_locale
.GetSysName();
333 wxString canname
= m_locale
.GetCanonicalName();
335 localeInfo
.Printf(_("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"),
336 locale
.c_str(), sysname
.c_str(), canname
.c_str() );
340 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
344 wxOK
| wxICON_INFORMATION
349 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
351 wxString str
= wxGetTextFromUser
353 _("Enter your number:"),
354 _("Try to guess my number!"),
366 if ( !str
.ToLong(&num
) || num
< 0 )
368 str
= _("You've probably entered an invalid number.");
372 // this message is not translated (not in catalog) because we used _T()
373 // and not _() around it
374 str
= _T("You've found a bug in this program!");
376 else if ( num
== 17 )
380 // string must be split in two -- otherwise the translation would't be
382 str
<< _("Congratulations! you've won. Here is the magic phrase:")
383 << _("cannot create fifo `%s'");
387 // this is a more implicit way to write _() but note that if you use it
388 // you must ensure that the strings get extracted in the message
389 // catalog as by default xgettext won't do it; it only knows of _(),
390 // not of wxTRANSLATE(). As internat's readme.txt says you should thus
391 // call xgettext with -kwxTRANSLATE.
392 str
= wxGetTranslation(wxTRANSLATE("Bad luck! try again..."));
394 // note also that if we want 'str' to contain a localized string
395 // we need to use wxGetTranslation explicitely as wxTRANSLATE just
396 // tells xgettext to extract the string but has no effect on the
397 // runtime of the program!
400 wxMessageBox(str
, _("Result"), wxOK
| wxICON_INFORMATION
);
403 void MyFrame::OnTestLocaleAvail(wxCommandEvent
& WXUNUSED(event
))
405 static wxString s_locale
;
406 wxString locale
= wxGetTextFromUser
408 _("Enter the locale to test"),
409 wxGetTextFromUserPromptStr
,
413 if ( locale
.empty() )
417 const wxLanguageInfo
* const info
= wxLocale::FindLanguageInfo(s_locale
);
420 wxLogError(_("Locale \"%s\" is unknown."), s_locale
.c_str());
424 if ( wxLocale::IsAvailable(info
->Language
) )
425 wxLogMessage(_("Locale \"%s\" is available."), s_locale
.c_str());
427 wxLogWarning(_("Locale \"%s\" is not available."), s_locale
.c_str());
430 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
432 // open a bogus file -- the error message should be also translated if
433 // you've got wxstd.mo somewhere in the search path (see MyApp::OnInit)
434 wxFile
file("NOTEXIST.ING");
437 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
439 const wxString
& title
= _("Testing _() (gettext)");
441 // NOTE: using the wxTRANSLATE() macro here we won't show a localized
442 // string in the text entry dialog; we'll simply show the un-translated
443 // string; however if the user press "ok" without altering the text,
444 // since the "default value" string has been extracted by xgettext
445 // the wxGetTranslation call later will manage to return a localized
447 wxTextEntryDialog
d(this, _("Please enter text to translate"),
448 title
, wxTRANSLATE("default value"));
450 if (d
.ShowModal() == wxID_OK
)
452 wxString v
= d
.GetValue();
454 s
<< "\n" << v
<< " -> "
455 << wxGetTranslation(v
.c_str()) << "\n";
460 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
462 const wxString
& title
= _("Testing _N() (ngettext)");
463 wxTextEntryDialog
d(this,
464 _("Please enter range for plural forms of \"n files deleted\" phrase"),
467 if (d
.ShowModal() == wxID_OK
)
470 wxSscanf(d
.GetValue(), "%d-%d", &first
, &last
);
473 for (int n
= first
; n
<= last
; ++n
)
476 wxPLURAL("file deleted", "files deleted", n
) <<
483 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
485 const char* lines
[] =
487 wxTRANSLATE("line 1"),
488 wxTRANSLATE("line 2"),
489 wxTRANSLATE("line 3"),
492 wxString
s(_("Testing wxTRANSLATE() (gettext_noop)"));
494 for (size_t i
= 0; i
< WXSIZEOF(lines
); ++i
)
496 s
<< lines
[i
] << " -> " << wxGetTranslation(lines
[i
]) << "\n";