Warning fixes found under hardest mode of OpenWatcom. Seems clean in Borland, MinGW...
[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 // ----------------------------------------------------------------------------
89 // wxWidgets macros
90 // ----------------------------------------------------------------------------
91
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)
100 END_EVENT_TABLE()
101
102 IMPLEMENT_APP(MyApp)
103
104 // ============================================================================
105 // implementation
106 // ============================================================================
107
108 // ----------------------------------------------------------------------------
109 // MyApp
110 // ----------------------------------------------------------------------------
111
112 // `Main program' equivalent, creating windows and returning main app frame
113 bool MyApp::OnInit()
114 {
115 long lng = -1;
116
117 if ( argc == 2 )
118 {
119 // the parameter must be the lang index
120 wxString tmp(argv[1]);
121 tmp.ToLong(&lng);
122 }
123
124 static const wxLanguage langIds[] =
125 {
126 wxLANGUAGE_DEFAULT,
127 wxLANGUAGE_FRENCH,
128 wxLANGUAGE_GERMAN,
129 wxLANGUAGE_RUSSIAN,
130 wxLANGUAGE_BULGARIAN,
131 wxLANGUAGE_CZECH,
132 wxLANGUAGE_POLISH,
133 #if wxUSE_UNICODE
134 wxLANGUAGE_JAPANESE,
135 wxLANGUAGE_GEORGIAN,
136 #endif
137 wxLANGUAGE_ENGLISH,
138 wxLANGUAGE_ENGLISH_US
139 };
140
141 if ( lng == -1 )
142 {
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[] =
146 {
147 _T("System default"),
148 _T("French"),
149 _T("German"),
150 _T("Russian"),
151 _T("Bulgarian"),
152 _T("Czech"),
153 _T("Polish"),
154 #if wxUSE_UNICODE
155 _T("Japanese"),
156 _T("Georgian"),
157 #endif
158 _T("English"),
159 _T("English (U.S.)")
160 };
161
162 // the arrays should be in sync
163 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
164 LangArraysMismatch );
165
166 lng = wxGetSingleChoiceIndex
167 (
168 _T("Please choose language:"),
169 _T("Language"),
170 WXSIZEOF(langNames),
171 langNames
172 );
173 }
174
175 if ( lng != -1 )
176 m_locale.Init(langIds[lng]);
177
178
179 // Initialize the catalogs we'll be using
180 m_locale.AddCatalog(wxT("internat"));
181
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
184 //
185 // if it's not installed on your system, it is just silently ignored
186 #ifdef __LINUX__
187 {
188 wxLogNull noLog;
189 m_locale.AddCatalog(_T("fileutils"));
190 }
191 #endif
192
193 // Create the main frame window
194 MyFrame *frame = new MyFrame(m_locale);
195
196 // Give it an icon
197 frame->SetIcon(wxICON(mondrian));
198
199 // Make a menubar
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"));
204
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)"));
212
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);
217
218 // Show the frame
219 frame->Show(true);
220 SetTopWindow(frame);
221
222 return true;
223 }
224
225 // ----------------------------------------------------------------------------
226 // MyFrame
227 // ----------------------------------------------------------------------------
228
229 // main frame constructor
230 MyFrame::MyFrame(wxLocale& locale)
231 : wxFrame(NULL,
232 wxID_ANY,
233 _("International wxWidgets App")),
234 m_locale(locale)
235 {
236 // Empty
237 }
238
239 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
240 {
241 Close(true);
242 }
243
244 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
245 {
246 wxString localeInfo;
247 wxString locale = m_locale.GetLocale();
248 wxString sysname = m_locale.GetSysName();
249 wxString canname = m_locale.GetCanonicalName();
250
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() );
253
254 wxMessageDialog dlg(
255 this,
256 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
257 + wxT("\n\n")
258 + localeInfo,
259 _("About Internat"),
260 wxOK | wxICON_INFORMATION
261 );
262 dlg.ShowModal();
263 }
264
265 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
266 {
267 wxString str = wxGetTextFromUser
268 (
269 _("Enter your number:"),
270 _("Try to guess my number!"),
271 wxEmptyString,
272 this
273 );
274
275 if ( str.empty() )
276 {
277 // cancelled
278 return;
279 }
280
281 long num;
282 if ( !str.ToLong(&num) || num < 0 )
283 {
284 str = _("You've probably entered an invalid number.");
285 }
286 else if ( num == 9 )
287 {
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!");
291 }
292 else if ( num == 17 )
293 {
294 str.clear();
295
296 // string must be split in two -- otherwise the translation would't be
297 // found
298 str << _("Congratulations! you've won. Here is the magic phrase:")
299 << _("cannot create fifo `%s'");
300 }
301 else
302 {
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..."));
308 }
309
310 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
311 }
312
313 void MyFrame::OnOpen(wxCommandEvent&)
314 {
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"));
318 }
319
320 void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
321 {
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)
326 {
327 wxString v = d.GetValue();
328 wxString s(title);
329 s << _T("\n") << v << _T(" -> ")
330 << wxGetTranslation(v.c_str()) << _T("\n");
331 wxMessageBox(s);
332 }
333 }
334
335 void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
336 {
337 const wxChar* title = _("Testing _N() (ngettext)");
338 wxTextEntryDialog d(this,
339 _("Please enter range for plural forms of \"n files deleted\" phrase"),
340 title, _T("0-10"));
341 if (d.ShowModal() == wxID_OK)
342 {
343 int first, last;
344 wxSscanf(d.GetValue(), _T("%d-%d"), &first, &last);
345 wxString s(title);
346 s << _T("\n");
347 for (int n = first; n <= last; ++n)
348 {
349 s << n << _T(" ") <<
350 wxGetTranslation(_T("file deleted"), _T("files deleted"), n) <<
351 _T("\n");
352 }
353 wxMessageBox(s);
354 }
355 }
356
357 void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
358 {
359 const wxChar* lines[] =
360 {
361 wxTRANSLATE("line 1"),
362 wxTRANSLATE("line 2"),
363 wxTRANSLATE("line 3"),
364 };
365 wxString s(_("Testing wxTRANSLATE() (gettext_noop)"));
366 s << _T("\n");
367 for (size_t i = 0; i < WXSIZEOF(lines); ++i)
368 {
369 s << lines[i] << _T(" -> ") << wxGetTranslation(lines[i]) << _T("\n");
370 }
371 wxMessageBox(s);
372 }
373
374