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"
35 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
36 #include "mondrian.xpm"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // Define a new application type
44 class MyApp
: public wxApp
47 virtual bool OnInit();
50 wxLocale m_locale
; // locale we'll be using
53 // Define a new frame type
54 class MyFrame
: public wxFrame
57 MyFrame(wxLocale
& m_locale
);
60 void OnTestLocaleAvail(wxCommandEvent
& event
);
61 void OnAbout(wxCommandEvent
& event
);
62 void OnQuit(wxCommandEvent
& event
);
64 void OnPlay(wxCommandEvent
& event
);
65 void OnOpen(wxCommandEvent
& event
);
66 void OnTest1(wxCommandEvent
& event
);
67 void OnTest2(wxCommandEvent
& event
);
68 void OnTest3(wxCommandEvent
& event
);
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // ID for the menu commands
82 INTERNAT_TEST
= wxID_HIGHEST
+ 1,
90 static const wxLanguage langIds
[] =
100 #if wxUSE_UNICODE || defined(__WXMOTIF__)
106 wxLANGUAGE_ENGLISH_US
,
108 wxLANGUAGE_ARABIC_EGYPT
112 // note that it makes no sense to translate these strings, they are
113 // shown before we set the locale anyhow
114 const wxString langNames
[] =
116 _T("System default"),
124 #if wxUSE_UNICODE || defined(__WXMOTIF__)
130 _T("English (U.S.)"),
136 // the arrays must be in sync
137 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames
) == WXSIZEOF(langIds
),
138 LangArraysMismatch
);
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
145 EVT_MENU(INTERNAT_TEST
, MyFrame::OnTestLocaleAvail
)
146 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
147 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
149 EVT_MENU(INTERNAT_PLAY
, MyFrame::OnPlay
)
150 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
151 EVT_MENU(INTERNAT_TEST_1
, MyFrame::OnTest1
)
152 EVT_MENU(INTERNAT_TEST_2
, MyFrame::OnTest2
)
153 EVT_MENU(INTERNAT_TEST_3
, MyFrame::OnTest3
)
158 // ============================================================================
160 // ============================================================================
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 // `Main program' equivalent, creating windows and returning main app frame
169 if ( !wxApp::OnInit() )
176 // the parameter must be the lang index
177 wxString
tmp(argv
[1]);
183 lng
= wxGetSingleChoiceIndex
185 _T("Please choose language:"),
194 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
195 // false just because it failed to load wxstd catalog
196 if ( !m_locale
.Init(langIds
[lng
], wxLOCALE_CONV_ENCODING
) )
198 wxLogError(_T("This language is not supported by the system."));
203 // normally this wouldn't be necessary as the catalog files would be found
204 // in the default locations, but when the program is not installed the
205 // catalogs are in the build directory where we wouldn't find them by
207 wxLocale::AddCatalogLookupPathPrefix(wxT("."));
209 // Initialize the catalogs we'll be using
210 m_locale
.AddCatalog(wxT("internat"));
212 // this catalog is installed in standard location on Linux systems and
213 // shows that you may make use of the standard message catalogs as well
215 // if it's not installed on your system, it is just silently ignored
219 m_locale
.AddCatalog(_T("fileutils"));
223 // Create the main frame window
224 MyFrame
*frame
= new MyFrame(m_locale
);
227 frame
->SetIcon(wxICON(mondrian
));
230 wxMenu
*file_menu
= new wxMenu
;
231 file_menu
->Append(INTERNAT_TEST
, _("&Test locale availability...\tCtrl-T"));
232 file_menu
->AppendSeparator();
233 file_menu
->Append(wxID_ABOUT
, _("&About..."));
234 file_menu
->AppendSeparator();
235 file_menu
->Append(wxID_EXIT
, _("E&xit"));
237 wxMenu
*test_menu
= new wxMenu
;
238 test_menu
->Append(wxID_OPEN
, _("&Open bogus file"));
239 test_menu
->Append(INTERNAT_PLAY
, _("&Play a game"));
240 test_menu
->AppendSeparator();
241 test_menu
->Append(INTERNAT_TEST_1
, _("&1 _() (gettext)"));
242 test_menu
->Append(INTERNAT_TEST_2
, _("&2 _N() (ngettext)"));
243 test_menu
->Append(INTERNAT_TEST_3
, _("&3 wxTRANSLATE() (gettext_noop)"));
245 wxMenuBar
*menu_bar
= new wxMenuBar
;
246 menu_bar
->Append(file_menu
, _("&File"));
247 menu_bar
->Append(test_menu
, _("&Test"));
248 frame
->SetMenuBar(menu_bar
);
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
261 // main frame constructor
262 MyFrame::MyFrame(wxLocale
& locale
)
265 _("International wxWidgets App")),
268 // this demonstrates RTL layout mirroring for Arabic locales
269 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
270 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("First")),
271 wxSizerFlags().Border());
272 sizer
->Add(new wxStaticText(this, wxID_ANY
, _("Second")),
273 wxSizerFlags().Border());
277 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
282 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
285 wxString locale
= m_locale
.GetLocale();
286 wxString sysname
= m_locale
.GetSysName();
287 wxString canname
= m_locale
.GetCanonicalName();
289 localeInfo
.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
290 locale
.c_str(), sysname
.c_str(), canname
.c_str() );
294 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
298 wxOK
| wxICON_INFORMATION
303 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
305 wxString str
= wxGetTextFromUser
307 _("Enter your number:"),
308 _("Try to guess my number!"),
320 if ( !str
.ToLong(&num
) || num
< 0 )
322 str
= _("You've probably entered an invalid number.");
326 // this message is not translated (not in catalog) because we used _T()
327 // and not _() around it
328 str
= _T("You've found a bug in this program!");
330 else if ( num
== 17 )
334 // string must be split in two -- otherwise the translation would't be
336 str
<< _("Congratulations! you've won. Here is the magic phrase:")
337 << _("cannot create fifo `%s'");
341 // this is a more implicit way to write _() but note that if you use it
342 // you must ensure that the strings get extracted in the message
343 // catalog as by default xgettext won't do it (it only knows of _(),
344 // not wxGetTranslation())
345 str
= wxGetTranslation(_T("Bad luck! try again..."));
348 wxMessageBox(str
, _("Result"), wxOK
| wxICON_INFORMATION
);
351 void MyFrame::OnTestLocaleAvail(wxCommandEvent
& WXUNUSED(event
))
353 static wxString s_locale
;
354 wxString locale
= wxGetTextFromUser
356 _("Enter the locale to test"),
357 wxGetTextFromUserPromptStr
,
361 if ( locale
.empty() )
365 const wxLanguageInfo
* const info
= wxLocale::FindLanguageInfo(s_locale
);
368 wxLogError(_("Locale \"%s\" is unknown."), s_locale
.c_str());
372 if ( wxLocale::IsAvailable(info
->Language
) )
373 wxLogMessage(_("Locale \"%s\" is available."), s_locale
.c_str());
375 wxLogWarning(_("Locale \"%s\" is not available."), s_locale
.c_str());
378 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
380 // open a bogus file -- the error message should be also translated if
381 // you've got wxstd.mo somewhere in the search path
382 wxFile
file(wxT("NOTEXIST.ING"));
385 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
387 const wxChar
* title
= _("Testing _() (gettext)");
388 wxTextEntryDialog
d(this, _("Please enter text to translate"),
389 title
, wxTRANSLATE("default value"));
390 if (d
.ShowModal() == wxID_OK
)
392 wxString v
= d
.GetValue();
394 s
<< _T("\n") << v
<< _T(" -> ")
395 << wxGetTranslation(v
.c_str()) << _T("\n");
400 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
402 const wxChar
* title
= _("Testing _N() (ngettext)");
403 wxTextEntryDialog
d(this,
404 _("Please enter range for plural forms of \"n files deleted\" phrase"),
406 if (d
.ShowModal() == wxID_OK
)
409 wxSscanf(d
.GetValue(), _T("%d-%d"), &first
, &last
);
412 for (int n
= first
; n
<= last
; ++n
)
415 wxPLURAL("file deleted", "files deleted", n
) <<
422 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
424 const wxChar
* lines
[] =
426 wxTRANSLATE("line 1"),
427 wxTRANSLATE("line 2"),
428 wxTRANSLATE("line 3"),
430 wxString
s(_("Testing wxTRANSLATE() (gettext_noop)"));
432 for (size_t i
= 0; i
< WXSIZEOF(lines
); ++i
)
434 s
<< lines
[i
] << _T(" -> ") << wxGetTranslation(lines
[i
]) << _T("\n");