fixed the sample to work with conforming C++ compiler: local struct can't reference...
[wxWidgets.git] / samples / internat / internat.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: internat.cpp
3 // Purpose: Demonstrates internationalisation (i18n) support
4 // Author: Vadim Zeitlin/Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
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"
32 #include "wx/file.h"
33 #include "wx/log.h"
34
35 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
36 #include "mondrian.xpm"
37 #endif
38
39 // ----------------------------------------------------------------------------
40 // private classes
41 // ----------------------------------------------------------------------------
42
43 // Define a new application type
44 class MyApp: public wxApp
45 {
46 public:
47 virtual bool OnInit();
48
49 protected:
50 wxLocale m_locale; // locale we'll be using
51 };
52
53 // Define a new frame type
54 class MyFrame: public wxFrame
55 {
56 public:
57 MyFrame(wxLocale& m_locale);
58
59 public:
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);
67
68 DECLARE_EVENT_TABLE()
69
70 wxLocale& m_locale;
71 };
72
73 // ----------------------------------------------------------------------------
74 // constants
75 // ----------------------------------------------------------------------------
76
77 // ID for the menu commands
78 enum
79 {
80 INTERNAT_TEXT = wxID_HIGHEST + 1,
81 INTERNAT_TEST,
82 INTERNAT_TEST_1,
83 INTERNAT_TEST_2,
84 INTERNAT_TEST_3,
85 INTERNAT_OPEN
86 };
87
88 // language data
89 static 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
111 const 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
132 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
133 LangArraysMismatch );
134
135 // ----------------------------------------------------------------------------
136 // wxWidgets macros
137 // ----------------------------------------------------------------------------
138
139 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
140 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
141 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
142 EVT_MENU(INTERNAT_TEST, MyFrame::OnPlay)
143 EVT_MENU(INTERNAT_OPEN, MyFrame::OnOpen)
144 EVT_MENU(INTERNAT_TEST_1, MyFrame::OnTest1)
145 EVT_MENU(INTERNAT_TEST_2, MyFrame::OnTest2)
146 EVT_MENU(INTERNAT_TEST_3, MyFrame::OnTest3)
147 END_EVENT_TABLE()
148
149 IMPLEMENT_APP(MyApp)
150
151 // ============================================================================
152 // implementation
153 // ============================================================================
154
155 // ----------------------------------------------------------------------------
156 // MyApp
157 // ----------------------------------------------------------------------------
158
159 // `Main program' equivalent, creating windows and returning main app frame
160 bool MyApp::OnInit()
161 {
162 long lng = -1;
163
164 if ( argc == 2 )
165 {
166 // the parameter must be the lang index
167 wxString tmp(argv[1]);
168 tmp.ToLong(&lng);
169 }
170
171 if ( lng == -1 )
172 {
173 lng = wxGetSingleChoiceIndex
174 (
175 _T("Please choose language:"),
176 _T("Language"),
177 WXSIZEOF(langNames),
178 langNames
179 );
180 }
181
182 if ( lng != -1 )
183 m_locale.Init(langIds[lng]);
184
185 // normally this wouldn't be necessary as the catalog files would be found
186 // in the default locations, but under Windows then the program is not
187 // installed the catalogs are in the parent directory (because the binary
188 // is in a subdirectory of samples/internat) where we wouldn't find them by
189 // default
190 wxLocale::AddCatalogLookupPathPrefix(wxT("."));
191 wxLocale::AddCatalogLookupPathPrefix(wxT(".."));
192
193 // Initialize the catalogs we'll be using
194 m_locale.AddCatalog(wxT("internat"));
195
196 // this catalog is installed in standard location on Linux systems and
197 // shows that you may make use of the standard message catalogs as well
198 //
199 // if it's not installed on your system, it is just silently ignored
200 #ifdef __LINUX__
201 {
202 wxLogNull noLog;
203 m_locale.AddCatalog(_T("fileutils"));
204 }
205 #endif
206
207 // Create the main frame window
208 MyFrame *frame = new MyFrame(m_locale);
209
210 // Give it an icon
211 frame->SetIcon(wxICON(mondrian));
212
213 // Make a menubar
214 wxMenu *file_menu = new wxMenu;
215 file_menu->Append(wxID_ABOUT, _("&About..."));
216 file_menu->AppendSeparator();
217 file_menu->Append(wxID_EXIT, _("E&xit"));
218
219 wxMenu *test_menu = new wxMenu;
220 test_menu->Append(INTERNAT_OPEN, _("&Open bogus file"));
221 test_menu->Append(INTERNAT_TEST, _("&Play a game"));
222 test_menu->AppendSeparator();
223 test_menu->Append(INTERNAT_TEST_1, _("&1 _() (gettext)"));
224 test_menu->Append(INTERNAT_TEST_2, _("&2 _N() (ngettext)"));
225 test_menu->Append(INTERNAT_TEST_3, _("&3 wxTRANSLATE() (gettext_noop)"));
226
227 wxMenuBar *menu_bar = new wxMenuBar;
228 menu_bar->Append(file_menu, _("&File"));
229 menu_bar->Append(test_menu, _("&Test"));
230 frame->SetMenuBar(menu_bar);
231
232 // Show the frame
233 frame->Show(true);
234 SetTopWindow(frame);
235
236 return true;
237 }
238
239 // ----------------------------------------------------------------------------
240 // MyFrame
241 // ----------------------------------------------------------------------------
242
243 // main frame constructor
244 MyFrame::MyFrame(wxLocale& locale)
245 : wxFrame(NULL,
246 wxID_ANY,
247 _("International wxWidgets App")),
248 m_locale(locale)
249 {
250 // Empty
251 }
252
253 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
254 {
255 Close(true);
256 }
257
258 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
259 {
260 wxString localeInfo;
261 wxString locale = m_locale.GetLocale();
262 wxString sysname = m_locale.GetSysName();
263 wxString canname = m_locale.GetCanonicalName();
264
265 localeInfo.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
266 locale.c_str(), sysname.c_str(), canname.c_str() );
267
268 wxMessageDialog dlg(
269 this,
270 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
271 + wxT("\n\n")
272 + localeInfo,
273 _("About Internat"),
274 wxOK | wxICON_INFORMATION
275 );
276 dlg.ShowModal();
277 }
278
279 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
280 {
281 wxString str = wxGetTextFromUser
282 (
283 _("Enter your number:"),
284 _("Try to guess my number!"),
285 wxEmptyString,
286 this
287 );
288
289 if ( str.empty() )
290 {
291 // cancelled
292 return;
293 }
294
295 long num;
296 if ( !str.ToLong(&num) || num < 0 )
297 {
298 str = _("You've probably entered an invalid number.");
299 }
300 else if ( num == 9 )
301 {
302 // this message is not translated (not in catalog) because we used _T()
303 // and not _() around it
304 str = _T("You've found a bug in this program!");
305 }
306 else if ( num == 17 )
307 {
308 str.clear();
309
310 // string must be split in two -- otherwise the translation would't be
311 // found
312 str << _("Congratulations! you've won. Here is the magic phrase:")
313 << _("cannot create fifo `%s'");
314 }
315 else
316 {
317 // this is a more implicit way to write _() but note that if you use it
318 // you must ensure that the strings get extracted in the message
319 // catalog as by default xgettext won't do it (it only knows of _(),
320 // not wxGetTranslation())
321 str = wxGetTranslation(_T("Bad luck! try again..."));
322 }
323
324 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
325 }
326
327 void MyFrame::OnOpen(wxCommandEvent&)
328 {
329 // open a bogus file -- the error message should be also translated if
330 // you've got wxstd.mo somewhere in the search path
331 wxFile file(wxT("NOTEXIST.ING"));
332 }
333
334 void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
335 {
336 const wxChar* title = _("Testing _() (gettext)");
337 wxTextEntryDialog d(this, _("Please enter text to translate"),
338 title, wxTRANSLATE("default value"));
339 if (d.ShowModal() == wxID_OK)
340 {
341 wxString v = d.GetValue();
342 wxString s(title);
343 s << _T("\n") << v << _T(" -> ")
344 << wxGetTranslation(v.c_str()) << _T("\n");
345 wxMessageBox(s);
346 }
347 }
348
349 void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
350 {
351 const wxChar* title = _("Testing _N() (ngettext)");
352 wxTextEntryDialog d(this,
353 _("Please enter range for plural forms of \"n files deleted\" phrase"),
354 title, _T("0-10"));
355 if (d.ShowModal() == wxID_OK)
356 {
357 int first, last;
358 wxSscanf(d.GetValue(), _T("%d-%d"), &first, &last);
359 wxString s(title);
360 s << _T("\n");
361 for (int n = first; n <= last; ++n)
362 {
363 s << n << _T(" ") <<
364 wxPLURAL("file deleted", "files deleted", n) <<
365 _T("\n");
366 }
367 wxMessageBox(s);
368 }
369 }
370
371 void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
372 {
373 const wxChar* lines[] =
374 {
375 wxTRANSLATE("line 1"),
376 wxTRANSLATE("line 2"),
377 wxTRANSLATE("line 3"),
378 };
379 wxString s(_("Testing wxTRANSLATE() (gettext_noop)"));
380 s << _T("\n");
381 for (size_t i = 0; i < WXSIZEOF(lines); ++i)
382 {
383 s << lines[i] << _T(" -> ") << wxGetTranslation(lines[i]) << _T("\n");
384 }
385 wxMessageBox(s);
386 }
387
388