]>
git.saurik.com Git - wxWidgets.git/blob - samples/internat/internat.cpp
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 OnQuit(wxCommandEvent
& event
);
61 void OnAbout(wxCommandEvent
& event
);
62 void OnPlay(wxCommandEvent
& event
);
63 void OnOpen(wxCommandEvent
& event
);
64 void OnTest1(wxCommandEvent
& event
);
65 void OnTest2(wxCommandEvent
& event
);
66 void OnTest3(wxCommandEvent
& event
);
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 // ID for the menu commands
80 INTERNAT_TEXT
= wxID_HIGHEST
+ 1,
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
93 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
94 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
95 EVT_MENU(INTERNAT_TEST
, MyFrame::OnPlay
)
96 EVT_MENU(INTERNAT_OPEN
, MyFrame::OnOpen
)
97 EVT_MENU(INTERNAT_TEST_1
, MyFrame::OnTest1
)
98 EVT_MENU(INTERNAT_TEST_2
, MyFrame::OnTest2
)
99 EVT_MENU(INTERNAT_TEST_3
, MyFrame::OnTest3
)
104 // ============================================================================
106 // ============================================================================
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 // `Main program' equivalent, creating windows and returning main app frame
119 // the parameter must be the lang index
120 wxString
tmp(argv
[1]);
124 static const wxLanguage langIds
[] =
130 wxLANGUAGE_BULGARIAN
,
139 wxLANGUAGE_ENGLISH_US
144 // note that it makes no sense to translate these strings, they are
145 // shown before we set the locale anyhow
146 const wxString langNames
[] =
148 _T("System default"),
164 // the arrays should be in sync
165 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames
) == WXSIZEOF(langIds
),
166 LangArraysMismatch
);
168 lng
= wxGetSingleChoiceIndex
170 _T("Please choose language:"),
178 m_locale
.Init(langIds
[lng
]);
180 // normally this wouldn't be necessary as the catalog files would be found
181 // in the default locations, but under Windows then the program is not
182 // installed the catalogs are in the parent directory (because the binary
183 // is in a subdirectory of samples/internat) where we wouldn't find them by
185 wxLocale::AddCatalogLookupPathPrefix(wxT("."));
186 wxLocale::AddCatalogLookupPathPrefix(wxT(".."));
188 // Initialize the catalogs we'll be using
189 m_locale
.AddCatalog(wxT("internat"));
191 // this catalog is installed in standard location on Linux systems and
192 // shows that you may make use of the standard message catalogs as well
194 // if it's not installed on your system, it is just silently ignored
198 m_locale
.AddCatalog(_T("fileutils"));
202 // Create the main frame window
203 MyFrame
*frame
= new MyFrame(m_locale
);
206 frame
->SetIcon(wxICON(mondrian
));
209 wxMenu
*file_menu
= new wxMenu
;
210 file_menu
->Append(wxID_ABOUT
, _("&About..."));
211 file_menu
->AppendSeparator();
212 file_menu
->Append(wxID_EXIT
, _("E&xit"));
214 wxMenu
*test_menu
= new wxMenu
;
215 test_menu
->Append(INTERNAT_OPEN
, _("&Open bogus file"));
216 test_menu
->Append(INTERNAT_TEST
, _("&Play a game"));
217 test_menu
->AppendSeparator();
218 test_menu
->Append(INTERNAT_TEST_1
, _("&1 _() (gettext)"));
219 test_menu
->Append(INTERNAT_TEST_2
, _("&2 _N() (ngettext)"));
220 test_menu
->Append(INTERNAT_TEST_3
, _("&3 wxTRANSLATE() (gettext_noop)"));
222 wxMenuBar
*menu_bar
= new wxMenuBar
;
223 menu_bar
->Append(file_menu
, _("&File"));
224 menu_bar
->Append(test_menu
, _("&Test"));
225 frame
->SetMenuBar(menu_bar
);
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 // main frame constructor
239 MyFrame::MyFrame(wxLocale
& locale
)
242 _("International wxWidgets App")),
248 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
253 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
256 wxString locale
= m_locale
.GetLocale();
257 wxString sysname
= m_locale
.GetSysName();
258 wxString canname
= m_locale
.GetCanonicalName();
260 localeInfo
.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
261 locale
.c_str(), sysname
.c_str(), canname
.c_str() );
265 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
269 wxOK
| wxICON_INFORMATION
274 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
276 wxString str
= wxGetTextFromUser
278 _("Enter your number:"),
279 _("Try to guess my number!"),
291 if ( !str
.ToLong(&num
) || num
< 0 )
293 str
= _("You've probably entered an invalid number.");
297 // this message is not translated (not in catalog) because we used _T()
298 // and not _() around it
299 str
= _T("You've found a bug in this program!");
301 else if ( num
== 17 )
305 // string must be split in two -- otherwise the translation would't be
307 str
<< _("Congratulations! you've won. Here is the magic phrase:")
308 << _("cannot create fifo `%s'");
312 // this is a more implicit way to write _() but note that if you use it
313 // you must ensure that the strings get extracted in the message
314 // catalog as by default xgettext won't do it (it only knows of _(),
315 // not wxGetTranslation())
316 str
= wxGetTranslation(_T("Bad luck! try again..."));
319 wxMessageBox(str
, _("Result"), wxOK
| wxICON_INFORMATION
);
322 void MyFrame::OnOpen(wxCommandEvent
&)
324 // open a bogus file -- the error message should be also translated if
325 // you've got wxstd.mo somewhere in the search path
326 wxFile
file(wxT("NOTEXIST.ING"));
329 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
331 const wxChar
* title
= _("Testing _() (gettext)");
332 wxTextEntryDialog
d(this, _("Please enter text to translate"),
333 title
, wxTRANSLATE("default value"));
334 if (d
.ShowModal() == wxID_OK
)
336 wxString v
= d
.GetValue();
338 s
<< _T("\n") << v
<< _T(" -> ")
339 << wxGetTranslation(v
.c_str()) << _T("\n");
344 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
346 const wxChar
* title
= _("Testing _N() (ngettext)");
347 wxTextEntryDialog
d(this,
348 _("Please enter range for plural forms of \"n files deleted\" phrase"),
350 if (d
.ShowModal() == wxID_OK
)
353 wxSscanf(d
.GetValue(), _T("%d-%d"), &first
, &last
);
356 for (int n
= first
; n
<= last
; ++n
)
359 wxPLURAL("file deleted", "files deleted", n
) <<
366 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
368 const wxChar
* lines
[] =
370 wxTRANSLATE("line 1"),
371 wxTRANSLATE("line 2"),
372 wxTRANSLATE("line 3"),
374 wxString
s(_("Testing wxTRANSLATE() (gettext_noop)"));
376 for (size_t i
= 0; i
< WXSIZEOF(lines
); ++i
)
378 s
<< lines
[i
] << _T(" -> ") << wxGetTranslation(lines
[i
]) << _T("\n");