1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Demonstrates internationalisation (i18n) support
4 // Author: Vadim Zeitlin/Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
34 #include "wx/cmdline.h"
36 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
37 #include "mondrian.xpm"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // Define a new application type
45 class MyApp
: public wxApp
48 MyApp() { m_lang
= wxLANGUAGE_UNKNOWN
; }
50 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
51 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
52 virtual bool OnInit();
55 wxLanguage m_lang
; // language specified by user
56 wxLocale m_locale
; // locale we'll be using
59 // Define a new frame type
60 class MyFrame
: public wxFrame
63 MyFrame(wxLocale
& m_locale
);
66 void OnTestLocaleAvail(wxCommandEvent
& event
);
67 void OnAbout(wxCommandEvent
& event
);
68 void OnQuit(wxCommandEvent
& event
);
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
);
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // ID for the menu commands
88 INTERNAT_TEST
= wxID_HIGHEST
+ 1,
96 static const wxLanguage langIds
[] =
103 wxLANGUAGE_BULGARIAN
,
107 #if wxUSE_UNICODE || defined(__WXMOTIF__)
113 wxLANGUAGE_ENGLISH_US
,
115 wxLANGUAGE_ARABIC_EGYPT
119 // note that it makes no sense to translate these strings, they are
120 // shown before we set the locale anyhow
121 const wxString langNames
[] =
132 #if wxUSE_UNICODE || defined(__WXMOTIF__)
144 // the arrays must be in sync
145 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames
) == WXSIZEOF(langIds
),
146 LangArraysMismatch
);
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
153 EVT_MENU(INTERNAT_TEST
, MyFrame::OnTestLocaleAvail
)
154 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
155 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
157 EVT_MENU(INTERNAT_PLAY
, MyFrame::OnPlay
)
158 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
159 EVT_MENU(INTERNAT_TEST_1
, MyFrame::OnTest1
)
160 EVT_MENU(INTERNAT_TEST_2
, MyFrame::OnTest2
)
161 EVT_MENU(INTERNAT_TEST_3
, MyFrame::OnTest3
)
166 // ============================================================================
168 // ============================================================================
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 // command line arguments handling
175 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
177 parser
.AddParam(_("locale"),
178 wxCMD_LINE_VAL_STRING
,
179 wxCMD_LINE_PARAM_OPTIONAL
);
181 wxApp::OnInitCmdLine(parser
);
184 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
186 if ( !wxApp::OnCmdLineParsed(parser
) )
189 if ( parser
.GetParamCount() )
191 const wxString loc
= parser
.GetParam();
192 const wxLanguageInfo
* const lang
= wxLocale::FindLanguageInfo(loc
);
195 wxLogError(_("Locale \"%s\" is unknown."), loc
);
199 m_lang
= static_cast<wxLanguage
>(lang
->Language
);
205 // `Main program' equivalent, creating windows and returning main app frame
208 if ( !wxApp::OnInit() )
211 if ( m_lang
== wxLANGUAGE_UNKNOWN
)
213 int lng
= wxGetSingleChoiceIndex
215 _("Please choose language:"),
220 m_lang
= lng
== -1 ? wxLANGUAGE_DEFAULT
: langIds
[lng
];
223 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
224 // false just because it failed to load wxstd catalog
225 if ( !m_locale
.Init(m_lang
, wxLOCALE_CONV_ENCODING
) )
227 wxLogWarning(_("This language is not supported by the system."));
229 // continue nevertheless
232 // normally this wouldn't be necessary as the catalog files would be found
233 // in the default locations, but when the program is not installed the
234 // catalogs are in the build directory where we wouldn't find them by
236 wxLocale::AddCatalogLookupPathPrefix(".");
238 // Initialize the catalogs we'll be using
239 if (!m_locale
.AddCatalog("internat"))
240 wxLogError(_("Couldn't find/load the 'internat' catalog."));
242 // Now try to add wxstd.mo so that loading "NOTEXIST.ING" file will produce
243 // a localized error message:
244 m_locale
.AddCatalog("wxstd.mo");
245 // NOTE: it's not an error if we couldn't find it!
247 // this catalog is installed in standard location on Linux systems and
248 // shows that you may make use of the standard message catalogs as well
250 // if it's not installed on your system, it is just silently ignored
254 m_locale
.AddCatalog("fileutils");
258 // Create the main frame window
259 MyFrame
*frame
= new MyFrame(m_locale
);
262 frame
->SetIcon(wxICON(mondrian
));
265 wxMenu
*file_menu
= new wxMenu
;
266 file_menu
->Append(INTERNAT_TEST
, _("&Test locale availability...\tCtrl-T"));
267 file_menu
->AppendSeparator();
268 file_menu
->Append(wxID_ABOUT
, _("&About..."));
269 file_menu
->AppendSeparator();
270 file_menu
->Append(wxID_EXIT
, _("E&xit"));
272 wxMenu
*test_menu
= new wxMenu
;
273 test_menu
->Append(wxID_OPEN
, _("&Open bogus file"));
274 test_menu
->Append(INTERNAT_PLAY
, _("&Play a game"));
275 test_menu
->AppendSeparator();
276 test_menu
->Append(INTERNAT_TEST_1
, _("&1 _() (gettext)"));
277 test_menu
->Append(INTERNAT_TEST_2
, _("&2 _N() (ngettext)"));
278 test_menu
->Append(INTERNAT_TEST_3
, _("&3 wxTRANSLATE() (gettext_noop)"));
280 wxMenuBar
*menu_bar
= new wxMenuBar
;
281 menu_bar
->Append(file_menu
, _("&File"));
282 menu_bar
->Append(test_menu
, _("&Test"));
283 frame
->SetMenuBar(menu_bar
);
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
296 // main frame constructor
297 MyFrame::MyFrame(wxLocale
& locale
)
300 _("International wxWidgets App")),
303 // this demonstrates RTL layout mirroring for Arabic locales
304 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
305 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("First")),
306 wxSizerFlags().Border());
307 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("Second")),
308 wxSizerFlags().Border());
312 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
317 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
320 wxString locale
= m_locale
.GetLocale();
321 wxString sysname
= m_locale
.GetSysName();
322 wxString canname
= m_locale
.GetCanonicalName();
324 localeInfo
.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
325 locale
.c_str(), sysname
.c_str(), canname
.c_str() );
329 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
333 wxOK
| wxICON_INFORMATION
338 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
340 wxString str
= wxGetTextFromUser
342 _("Enter your number:"),
343 _("Try to guess my number!"),
355 if ( !str
.ToLong(&num
) || num
< 0 )
357 str
= _("You've probably entered an invalid number.");
361 // this message is not translated (not in catalog) because we used _T()
362 // and not _() around it
363 str
= _T("You've found a bug in this program!");
365 else if ( num
== 17 )
369 // string must be split in two -- otherwise the translation would't be
371 str
<< _("Congratulations! you've won. Here is the magic phrase:")
372 << _("cannot create fifo `%s'");
376 // this is a more implicit way to write _() but note that if you use it
377 // you must ensure that the strings get extracted in the message
378 // catalog as by default xgettext won't do it; it only knows of _(),
379 // not of wxTRANSLATE(). As internat's readme.txt says you should thus
380 // call xgettext with -kwxTRANSLATE.
381 str
= wxGetTranslation(wxTRANSLATE("Bad luck! try again..."));
383 // note also that if we want 'str' to contain a localized string
384 // we need to use wxGetTranslation explicitely as wxTRANSLATE just
385 // tells xgettext to extract the string but has no effect on the
386 // runtime of the program!
389 wxMessageBox(str
, _("Result"), wxOK
| wxICON_INFORMATION
);
392 void MyFrame::OnTestLocaleAvail(wxCommandEvent
& WXUNUSED(event
))
394 static wxString s_locale
;
395 wxString locale
= wxGetTextFromUser
397 _("Enter the locale to test"),
398 wxGetTextFromUserPromptStr
,
402 if ( locale
.empty() )
406 const wxLanguageInfo
* const info
= wxLocale::FindLanguageInfo(s_locale
);
409 wxLogError(_("Locale \"%s\" is unknown."), s_locale
.c_str());
413 if ( wxLocale::IsAvailable(info
->Language
) )
414 wxLogMessage(_("Locale \"%s\" is available."), s_locale
.c_str());
416 wxLogWarning(_("Locale \"%s\" is not available."), s_locale
.c_str());
419 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
421 // open a bogus file -- the error message should be also translated if
422 // you've got wxstd.mo somewhere in the search path (see MyApp::OnInit)
423 wxFile
file("NOTEXIST.ING");
426 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
428 const wxString
& title
= _("Testing _() (gettext)");
430 // NOTE: using the wxTRANSLATE() macro here we won't show a localized
431 // string in the text entry dialog; we'll simply show the un-translated
432 // string; however if the user press "ok" without altering the text,
433 // since the "default value" string has been extracted by xgettext
434 // the wxGetTranslation call later will manage to return a localized
436 wxTextEntryDialog
d(this, _("Please enter text to translate"),
437 title
, wxTRANSLATE("default value"));
439 if (d
.ShowModal() == wxID_OK
)
441 wxString v
= d
.GetValue();
443 s
<< "\n" << v
<< " -> "
444 << wxGetTranslation(v
.c_str()) << "\n";
449 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
451 const wxString
& title
= _("Testing _N() (ngettext)");
452 wxTextEntryDialog
d(this,
453 _("Please enter range for plural forms of \"n files deleted\" phrase"),
456 if (d
.ShowModal() == wxID_OK
)
459 wxSscanf(d
.GetValue(), "%d-%d", &first
, &last
);
462 for (int n
= first
; n
<= last
; ++n
)
465 wxPLURAL("file deleted", "files deleted", n
) <<
472 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
474 const char* lines
[] =
476 wxTRANSLATE("line 1"),
477 wxTRANSLATE("line 2"),
478 wxTRANSLATE("line 3"),
481 wxString
s(_("Testing wxTRANSLATE() (gettext_noop)"));
483 for (size_t i
= 0; i
< WXSIZEOF(lines
); ++i
)
485 s
<< lines
[i
] << " -> " << wxGetTranslation(lines
[i
]) << "\n";