1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Demonstrates internationalisation (i18n) support
4 // Author: Vadim Zeitlin/Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // NOTE: don't miss the "readme.txt" file which comes with this sample!
15 // ============================================================================
17 // ============================================================================
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include "wx/wxprec.h"
37 #include "wx/cmdline.h"
39 #ifndef wxHAS_IMAGES_IN_RESOURCES
40 #include "../sample.xpm"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // Define a new application type
48 class MyApp
: public wxApp
51 MyApp() { m_lang
= wxLANGUAGE_UNKNOWN
; }
53 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
54 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
55 virtual bool OnInit();
58 wxLanguage m_lang
; // language specified by user
59 wxLocale m_locale
; // locale we'll be using
62 // Define a new frame type
63 class MyFrame
: public wxFrame
66 MyFrame(wxLocale
& m_locale
);
69 void OnTestLocaleAvail(wxCommandEvent
& event
);
70 void OnAbout(wxCommandEvent
& event
);
71 void OnQuit(wxCommandEvent
& event
);
73 void OnPlay(wxCommandEvent
& event
);
74 void OnOpen(wxCommandEvent
& event
);
75 void OnTest1(wxCommandEvent
& event
);
76 void OnTest2(wxCommandEvent
& event
);
77 void OnTest3(wxCommandEvent
& event
);
78 void OnTestMsgBox(wxCommandEvent
& event
);
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // ID for the menu commands
92 INTERNAT_TEST
= wxID_HIGHEST
+ 1,
101 static const wxLanguage langIds
[] =
108 wxLANGUAGE_BULGARIAN
,
112 #if wxUSE_UNICODE || defined(__WXMOTIF__)
118 wxLANGUAGE_ENGLISH_US
,
120 wxLANGUAGE_ARABIC_EGYPT
124 // note that it makes no sense to translate these strings, they are
125 // shown before we set the locale anyhow
126 const wxString langNames
[] =
137 #if wxUSE_UNICODE || defined(__WXMOTIF__)
149 // the arrays must be in sync
150 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames
) == WXSIZEOF(langIds
),
151 LangArraysMismatch
);
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
158 EVT_MENU(INTERNAT_TEST
, MyFrame::OnTestLocaleAvail
)
159 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
160 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
162 EVT_MENU(INTERNAT_PLAY
, MyFrame::OnPlay
)
163 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
164 EVT_MENU(INTERNAT_TEST_1
, MyFrame::OnTest1
)
165 EVT_MENU(INTERNAT_TEST_2
, MyFrame::OnTest2
)
166 EVT_MENU(INTERNAT_TEST_3
, MyFrame::OnTest3
)
167 EVT_MENU(INTERNAT_TEST_MSGBOX
, MyFrame::OnTestMsgBox
)
172 // ============================================================================
174 // ============================================================================
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 // command line arguments handling
181 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
183 parser
.AddParam(_("locale"),
184 wxCMD_LINE_VAL_STRING
,
185 wxCMD_LINE_PARAM_OPTIONAL
);
187 wxApp::OnInitCmdLine(parser
);
190 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
192 if ( !wxApp::OnCmdLineParsed(parser
) )
195 if ( parser
.GetParamCount() )
197 const wxString loc
= parser
.GetParam();
198 const wxLanguageInfo
* const lang
= wxLocale::FindLanguageInfo(loc
);
201 wxLogError(_("Locale \"%s\" is unknown."), loc
);
205 m_lang
= static_cast<wxLanguage
>(lang
->Language
);
211 // `Main program' equivalent, creating windows and returning main app frame
214 if ( !wxApp::OnInit() )
217 if ( m_lang
== wxLANGUAGE_UNKNOWN
)
219 int lng
= wxGetSingleChoiceIndex
221 _("Please choose language:"),
226 m_lang
= lng
== -1 ? wxLANGUAGE_DEFAULT
: langIds
[lng
];
229 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
230 // false just because it failed to load wxstd catalog
231 if ( !m_locale
.Init(m_lang
, wxLOCALE_DONT_LOAD_DEFAULT
) )
233 wxLogWarning(_("This language is not supported by the system."));
235 // continue nevertheless
238 // normally this wouldn't be necessary as the catalog files would be found
239 // in the default locations, but when the program is not installed the
240 // catalogs are in the build directory where we wouldn't find them by
242 wxLocale::AddCatalogLookupPathPrefix(".");
244 // Initialize the catalogs we'll be using
245 const wxLanguageInfo
* pInfo
= wxLocale::GetLanguageInfo(m_lang
);
246 if (!m_locale
.AddCatalog("internat"))
248 wxLogError(_("Couldn't find/load the 'internat' catalog for locale '%s'."),
249 pInfo
? pInfo
->GetLocaleName() : _("unknown"));
252 // Now try to add wxstd.mo so that loading "NOTEXIST.ING" file will produce
253 // a localized error message:
254 m_locale
.AddCatalog("wxstd");
255 // NOTE: it's not an error if we couldn't find it!
257 // this catalog is installed in standard location on Linux systems and
258 // shows that you may make use of the standard message catalogs as well
260 // if it's not installed on your system, it is just silently ignored
264 m_locale
.AddCatalog("fileutils");
268 // Create the main frame window
269 MyFrame
*frame
= new MyFrame(m_locale
);
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 // main frame constructor
282 MyFrame::MyFrame(wxLocale
& locale
)
285 _("International wxWidgets App")),
288 SetIcon(wxICON(sample
));
291 wxMenu
*file_menu
= new wxMenu
;
292 file_menu
->Append(INTERNAT_TEST
, _("&Test locale availability...\tCtrl-T"));
293 file_menu
->AppendSeparator();
295 // since wxID_ABOUT and wxID_EXIT are stock IDs they will automatically get
296 // translated help strings; nice isn't it?
297 file_menu
->Append(wxID_ABOUT
, _("&About"));
298 file_menu
->AppendSeparator();
299 file_menu
->Append(wxID_EXIT
, _("E&xit"));
301 wxMenu
*test_menu
= new wxMenu
;
302 test_menu
->Append(wxID_OPEN
, _("&Open bogus file"), _("Shows a wxWidgets localized error message"));
303 test_menu
->Append(INTERNAT_PLAY
, _("&Play a game"), _("A little game; hint: 17 is a lucky number for many"));
304 test_menu
->AppendSeparator();
305 test_menu
->Append(INTERNAT_TEST_1
, _("&1 _() (gettext)"), _("Tests the _() macro"));
306 test_menu
->Append(INTERNAT_TEST_2
, _("&2 _N() (ngettext)"), _("Tests the _N() macro"));
307 test_menu
->Append(INTERNAT_TEST_3
, _("&3 wxTRANSLATE() (gettext_noop)"), _("Tests the wxTRANSLATE() macro"));
308 test_menu
->Append(INTERNAT_TEST_MSGBOX
, _("&Message box test"),
309 _("Tests message box buttons labels translation"));
311 wxMenuBar
*menu_bar
= new wxMenuBar
;
312 menu_bar
->Append(file_menu
, _("&File"));
313 menu_bar
->Append(test_menu
, _("&Test"));
314 SetMenuBar(menu_bar
);
316 // this demonstrates RTL support in wxStatusBar:
319 // this demonstrates RTL layout mirroring for Arabic locales
320 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
321 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("First")),
322 wxSizerFlags().Border());
323 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("Second")),
324 wxSizerFlags().Border());
328 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
333 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
336 wxString locale
= m_locale
.GetLocale();
337 wxString sysname
= m_locale
.GetSysName();
338 wxString canname
= m_locale
.GetCanonicalName();
340 localeInfo
.Printf(_("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"),
341 locale
.c_str(), sysname
.c_str(), canname
.c_str() );
345 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
349 wxOK
| wxICON_INFORMATION
354 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
356 wxString str
= wxGetTextFromUser
358 _("Enter your number:"),
359 _("Try to guess my number!"),
371 if ( !str
.ToLong(&num
) || num
< 0 )
373 str
= _("You've probably entered an invalid number.");
377 // this message is not translated (not in catalog) because we used wxT()
378 // and not _() around it
379 str
= wxT("You've found a bug in this program!");
381 else if ( num
== 17 )
385 // string must be split in two -- otherwise the translation would't be
387 str
<< _("Congratulations! you've won. Here is the magic phrase:")
388 << _("cannot create fifo `%s'");
392 // this is a more implicit way to write _() but note that if you use it
393 // you must ensure that the strings get extracted in the message
394 // catalog as by default xgettext won't do it; it only knows of _(),
395 // not of wxTRANSLATE(). As internat's readme.txt says you should thus
396 // call xgettext with -kwxTRANSLATE.
397 str
= wxGetTranslation(wxTRANSLATE("Bad luck! try again..."));
399 // note also that if we want 'str' to contain a localized string
400 // we need to use wxGetTranslation explicitly as wxTRANSLATE just
401 // tells xgettext to extract the string but has no effect on the
402 // runtime of the program!
405 wxMessageBox(str
, _("Result"), wxOK
| wxICON_INFORMATION
);
408 void MyFrame::OnTestLocaleAvail(wxCommandEvent
& WXUNUSED(event
))
410 static wxString s_locale
;
411 wxString locale
= wxGetTextFromUser
413 _("Enter the locale to test"),
414 wxGetTextFromUserPromptStr
,
418 if ( locale
.empty() )
422 const wxLanguageInfo
* const info
= wxLocale::FindLanguageInfo(s_locale
);
425 wxLogError(_("Locale \"%s\" is unknown."), s_locale
.c_str());
429 if ( wxLocale::IsAvailable(info
->Language
) )
431 wxLogMessage(_("Locale \"%s\" is available."), s_locale
.c_str());
435 wxLogWarning(_("Locale \"%s\" is not available."), s_locale
.c_str());
439 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
441 // open a bogus file -- the error message should be also translated if
442 // you've got wxstd.mo somewhere in the search path (see MyApp::OnInit)
443 wxFile
file("NOTEXIST.ING");
446 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
448 const wxString
& title
= _("Testing _() (gettext)");
450 // NOTE: using the wxTRANSLATE() macro here we won't show a localized
451 // string in the text entry dialog; we'll simply show the un-translated
452 // string; however if the user press "ok" without altering the text,
453 // since the "default value" string has been extracted by xgettext
454 // the wxGetTranslation call later will manage to return a localized
456 wxTextEntryDialog
d(this, _("Please enter text to translate"),
457 title
, wxTRANSLATE("default value"));
459 if (d
.ShowModal() == wxID_OK
)
461 wxString v
= d
.GetValue();
463 s
<< "\n" << v
<< " -> "
464 << wxGetTranslation(v
.c_str()) << "\n";
469 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
471 const wxString
& title
= _("Testing _N() (ngettext)");
472 wxTextEntryDialog
d(this,
473 _("Please enter range for plural forms of \"n files deleted\" phrase"),
476 if (d
.ShowModal() == wxID_OK
)
479 wxSscanf(d
.GetValue(), "%d-%d", &first
, &last
);
482 for (int n
= first
; n
<= last
; ++n
)
485 wxPLURAL("file deleted", "files deleted", n
) <<
492 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
494 const char* lines
[] =
496 wxTRANSLATE("line 1"),
497 wxTRANSLATE("line 2"),
498 wxTRANSLATE("line 3"),
501 wxString
s(_("Testing wxTRANSLATE() (gettext_noop)"));
503 for (size_t i
= 0; i
< WXSIZEOF(lines
); ++i
)
505 s
<< lines
[i
] << " -> " << wxGetTranslation(lines
[i
]) << "\n";
510 void MyFrame::OnTestMsgBox(wxCommandEvent
& WXUNUSED(event
))
514 _("Are the labels of the buttons in this message box "
515 "translated into the current locale language?"),
516 _("wxWidgets i18n sample"),
521 wxMessageBox(_("Please report the details of your platform to us."));