]> git.saurik.com Git - wxWidgets.git/blame - samples/internat/internat.cpp
don't use wxVector<wxDataFormat> from wx/clipbrd.h as wxDataFormat definition is...
[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
3a331275 28 #include "wx/wx.h"
c801d85f
KB
29#endif
30
31#include "wx/intl.h"
c7f3b78b 32#include "wx/file.h"
c801d85f 33#include "wx/log.h"
3a331275 34#include "wx/cmdline.h"
c801d85f 35
618f2efa 36#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
47908e25
RR
37#include "mondrian.xpm"
38#endif
39
017fcf32
VZ
40// ----------------------------------------------------------------------------
41// private classes
42// ----------------------------------------------------------------------------
43
c801d85f
KB
44// Define a new application type
45class MyApp: public wxApp
46{
47public:
3a331275
VZ
48 MyApp() { m_lang = wxLANGUAGE_UNKNOWN; }
49
50 virtual void OnInitCmdLine(wxCmdLineParser& parser);
51 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
aec18ff7 52 virtual bool OnInit();
c7f3b78b 53
c801d85f 54protected:
3a331275
VZ
55 wxLanguage m_lang; // language specified by user
56 wxLocale m_locale; // locale we'll be using
c801d85f
KB
57};
58
59// Define a new frame type
60class MyFrame: public wxFrame
aec18ff7 61{
c7f3b78b 62public:
017fcf32 63 MyFrame(wxLocale& m_locale);
c7f3b78b
VZ
64
65public:
cec5ffc4 66 void OnTestLocaleAvail(wxCommandEvent& event);
aec18ff7 67 void OnAbout(wxCommandEvent& event);
cec5ffc4
VZ
68 void OnQuit(wxCommandEvent& event);
69
aec18ff7
MB
70 void OnPlay(wxCommandEvent& event);
71 void OnOpen(wxCommandEvent& event);
849a28d0
VS
72 void OnTest1(wxCommandEvent& event);
73 void OnTest2(wxCommandEvent& event);
74 void OnTest3(wxCommandEvent& event);
f13880a9 75
aec18ff7
MB
76 DECLARE_EVENT_TABLE()
77
78 wxLocale& m_locale;
c801d85f
KB
79};
80
017fcf32
VZ
81// ----------------------------------------------------------------------------
82// constants
83// ----------------------------------------------------------------------------
84
c801d85f 85// ID for the menu commands
c7f3b78b
VZ
86enum
87{
cec5ffc4
VZ
88 INTERNAT_TEST = wxID_HIGHEST + 1,
89 INTERNAT_PLAY,
849a28d0
VS
90 INTERNAT_TEST_1,
91 INTERNAT_TEST_2,
6ea0cc70 92 INTERNAT_TEST_3
c7f3b78b 93};
c801d85f 94
79b4079f
VZ
95// language data
96static const wxLanguage langIds[] =
97{
98 wxLANGUAGE_DEFAULT,
99 wxLANGUAGE_FRENCH,
100 wxLANGUAGE_GERMAN,
101 wxLANGUAGE_RUSSIAN,
102 wxLANGUAGE_BULGARIAN,
103 wxLANGUAGE_CZECH,
104 wxLANGUAGE_POLISH,
105 wxLANGUAGE_SWEDISH,
106#if wxUSE_UNICODE || defined(__WXMOTIF__)
107 wxLANGUAGE_JAPANESE,
108#endif
109#if wxUSE_UNICODE
110 wxLANGUAGE_GEORGIAN,
111 wxLANGUAGE_ENGLISH,
978af864
VZ
112 wxLANGUAGE_ENGLISH_US,
113 wxLANGUAGE_ARABIC,
114 wxLANGUAGE_ARABIC_EGYPT
79b4079f
VZ
115#endif
116};
117
118// note that it makes no sense to translate these strings, they are
119// shown before we set the locale anyhow
120const wxString langNames[] =
121{
122 _T("System default"),
123 _T("French"),
124 _T("German"),
125 _T("Russian"),
126 _T("Bulgarian"),
127 _T("Czech"),
128 _T("Polish"),
129 _T("Swedish"),
130#if wxUSE_UNICODE || defined(__WXMOTIF__)
131 _T("Japanese"),
132#endif
133#if wxUSE_UNICODE
134 _T("Georgian"),
135 _T("English"),
978af864
VZ
136 _T("English (U.S.)"),
137 _T("Arabic"),
138 _T("Arabic (Egypt)")
79b4079f
VZ
139#endif
140};
141
142// the arrays must be in sync
143wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
144 LangArraysMismatch );
145
017fcf32 146// ----------------------------------------------------------------------------
be5a51fb 147// wxWidgets macros
017fcf32
VZ
148// ----------------------------------------------------------------------------
149
c801d85f 150BEGIN_EVENT_TABLE(MyFrame, wxFrame)
cec5ffc4 151 EVT_MENU(INTERNAT_TEST, MyFrame::OnTestLocaleAvail)
aec18ff7 152 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
cec5ffc4
VZ
153 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
154
155 EVT_MENU(INTERNAT_PLAY, MyFrame::OnPlay)
6ea0cc70 156 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
849a28d0
VS
157 EVT_MENU(INTERNAT_TEST_1, MyFrame::OnTest1)
158 EVT_MENU(INTERNAT_TEST_2, MyFrame::OnTest2)
159 EVT_MENU(INTERNAT_TEST_3, MyFrame::OnTest3)
c801d85f
KB
160END_EVENT_TABLE()
161
162IMPLEMENT_APP(MyApp)
163
017fcf32
VZ
164// ============================================================================
165// implementation
166// ============================================================================
167
168// ----------------------------------------------------------------------------
169// MyApp
170// ----------------------------------------------------------------------------
c801d85f 171
3a331275
VZ
172// command line arguments handling
173void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
c801d85f 174{
3a331275
VZ
175 parser.AddParam(_("locale"),
176 wxCMD_LINE_VAL_STRING,
177 wxCMD_LINE_PARAM_OPTIONAL);
45e6e6f8 178
3a331275
VZ
179 wxApp::OnInitCmdLine(parser);
180}
017fcf32 181
3a331275
VZ
182bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
183{
184 if ( !wxApp::OnCmdLineParsed(parser) )
185 return false;
186
187 if ( parser.GetParamCount() )
aec18ff7 188 {
3a331275
VZ
189 const wxString loc = parser.GetParam();
190 const wxLanguageInfo * const lang = wxLocale::FindLanguageInfo(loc);
191 if ( !lang )
192 {
193 wxLogError(_("Locale \"%s\" is unknown."), loc);
194 return false;
195 }
196
81d3348a 197 m_lang = static_cast<wxLanguage>(lang->Language);
017fcf32
VZ
198 }
199
3a331275
VZ
200 return true;
201}
202
203// `Main program' equivalent, creating windows and returning main app frame
204bool MyApp::OnInit()
205{
206 if ( !wxApp::OnInit() )
207 return false;
208
209 if ( m_lang == wxLANGUAGE_UNKNOWN )
017fcf32 210 {
3a331275
VZ
211 int lng = wxGetSingleChoiceIndex
212 (
213 _("Please choose language:"),
214 _("Language"),
215 WXSIZEOF(langNames),
216 langNames
217 );
218 m_lang = lng == -1 ? wxLANGUAGE_DEFAULT : langIds[lng];
017fcf32
VZ
219 }
220
6f011443
VZ
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) )
346f4231 224 {
6f011443 225 wxLogWarning(_("This language is not supported by the system."));
3a331275 226
6f011443 227 // continue nevertheless
346f4231 228 }
aec18ff7 229
89611d4d
VZ
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
233 // default
234 wxLocale::AddCatalogLookupPathPrefix(wxT("."));
017fcf32 235
aec18ff7 236 // Initialize the catalogs we'll be using
017fcf32
VZ
237 m_locale.AddCatalog(wxT("internat"));
238
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
241 //
242 // if it's not installed on your system, it is just silently ignored
8e97b17b 243#ifdef __LINUX__
aec18ff7 244 {
017fcf32
VZ
245 wxLogNull noLog;
246 m_locale.AddCatalog(_T("fileutils"));
aec18ff7 247 }
8e97b17b 248#endif
aec18ff7
MB
249
250 // Create the main frame window
017fcf32 251 MyFrame *frame = new MyFrame(m_locale);
aec18ff7
MB
252
253 // Give it an icon
254 frame->SetIcon(wxICON(mondrian));
255
256 // Make a menubar
257 wxMenu *file_menu = new wxMenu;
cec5ffc4
VZ
258 file_menu->Append(INTERNAT_TEST, _("&Test locale availability...\tCtrl-T"));
259 file_menu->AppendSeparator();
aec18ff7
MB
260 file_menu->Append(wxID_ABOUT, _("&About..."));
261 file_menu->AppendSeparator();
f13880a9 262 file_menu->Append(wxID_EXIT, _("E&xit"));
aec18ff7
MB
263
264 wxMenu *test_menu = new wxMenu;
6ea0cc70 265 test_menu->Append(wxID_OPEN, _("&Open bogus file"));
cec5ffc4 266 test_menu->Append(INTERNAT_PLAY, _("&Play a game"));
849a28d0
VS
267 test_menu->AppendSeparator();
268 test_menu->Append(INTERNAT_TEST_1, _("&1 _() (gettext)"));
269 test_menu->Append(INTERNAT_TEST_2, _("&2 _N() (ngettext)"));
4c3590e4 270 test_menu->Append(INTERNAT_TEST_3, _("&3 wxTRANSLATE() (gettext_noop)"));
aec18ff7
MB
271
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);
276
277 // Show the frame
f13880a9 278 frame->Show(true);
aec18ff7
MB
279 SetTopWindow(frame);
280
f13880a9 281 return true;
c801d85f
KB
282}
283
017fcf32
VZ
284// ----------------------------------------------------------------------------
285// MyFrame
286// ----------------------------------------------------------------------------
287
288// main frame constructor
289MyFrame::MyFrame(wxLocale& locale)
290 : wxFrame(NULL,
f13880a9
WS
291 wxID_ANY,
292 _("International wxWidgets App")),
017fcf32 293 m_locale(locale)
c7f3b78b 294{
978af864
VZ
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());
301 SetSizer(sizer);
c7f3b78b 302}
c801d85f 303
bd7d06f2 304void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
c801d85f 305{
f13880a9 306 Close(true);
c801d85f
KB
307}
308
bd7d06f2 309void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 310{
aec18ff7 311 wxString localeInfo;
2b5f62a0
VZ
312 wxString locale = m_locale.GetLocale();
313 wxString sysname = m_locale.GetSysName();
314 wxString canname = m_locale.GetCanonicalName();
315
f13880a9 316 localeInfo.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
2b5f62a0 317 locale.c_str(), sysname.c_str(), canname.c_str() );
aec18ff7 318
925e9792
WS
319 wxMessageDialog dlg(
320 this,
321 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
322 + wxT("\n\n")
323 + localeInfo,
324 _("About Internat"),
325 wxOK | wxICON_INFORMATION
326 );
327 dlg.ShowModal();
c801d85f
KB
328}
329
bd7d06f2 330void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
c7f3b78b 331{
085c26ac
VZ
332 wxString str = wxGetTextFromUser
333 (
334 _("Enter your number:"),
335 _("Try to guess my number!"),
336 wxEmptyString,
337 this
338 );
339
340 if ( str.empty() )
341 {
342 // cancelled
343 return;
344 }
aec18ff7 345
085c26ac
VZ
346 long num;
347 if ( !str.ToLong(&num) || num < 0 )
348 {
aec18ff7 349 str = _("You've probably entered an invalid number.");
085c26ac
VZ
350 }
351 else if ( num == 9 )
352 {
353 // this message is not translated (not in catalog) because we used _T()
354 // and not _() around it
2b5f62a0 355 str = _T("You've found a bug in this program!");
085c26ac
VZ
356 }
357 else if ( num == 17 )
aec18ff7 358 {
085c26ac
VZ
359 str.clear();
360
361 // string must be split in two -- otherwise the translation would't be
362 // found
aec18ff7
MB
363 str << _("Congratulations! you've won. Here is the magic phrase:")
364 << _("cannot create fifo `%s'");
365 }
085c26ac
VZ
366 else
367 {
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..."));
373 }
aec18ff7
MB
374
375 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
c7f3b78b
VZ
376}
377
cec5ffc4
VZ
378void MyFrame::OnTestLocaleAvail(wxCommandEvent& WXUNUSED(event))
379{
380 static wxString s_locale;
381 wxString locale = wxGetTextFromUser
382 (
383 _("Enter the locale to test"),
384 wxGetTextFromUserPromptStr,
385 s_locale,
386 this
387 );
388 if ( locale.empty() )
389 return;
390
391 s_locale = locale;
392 const wxLanguageInfo * const info = wxLocale::FindLanguageInfo(s_locale);
393 if ( !info )
394 {
395 wxLogError(_("Locale \"%s\" is unknown."), s_locale.c_str());
396 return;
397 }
398
399 if ( wxLocale::IsAvailable(info->Language) )
400 wxLogMessage(_("Locale \"%s\" is available."), s_locale.c_str());
401 else
402 wxLogWarning(_("Locale \"%s\" is not available."), s_locale.c_str());
403}
404
405void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
c7f3b78b 406{
849a28d0
VS
407 // open a bogus file -- the error message should be also translated if
408 // you've got wxstd.mo somewhere in the search path
aec18ff7 409 wxFile file(wxT("NOTEXIST.ING"));
071cc2be 410}
085c26ac 411
849a28d0
VS
412void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
413{
79478afa 414 const wxString title = _("Testing _() (gettext)");
849a28d0 415 wxTextEntryDialog d(this, _("Please enter text to translate"),
f13880a9 416 title, wxTRANSLATE("default value"));
849a28d0
VS
417 if (d.ShowModal() == wxID_OK)
418 {
f13880a9
WS
419 wxString v = d.GetValue();
420 wxString s(title);
421 s << _T("\n") << v << _T(" -> ")
422 << wxGetTranslation(v.c_str()) << _T("\n");
423 wxMessageBox(s);
849a28d0
VS
424 }
425}
426
427void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
428{
79478afa 429 const wxString title = _("Testing _N() (ngettext)");
849a28d0 430 wxTextEntryDialog d(this,
f13880a9
WS
431 _("Please enter range for plural forms of \"n files deleted\" phrase"),
432 title, _T("0-10"));
849a28d0
VS
433 if (d.ShowModal() == wxID_OK)
434 {
f13880a9
WS
435 int first, last;
436 wxSscanf(d.GetValue(), _T("%d-%d"), &first, &last);
437 wxString s(title);
438 s << _T("\n");
439 for (int n = first; n <= last; ++n)
849a28d0 440 {
f13880a9 441 s << n << _T(" ") <<
15d06954 442 wxPLURAL("file deleted", "files deleted", n) <<
552973e0 443 _T("\n");
f13880a9 444 }
849a28d0
VS
445 wxMessageBox(s);
446 }
447}
448
449void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
450{
ad4814e7 451 const char* lines[] =
849a28d0 452 {
f13880a9
WS
453 wxTRANSLATE("line 1"),
454 wxTRANSLATE("line 2"),
455 wxTRANSLATE("line 3"),
849a28d0 456 };
4c3590e4 457 wxString s(_("Testing wxTRANSLATE() (gettext_noop)"));
849a28d0
VS
458 s << _T("\n");
459 for (size_t i = 0; i < WXSIZEOF(lines); ++i)
460 {
f13880a9 461 s << lines[i] << _T(" -> ") << wxGetTranslation(lines[i]) << _T("\n");
849a28d0
VS
462 }
463 wxMessageBox(s);
464}
465
466