]>
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
,
138 wxLANGUAGE_ENGLISH_US
143 // note that it makes no sense to translate these strings, they are
144 // shown before we set the locale anyhow
145 const wxString langNames
[] =
147 _T("System default"),
162 // the arrays should be in sync
163 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames
) == WXSIZEOF(langIds
),
164 LangArraysMismatch
);
166 lng
= wxGetSingleChoiceIndex
168 _T("Please choose language:"),
176 m_locale
.Init(langIds
[lng
]);
179 // Initialize the catalogs we'll be using
180 m_locale
.AddCatalog(wxT("internat"));
182 // this catalog is installed in standard location on Linux systems and
183 // shows that you may make use of the standard message catalogs as well
185 // if it's not installed on your system, it is just silently ignored
189 m_locale
.AddCatalog(_T("fileutils"));
193 // Create the main frame window
194 MyFrame
*frame
= new MyFrame(m_locale
);
197 frame
->SetIcon(wxICON(mondrian
));
200 wxMenu
*file_menu
= new wxMenu
;
201 file_menu
->Append(wxID_ABOUT
, _("&About..."));
202 file_menu
->AppendSeparator();
203 file_menu
->Append(wxID_EXIT
, _("E&xit"));
205 wxMenu
*test_menu
= new wxMenu
;
206 test_menu
->Append(INTERNAT_OPEN
, _("&Open bogus file"));
207 test_menu
->Append(INTERNAT_TEST
, _("&Play a game"));
208 test_menu
->AppendSeparator();
209 test_menu
->Append(INTERNAT_TEST_1
, _("&1 _() (gettext)"));
210 test_menu
->Append(INTERNAT_TEST_2
, _("&2 _N() (ngettext)"));
211 test_menu
->Append(INTERNAT_TEST_3
, _("&3 wxTRANSLATE() (gettext_noop)"));
213 wxMenuBar
*menu_bar
= new wxMenuBar
;
214 menu_bar
->Append(file_menu
, _("&File"));
215 menu_bar
->Append(test_menu
, _("&Test"));
216 frame
->SetMenuBar(menu_bar
);
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 // main frame constructor
230 MyFrame::MyFrame(wxLocale
& locale
)
233 _("International wxWidgets App")),
239 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
244 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
247 wxString locale
= m_locale
.GetLocale();
248 wxString sysname
= m_locale
.GetSysName();
249 wxString canname
= m_locale
.GetCanonicalName();
251 localeInfo
.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
252 locale
.c_str(), sysname
.c_str(), canname
.c_str() );
257 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
261 wxOK
| wxICON_INFORMATION
265 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
267 wxString str
= wxGetTextFromUser
269 _("Enter your number:"),
270 _("Try to guess my number!"),
282 if ( !str
.ToLong(&num
) || num
< 0 )
284 str
= _("You've probably entered an invalid number.");
288 // this message is not translated (not in catalog) because we used _T()
289 // and not _() around it
290 str
= _T("You've found a bug in this program!");
292 else if ( num
== 17 )
296 // string must be split in two -- otherwise the translation would't be
298 str
<< _("Congratulations! you've won. Here is the magic phrase:")
299 << _("cannot create fifo `%s'");
303 // this is a more implicit way to write _() but note that if you use it
304 // you must ensure that the strings get extracted in the message
305 // catalog as by default xgettext won't do it (it only knows of _(),
306 // not wxGetTranslation())
307 str
= wxGetTranslation(_T("Bad luck! try again..."));
310 wxMessageBox(str
, _("Result"), wxOK
| wxICON_INFORMATION
);
313 void MyFrame::OnOpen(wxCommandEvent
&)
315 // open a bogus file -- the error message should be also translated if
316 // you've got wxstd.mo somewhere in the search path
317 wxFile
file(wxT("NOTEXIST.ING"));
320 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
322 const wxChar
* title
= _("Testing _() (gettext)");
323 wxTextEntryDialog
d(this, _("Please enter text to translate"),
324 title
, wxTRANSLATE("default value"));
325 if (d
.ShowModal() == wxID_OK
)
327 wxString v
= d
.GetValue();
329 s
<< _T("\n") << v
<< _T(" -> ")
330 << wxGetTranslation(v
.c_str()) << _T("\n");
335 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
337 const wxChar
* title
= _("Testing _N() (ngettext)");
338 wxTextEntryDialog
d(this,
339 _("Please enter range for plural forms of \"n files deleted\" phrase"),
341 if (d
.ShowModal() == wxID_OK
)
344 wxSscanf(d
.GetValue(), _T("%d-%d"), &first
, &last
);
347 for (int n
= first
; n
<= last
; ++n
)
350 wxGetTranslation(_T("file deleted"), _T("files deleted"), n
) <<
357 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
359 const wxChar
* lines
[] =
361 wxTRANSLATE("line 1"),
362 wxTRANSLATE("line 2"),
363 wxTRANSLATE("line 3"),
365 wxString
s(_("Testing wxTRANSLATE() (gettext_noop)"));
367 for (size_t i
= 0; i
< WXSIZEOF(lines
); ++i
)
369 s
<< lines
[i
] << _T(" -> ") << wxGetTranslation(lines
[i
]) << _T("\n");