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
[] =
102 wxLANGUAGE_BULGARIAN
,
106 #if wxUSE_UNICODE || defined(__WXMOTIF__)
112 wxLANGUAGE_ENGLISH_US
,
114 wxLANGUAGE_ARABIC_EGYPT
118 // note that it makes no sense to translate these strings, they are
119 // shown before we set the locale anyhow
120 const wxString langNames
[] =
122 _T("System default"),
130 #if wxUSE_UNICODE || defined(__WXMOTIF__)
136 _T("English (U.S.)"),
142 // the arrays must be in sync
143 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames
) == WXSIZEOF(langIds
),
144 LangArraysMismatch
);
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
151 EVT_MENU(INTERNAT_TEST
, MyFrame::OnTestLocaleAvail
)
152 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
153 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
155 EVT_MENU(INTERNAT_PLAY
, MyFrame::OnPlay
)
156 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
157 EVT_MENU(INTERNAT_TEST_1
, MyFrame::OnTest1
)
158 EVT_MENU(INTERNAT_TEST_2
, MyFrame::OnTest2
)
159 EVT_MENU(INTERNAT_TEST_3
, MyFrame::OnTest3
)
164 // ============================================================================
166 // ============================================================================
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 // command line arguments handling
173 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
175 parser
.AddParam(_("locale"),
176 wxCMD_LINE_VAL_STRING
,
177 wxCMD_LINE_PARAM_OPTIONAL
);
179 wxApp::OnInitCmdLine(parser
);
182 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
184 if ( !wxApp::OnCmdLineParsed(parser
) )
187 if ( parser
.GetParamCount() )
189 const wxString loc
= parser
.GetParam();
190 const wxLanguageInfo
* const lang
= wxLocale::FindLanguageInfo(loc
);
193 wxLogError(_("Locale \"%s\" is unknown."), loc
);
197 m_lang
= static_cast<wxLanguage
>(lang
->Language
);
203 // `Main program' equivalent, creating windows and returning main app frame
206 if ( !wxApp::OnInit() )
209 if ( m_lang
== wxLANGUAGE_UNKNOWN
)
211 int lng
= wxGetSingleChoiceIndex
213 _("Please choose language:"),
218 m_lang
= lng
== -1 ? wxLANGUAGE_DEFAULT
: langIds
[lng
];
221 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
222 // false just because it failed to load wxstd catalog
223 if ( !m_locale
.Init(m_lang
, wxLOCALE_CONV_ENCODING
) )
225 wxLogWarning(_("This language is not supported by the system."));
227 // continue nevertheless
230 // normally this wouldn't be necessary as the catalog files would be found
231 // in the default locations, but when the program is not installed the
232 // catalogs are in the build directory where we wouldn't find them by
234 wxLocale::AddCatalogLookupPathPrefix(wxT("."));
236 // Initialize the catalogs we'll be using
237 m_locale
.AddCatalog(wxT("internat"));
239 // this catalog is installed in standard location on Linux systems and
240 // shows that you may make use of the standard message catalogs as well
242 // if it's not installed on your system, it is just silently ignored
246 m_locale
.AddCatalog(_T("fileutils"));
250 // Create the main frame window
251 MyFrame
*frame
= new MyFrame(m_locale
);
254 frame
->SetIcon(wxICON(mondrian
));
257 wxMenu
*file_menu
= new wxMenu
;
258 file_menu
->Append(INTERNAT_TEST
, _("&Test locale availability...\tCtrl-T"));
259 file_menu
->AppendSeparator();
260 file_menu
->Append(wxID_ABOUT
, _("&About..."));
261 file_menu
->AppendSeparator();
262 file_menu
->Append(wxID_EXIT
, _("E&xit"));
264 wxMenu
*test_menu
= new wxMenu
;
265 test_menu
->Append(wxID_OPEN
, _("&Open bogus file"));
266 test_menu
->Append(INTERNAT_PLAY
, _("&Play a game"));
267 test_menu
->AppendSeparator();
268 test_menu
->Append(INTERNAT_TEST_1
, _("&1 _() (gettext)"));
269 test_menu
->Append(INTERNAT_TEST_2
, _("&2 _N() (ngettext)"));
270 test_menu
->Append(INTERNAT_TEST_3
, _("&3 wxTRANSLATE() (gettext_noop)"));
272 wxMenuBar
*menu_bar
= new wxMenuBar
;
273 menu_bar
->Append(file_menu
, _("&File"));
274 menu_bar
->Append(test_menu
, _("&Test"));
275 frame
->SetMenuBar(menu_bar
);
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 // main frame constructor
289 MyFrame::MyFrame(wxLocale
& locale
)
292 _("International wxWidgets App")),
295 // this demonstrates RTL layout mirroring for Arabic locales
296 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
297 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("First")),
298 wxSizerFlags().Border());
299 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("Second")),
300 wxSizerFlags().Border());
304 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
309 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
312 wxString locale
= m_locale
.GetLocale();
313 wxString sysname
= m_locale
.GetSysName();
314 wxString canname
= m_locale
.GetCanonicalName();
316 localeInfo
.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
317 locale
.c_str(), sysname
.c_str(), canname
.c_str() );
321 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
325 wxOK
| wxICON_INFORMATION
330 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
332 wxString str
= wxGetTextFromUser
334 _("Enter your number:"),
335 _("Try to guess my number!"),
347 if ( !str
.ToLong(&num
) || num
< 0 )
349 str
= _("You've probably entered an invalid number.");
353 // this message is not translated (not in catalog) because we used _T()
354 // and not _() around it
355 str
= _T("You've found a bug in this program!");
357 else if ( num
== 17 )
361 // string must be split in two -- otherwise the translation would't be
363 str
<< _("Congratulations! you've won. Here is the magic phrase:")
364 << _("cannot create fifo `%s'");
368 // this is a more implicit way to write _() but note that if you use it
369 // you must ensure that the strings get extracted in the message
370 // catalog as by default xgettext won't do it (it only knows of _(),
371 // not wxGetTranslation())
372 str
= wxGetTranslation(_T("Bad luck! try again..."));
375 wxMessageBox(str
, _("Result"), wxOK
| wxICON_INFORMATION
);
378 void MyFrame::OnTestLocaleAvail(wxCommandEvent
& WXUNUSED(event
))
380 static wxString s_locale
;
381 wxString locale
= wxGetTextFromUser
383 _("Enter the locale to test"),
384 wxGetTextFromUserPromptStr
,
388 if ( locale
.empty() )
392 const wxLanguageInfo
* const info
= wxLocale::FindLanguageInfo(s_locale
);
395 wxLogError(_("Locale \"%s\" is unknown."), s_locale
.c_str());
399 if ( wxLocale::IsAvailable(info
->Language
) )
400 wxLogMessage(_("Locale \"%s\" is available."), s_locale
.c_str());
402 wxLogWarning(_("Locale \"%s\" is not available."), s_locale
.c_str());
405 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
407 // open a bogus file -- the error message should be also translated if
408 // you've got wxstd.mo somewhere in the search path
409 wxFile
file(wxT("NOTEXIST.ING"));
412 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
414 const wxString title
= _("Testing _() (gettext)");
415 wxTextEntryDialog
d(this, _("Please enter text to translate"),
416 title
, wxTRANSLATE("default value"));
417 if (d
.ShowModal() == wxID_OK
)
419 wxString v
= d
.GetValue();
421 s
<< _T("\n") << v
<< _T(" -> ")
422 << wxGetTranslation(v
.c_str()) << _T("\n");
427 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
429 const wxString title
= _("Testing _N() (ngettext)");
430 wxTextEntryDialog
d(this,
431 _("Please enter range for plural forms of \"n files deleted\" phrase"),
433 if (d
.ShowModal() == wxID_OK
)
436 wxSscanf(d
.GetValue(), _T("%d-%d"), &first
, &last
);
439 for (int n
= first
; n
<= last
; ++n
)
442 wxPLURAL("file deleted", "files deleted", n
) <<
449 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
451 const char* lines
[] =
453 wxTRANSLATE("line 1"),
454 wxTRANSLATE("line 2"),
455 wxTRANSLATE("line 3"),
457 wxString
s(_("Testing wxTRANSLATE() (gettext_noop)"));
459 for (size_t i
= 0; i
< WXSIZEOF(lines
); ++i
)
461 s
<< lines
[i
] << _T(" -> ") << wxGetTranslation(lines
[i
]) << _T("\n");