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(__WXMSW__) && !defined(__WXPM__)
41 #include "../sample.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
);
79 void OnTestMsgBox(wxCommandEvent
& event
);
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // ID for the menu commands
93 INTERNAT_TEST
= wxID_HIGHEST
+ 1,
102 static const wxLanguage langIds
[] =
109 wxLANGUAGE_BULGARIAN
,
113 #if wxUSE_UNICODE || defined(__WXMOTIF__)
119 wxLANGUAGE_ENGLISH_US
,
121 wxLANGUAGE_ARABIC_EGYPT
125 // note that it makes no sense to translate these strings, they are
126 // shown before we set the locale anyhow
127 const wxString langNames
[] =
138 #if wxUSE_UNICODE || defined(__WXMOTIF__)
150 // the arrays must be in sync
151 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames
) == WXSIZEOF(langIds
),
152 LangArraysMismatch
);
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
159 EVT_MENU(INTERNAT_TEST
, MyFrame::OnTestLocaleAvail
)
160 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
161 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
163 EVT_MENU(INTERNAT_PLAY
, MyFrame::OnPlay
)
164 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
165 EVT_MENU(INTERNAT_TEST_1
, MyFrame::OnTest1
)
166 EVT_MENU(INTERNAT_TEST_2
, MyFrame::OnTest2
)
167 EVT_MENU(INTERNAT_TEST_3
, MyFrame::OnTest3
)
168 EVT_MENU(INTERNAT_TEST_MSGBOX
, MyFrame::OnTestMsgBox
)
173 // ============================================================================
175 // ============================================================================
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 // command line arguments handling
182 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
184 parser
.AddParam(_("locale"),
185 wxCMD_LINE_VAL_STRING
,
186 wxCMD_LINE_PARAM_OPTIONAL
);
188 wxApp::OnInitCmdLine(parser
);
191 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
193 if ( !wxApp::OnCmdLineParsed(parser
) )
196 if ( parser
.GetParamCount() )
198 const wxString loc
= parser
.GetParam();
199 const wxLanguageInfo
* const lang
= wxLocale::FindLanguageInfo(loc
);
202 wxLogError(_("Locale \"%s\" is unknown."), loc
);
206 m_lang
= static_cast<wxLanguage
>(lang
->Language
);
212 // `Main program' equivalent, creating windows and returning main app frame
215 if ( !wxApp::OnInit() )
218 if ( m_lang
== wxLANGUAGE_UNKNOWN
)
220 int lng
= wxGetSingleChoiceIndex
222 _("Please choose language:"),
227 m_lang
= lng
== -1 ? wxLANGUAGE_DEFAULT
: langIds
[lng
];
230 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
231 // false just because it failed to load wxstd catalog
232 if ( !m_locale
.Init(m_lang
, wxLOCALE_DONT_LOAD_DEFAULT
) )
234 wxLogWarning(_("This language is not supported by the system."));
236 // continue nevertheless
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
243 wxLocale::AddCatalogLookupPathPrefix(".");
245 // Initialize the catalogs we'll be using
246 const wxLanguageInfo
* pInfo
= wxLocale::GetLanguageInfo(m_lang
);
247 if (!m_locale
.AddCatalog("internat"))
249 wxLogError(_("Couldn't find/load the 'internat' catalog for locale '%s'."),
250 pInfo
? pInfo
->GetLocaleName() : _("unknown"));
253 // Now try to add wxstd.mo so that loading "NOTEXIST.ING" file will produce
254 // a localized error message:
255 m_locale
.AddCatalog("wxstd");
256 // NOTE: it's not an error if we couldn't find it!
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
261 // if it's not installed on your system, it is just silently ignored
265 m_locale
.AddCatalog("fileutils");
269 // Create the main frame window
270 MyFrame
*frame
= new MyFrame(m_locale
);
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 // main frame constructor
284 MyFrame::MyFrame(wxLocale
& locale
)
287 _("International wxWidgets App")),
290 SetIcon(wxICON(sample
));
293 wxMenu
*file_menu
= new wxMenu
;
294 file_menu
->Append(INTERNAT_TEST
, _("&Test locale availability...\tCtrl-T"));
295 file_menu
->AppendSeparator();
297 // since wxID_ABOUT and wxID_EXIT are stock IDs they will automatically get
298 // translated help strings; nice isn't it?
299 file_menu
->Append(wxID_ABOUT
, _("&About..."));
300 file_menu
->AppendSeparator();
301 file_menu
->Append(wxID_EXIT
, _("E&xit"));
303 wxMenu
*test_menu
= new wxMenu
;
304 test_menu
->Append(wxID_OPEN
, _("&Open bogus file"), _("Shows a wxWidgets localized error message"));
305 test_menu
->Append(INTERNAT_PLAY
, _("&Play a game"), _("A little game; hint: 17 is a lucky number for many"));
306 test_menu
->AppendSeparator();
307 test_menu
->Append(INTERNAT_TEST_1
, _("&1 _() (gettext)"), _("Tests the _() macro"));
308 test_menu
->Append(INTERNAT_TEST_2
, _("&2 _N() (ngettext)"), _("Tests the _N() macro"));
309 test_menu
->Append(INTERNAT_TEST_3
, _("&3 wxTRANSLATE() (gettext_noop)"), _("Tests the wxTRANSLATE() macro"));
310 test_menu
->Append(INTERNAT_TEST_MSGBOX
, _("&Message box test"),
311 _("Tests message box buttons labels translation"));
313 wxMenuBar
*menu_bar
= new wxMenuBar
;
314 menu_bar
->Append(file_menu
, _("&File"));
315 menu_bar
->Append(test_menu
, _("&Test"));
316 SetMenuBar(menu_bar
);
318 // this demonstrates RTL support in wxStatusBar:
321 // this demonstrates RTL layout mirroring for Arabic locales
322 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
323 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("First")),
324 wxSizerFlags().Border());
325 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("Second")),
326 wxSizerFlags().Border());
330 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
335 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
338 wxString locale
= m_locale
.GetLocale();
339 wxString sysname
= m_locale
.GetSysName();
340 wxString canname
= m_locale
.GetCanonicalName();
342 localeInfo
.Printf(_("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"),
343 locale
.c_str(), sysname
.c_str(), canname
.c_str() );
347 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
351 wxOK
| wxICON_INFORMATION
356 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
358 wxString str
= wxGetTextFromUser
360 _("Enter your number:"),
361 _("Try to guess my number!"),
373 if ( !str
.ToLong(&num
) || num
< 0 )
375 str
= _("You've probably entered an invalid number.");
379 // this message is not translated (not in catalog) because we used wxT()
380 // and not _() around it
381 str
= wxT("You've found a bug in this program!");
383 else if ( num
== 17 )
387 // string must be split in two -- otherwise the translation would't be
389 str
<< _("Congratulations! you've won. Here is the magic phrase:")
390 << _("cannot create fifo `%s'");
394 // this is a more implicit way to write _() but note that if you use it
395 // you must ensure that the strings get extracted in the message
396 // catalog as by default xgettext won't do it; it only knows of _(),
397 // not of wxTRANSLATE(). As internat's readme.txt says you should thus
398 // call xgettext with -kwxTRANSLATE.
399 str
= wxGetTranslation(wxTRANSLATE("Bad luck! try again..."));
401 // note also that if we want 'str' to contain a localized string
402 // we need to use wxGetTranslation explicitely as wxTRANSLATE just
403 // tells xgettext to extract the string but has no effect on the
404 // runtime of the program!
407 wxMessageBox(str
, _("Result"), wxOK
| wxICON_INFORMATION
);
410 void MyFrame::OnTestLocaleAvail(wxCommandEvent
& WXUNUSED(event
))
412 static wxString s_locale
;
413 wxString locale
= wxGetTextFromUser
415 _("Enter the locale to test"),
416 wxGetTextFromUserPromptStr
,
420 if ( locale
.empty() )
424 const wxLanguageInfo
* const info
= wxLocale::FindLanguageInfo(s_locale
);
427 wxLogError(_("Locale \"%s\" is unknown."), s_locale
.c_str());
431 if ( wxLocale::IsAvailable(info
->Language
) )
433 wxLogMessage(_("Locale \"%s\" is available."), s_locale
.c_str());
437 wxLogWarning(_("Locale \"%s\" is not available."), s_locale
.c_str());
441 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
443 // open a bogus file -- the error message should be also translated if
444 // you've got wxstd.mo somewhere in the search path (see MyApp::OnInit)
445 wxFile
file("NOTEXIST.ING");
448 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
450 const wxString
& title
= _("Testing _() (gettext)");
452 // NOTE: using the wxTRANSLATE() macro here we won't show a localized
453 // string in the text entry dialog; we'll simply show the un-translated
454 // string; however if the user press "ok" without altering the text,
455 // since the "default value" string has been extracted by xgettext
456 // the wxGetTranslation call later will manage to return a localized
458 wxTextEntryDialog
d(this, _("Please enter text to translate"),
459 title
, wxTRANSLATE("default value"));
461 if (d
.ShowModal() == wxID_OK
)
463 wxString v
= d
.GetValue();
465 s
<< "\n" << v
<< " -> "
466 << wxGetTranslation(v
.c_str()) << "\n";
471 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
473 const wxString
& title
= _("Testing _N() (ngettext)");
474 wxTextEntryDialog
d(this,
475 _("Please enter range for plural forms of \"n files deleted\" phrase"),
478 if (d
.ShowModal() == wxID_OK
)
481 wxSscanf(d
.GetValue(), "%d-%d", &first
, &last
);
484 for (int n
= first
; n
<= last
; ++n
)
487 wxPLURAL("file deleted", "files deleted", n
) <<
494 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
496 const char* lines
[] =
498 wxTRANSLATE("line 1"),
499 wxTRANSLATE("line 2"),
500 wxTRANSLATE("line 3"),
503 wxString
s(_("Testing wxTRANSLATE() (gettext_noop)"));
505 for (size_t i
= 0; i
< WXSIZEOF(lines
); ++i
)
507 s
<< lines
[i
] << " -> " << wxGetTranslation(lines
[i
]) << "\n";
512 void MyFrame::OnTestMsgBox(wxCommandEvent
& WXUNUSED(event
))
516 _("Are the labels of the buttons in this message box "
517 "translated into the current locale language?"),
518 _("wxWidgets i18n sample"),
523 wxMessageBox(_("Please report the details of your platform to us."));