]> git.saurik.com Git - wxWidgets.git/blame - samples/internat/internat.cpp
using new API (no visual difference)
[wxWidgets.git] / samples / internat / internat.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: internat.cpp
c7f3b78b 3// Purpose: Demonstrates internationalisation (i18n) support
c801d85f
KB
4// Author: Vadim Zeitlin/Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
c7f3b78b 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
017fcf32
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
c801d85f
KB
19
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
28#include "wx/wx.h"
29#endif
30
31#include "wx/intl.h"
c7f3b78b 32#include "wx/file.h"
c801d85f
KB
33#include "wx/log.h"
34
618f2efa 35#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
47908e25
RR
36#include "mondrian.xpm"
37#endif
38
017fcf32
VZ
39// ----------------------------------------------------------------------------
40// private classes
41// ----------------------------------------------------------------------------
42
c801d85f
KB
43// Define a new application type
44class MyApp: public wxApp
45{
46public:
aec18ff7 47 virtual bool OnInit();
c7f3b78b 48
c801d85f 49protected:
aec18ff7 50 wxLocale m_locale; // locale we'll be using
c801d85f
KB
51};
52
53// Define a new frame type
54class MyFrame: public wxFrame
aec18ff7 55{
c7f3b78b 56public:
017fcf32 57 MyFrame(wxLocale& m_locale);
c7f3b78b
VZ
58
59public:
aec18ff7
MB
60 void OnQuit(wxCommandEvent& event);
61 void OnAbout(wxCommandEvent& event);
62 void OnPlay(wxCommandEvent& event);
63 void OnOpen(wxCommandEvent& event);
849a28d0
VS
64 void OnTest1(wxCommandEvent& event);
65 void OnTest2(wxCommandEvent& event);
66 void OnTest3(wxCommandEvent& event);
f13880a9 67
aec18ff7
MB
68 DECLARE_EVENT_TABLE()
69
70 wxLocale& m_locale;
c801d85f
KB
71};
72
017fcf32
VZ
73// ----------------------------------------------------------------------------
74// constants
75// ----------------------------------------------------------------------------
76
c801d85f 77// ID for the menu commands
c7f3b78b
VZ
78enum
79{
f13880a9 80 INTERNAT_TEXT = wxID_HIGHEST + 1,
017fcf32 81 INTERNAT_TEST,
849a28d0
VS
82 INTERNAT_TEST_1,
83 INTERNAT_TEST_2,
84 INTERNAT_TEST_3,
017fcf32 85 INTERNAT_OPEN
c7f3b78b 86};
c801d85f 87
79b4079f
VZ
88// language data
89static const wxLanguage langIds[] =
90{
91 wxLANGUAGE_DEFAULT,
92 wxLANGUAGE_FRENCH,
93 wxLANGUAGE_GERMAN,
94 wxLANGUAGE_RUSSIAN,
95 wxLANGUAGE_BULGARIAN,
96 wxLANGUAGE_CZECH,
97 wxLANGUAGE_POLISH,
98 wxLANGUAGE_SWEDISH,
99#if wxUSE_UNICODE || defined(__WXMOTIF__)
100 wxLANGUAGE_JAPANESE,
101#endif
102#if wxUSE_UNICODE
103 wxLANGUAGE_GEORGIAN,
104 wxLANGUAGE_ENGLISH,
105 wxLANGUAGE_ENGLISH_US
106#endif
107};
108
109// note that it makes no sense to translate these strings, they are
110// shown before we set the locale anyhow
111const wxString langNames[] =
112{
113 _T("System default"),
114 _T("French"),
115 _T("German"),
116 _T("Russian"),
117 _T("Bulgarian"),
118 _T("Czech"),
119 _T("Polish"),
120 _T("Swedish"),
121#if wxUSE_UNICODE || defined(__WXMOTIF__)
122 _T("Japanese"),
123#endif
124#if wxUSE_UNICODE
125 _T("Georgian"),
126 _T("English"),
127 _T("English (U.S.)")
128#endif
129};
130
131// the arrays must be in sync
132wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
133 LangArraysMismatch );
134
017fcf32 135// ----------------------------------------------------------------------------
be5a51fb 136// wxWidgets macros
017fcf32
VZ
137// ----------------------------------------------------------------------------
138
c801d85f 139BEGIN_EVENT_TABLE(MyFrame, wxFrame)
f13880a9 140 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
aec18ff7 141 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
017fcf32
VZ
142 EVT_MENU(INTERNAT_TEST, MyFrame::OnPlay)
143 EVT_MENU(INTERNAT_OPEN, MyFrame::OnOpen)
849a28d0
VS
144 EVT_MENU(INTERNAT_TEST_1, MyFrame::OnTest1)
145 EVT_MENU(INTERNAT_TEST_2, MyFrame::OnTest2)
146 EVT_MENU(INTERNAT_TEST_3, MyFrame::OnTest3)
c801d85f
KB
147END_EVENT_TABLE()
148
149IMPLEMENT_APP(MyApp)
150
017fcf32
VZ
151// ============================================================================
152// implementation
153// ============================================================================
154
155// ----------------------------------------------------------------------------
156// MyApp
157// ----------------------------------------------------------------------------
c801d85f 158
071cc2be 159// `Main program' equivalent, creating windows and returning main app frame
fd323a5e 160bool MyApp::OnInit()
c801d85f 161{
017fcf32
VZ
162 long lng = -1;
163
164 if ( argc == 2 )
aec18ff7 165 {
017fcf32 166 // the parameter must be the lang index
5c0ee4a8
MB
167 wxString tmp(argv[1]);
168 tmp.ToLong(&lng);
017fcf32
VZ
169 }
170
171 if ( lng == -1 )
172 {
017fcf32
VZ
173 lng = wxGetSingleChoiceIndex
174 (
175 _T("Please choose language:"),
f13880a9 176 _T("Language"),
a2f26963
VZ
177 WXSIZEOF(langNames),
178 langNames
017fcf32
VZ
179 );
180 }
181
a2f26963 182 if ( lng != -1 )
346f4231
VZ
183 {
184 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
185 // false just because it failed to load wxstd catalog
186 if ( !m_locale.Init(langIds[lng], wxLOCALE_CONV_ENCODING) )
187 {
188 wxLogError(_T("This language is not supported by the system."));
189 return false;
190 }
191 }
aec18ff7 192
de93d71f
VZ
193 // normally this wouldn't be necessary as the catalog files would be found
194 // in the default locations, but under Windows then the program is not
195 // installed the catalogs are in the parent directory (because the binary
196 // is in a subdirectory of samples/internat) where we wouldn't find them by
197 // default
198 wxLocale::AddCatalogLookupPathPrefix(wxT("."));
199 wxLocale::AddCatalogLookupPathPrefix(wxT(".."));
017fcf32 200
aec18ff7 201 // Initialize the catalogs we'll be using
017fcf32
VZ
202 m_locale.AddCatalog(wxT("internat"));
203
204 // this catalog is installed in standard location on Linux systems and
205 // shows that you may make use of the standard message catalogs as well
206 //
207 // if it's not installed on your system, it is just silently ignored
8e97b17b 208#ifdef __LINUX__
aec18ff7 209 {
017fcf32
VZ
210 wxLogNull noLog;
211 m_locale.AddCatalog(_T("fileutils"));
aec18ff7 212 }
8e97b17b 213#endif
aec18ff7
MB
214
215 // Create the main frame window
017fcf32 216 MyFrame *frame = new MyFrame(m_locale);
aec18ff7
MB
217
218 // Give it an icon
219 frame->SetIcon(wxICON(mondrian));
220
221 // Make a menubar
222 wxMenu *file_menu = new wxMenu;
223 file_menu->Append(wxID_ABOUT, _("&About..."));
224 file_menu->AppendSeparator();
f13880a9 225 file_menu->Append(wxID_EXIT, _("E&xit"));
aec18ff7
MB
226
227 wxMenu *test_menu = new wxMenu;
017fcf32
VZ
228 test_menu->Append(INTERNAT_OPEN, _("&Open bogus file"));
229 test_menu->Append(INTERNAT_TEST, _("&Play a game"));
849a28d0
VS
230 test_menu->AppendSeparator();
231 test_menu->Append(INTERNAT_TEST_1, _("&1 _() (gettext)"));
232 test_menu->Append(INTERNAT_TEST_2, _("&2 _N() (ngettext)"));
4c3590e4 233 test_menu->Append(INTERNAT_TEST_3, _("&3 wxTRANSLATE() (gettext_noop)"));
aec18ff7
MB
234
235 wxMenuBar *menu_bar = new wxMenuBar;
236 menu_bar->Append(file_menu, _("&File"));
237 menu_bar->Append(test_menu, _("&Test"));
238 frame->SetMenuBar(menu_bar);
239
240 // Show the frame
f13880a9 241 frame->Show(true);
aec18ff7
MB
242 SetTopWindow(frame);
243
f13880a9 244 return true;
c801d85f
KB
245}
246
017fcf32
VZ
247// ----------------------------------------------------------------------------
248// MyFrame
249// ----------------------------------------------------------------------------
250
251// main frame constructor
252MyFrame::MyFrame(wxLocale& locale)
253 : wxFrame(NULL,
f13880a9
WS
254 wxID_ANY,
255 _("International wxWidgets App")),
017fcf32 256 m_locale(locale)
c7f3b78b 257{
aec18ff7 258 // Empty
c7f3b78b 259}
c801d85f 260
bd7d06f2 261void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
c801d85f 262{
f13880a9 263 Close(true);
c801d85f
KB
264}
265
bd7d06f2 266void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 267{
aec18ff7 268 wxString localeInfo;
2b5f62a0
VZ
269 wxString locale = m_locale.GetLocale();
270 wxString sysname = m_locale.GetSysName();
271 wxString canname = m_locale.GetCanonicalName();
272
f13880a9 273 localeInfo.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
2b5f62a0 274 locale.c_str(), sysname.c_str(), canname.c_str() );
aec18ff7 275
925e9792
WS
276 wxMessageDialog dlg(
277 this,
278 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
279 + wxT("\n\n")
280 + localeInfo,
281 _("About Internat"),
282 wxOK | wxICON_INFORMATION
283 );
284 dlg.ShowModal();
c801d85f
KB
285}
286
bd7d06f2 287void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
c7f3b78b 288{
085c26ac
VZ
289 wxString str = wxGetTextFromUser
290 (
291 _("Enter your number:"),
292 _("Try to guess my number!"),
293 wxEmptyString,
294 this
295 );
296
297 if ( str.empty() )
298 {
299 // cancelled
300 return;
301 }
aec18ff7 302
085c26ac
VZ
303 long num;
304 if ( !str.ToLong(&num) || num < 0 )
305 {
aec18ff7 306 str = _("You've probably entered an invalid number.");
085c26ac
VZ
307 }
308 else if ( num == 9 )
309 {
310 // this message is not translated (not in catalog) because we used _T()
311 // and not _() around it
2b5f62a0 312 str = _T("You've found a bug in this program!");
085c26ac
VZ
313 }
314 else if ( num == 17 )
aec18ff7 315 {
085c26ac
VZ
316 str.clear();
317
318 // string must be split in two -- otherwise the translation would't be
319 // found
aec18ff7
MB
320 str << _("Congratulations! you've won. Here is the magic phrase:")
321 << _("cannot create fifo `%s'");
322 }
085c26ac
VZ
323 else
324 {
325 // this is a more implicit way to write _() but note that if you use it
326 // you must ensure that the strings get extracted in the message
327 // catalog as by default xgettext won't do it (it only knows of _(),
328 // not wxGetTranslation())
329 str = wxGetTranslation(_T("Bad luck! try again..."));
330 }
aec18ff7
MB
331
332 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
c7f3b78b
VZ
333}
334
335void MyFrame::OnOpen(wxCommandEvent&)
336{
849a28d0
VS
337 // open a bogus file -- the error message should be also translated if
338 // you've got wxstd.mo somewhere in the search path
aec18ff7 339 wxFile file(wxT("NOTEXIST.ING"));
071cc2be 340}
085c26ac 341
849a28d0
VS
342void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
343{
344 const wxChar* title = _("Testing _() (gettext)");
345 wxTextEntryDialog d(this, _("Please enter text to translate"),
f13880a9 346 title, wxTRANSLATE("default value"));
849a28d0
VS
347 if (d.ShowModal() == wxID_OK)
348 {
f13880a9
WS
349 wxString v = d.GetValue();
350 wxString s(title);
351 s << _T("\n") << v << _T(" -> ")
352 << wxGetTranslation(v.c_str()) << _T("\n");
353 wxMessageBox(s);
849a28d0
VS
354 }
355}
356
357void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
358{
359 const wxChar* title = _("Testing _N() (ngettext)");
360 wxTextEntryDialog d(this,
f13880a9
WS
361 _("Please enter range for plural forms of \"n files deleted\" phrase"),
362 title, _T("0-10"));
849a28d0
VS
363 if (d.ShowModal() == wxID_OK)
364 {
f13880a9
WS
365 int first, last;
366 wxSscanf(d.GetValue(), _T("%d-%d"), &first, &last);
367 wxString s(title);
368 s << _T("\n");
369 for (int n = first; n <= last; ++n)
849a28d0 370 {
f13880a9 371 s << n << _T(" ") <<
15d06954 372 wxPLURAL("file deleted", "files deleted", n) <<
552973e0 373 _T("\n");
f13880a9 374 }
849a28d0
VS
375 wxMessageBox(s);
376 }
377}
378
379void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
380{
381 const wxChar* lines[] =
382 {
f13880a9
WS
383 wxTRANSLATE("line 1"),
384 wxTRANSLATE("line 2"),
385 wxTRANSLATE("line 3"),
849a28d0 386 };
4c3590e4 387 wxString s(_("Testing wxTRANSLATE() (gettext_noop)"));
849a28d0
VS
388 s << _T("\n");
389 for (size_t i = 0; i < WXSIZEOF(lines); ++i)
390 {
f13880a9 391 s << lines[i] << _T(" -> ") << wxGetTranslation(lines[i]) << _T("\n");
849a28d0
VS
392 }
393 wxMessageBox(s);
394}
395
396