use wxID_OPEN instead of INTERNAT_OPEN
[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 };
86
87 // language data
88 static const wxLanguage langIds[] =
89 {
90 wxLANGUAGE_DEFAULT,
91 wxLANGUAGE_FRENCH,
92 wxLANGUAGE_GERMAN,
93 wxLANGUAGE_RUSSIAN,
94 wxLANGUAGE_BULGARIAN,
95 wxLANGUAGE_CZECH,
96 wxLANGUAGE_POLISH,
97 wxLANGUAGE_SWEDISH,
98 #if wxUSE_UNICODE || defined(__WXMOTIF__)
99 wxLANGUAGE_JAPANESE,
100 #endif
101 #if wxUSE_UNICODE
102 wxLANGUAGE_GEORGIAN,
103 wxLANGUAGE_ENGLISH,
104 wxLANGUAGE_ENGLISH_US,
105 wxLANGUAGE_ARABIC,
106 wxLANGUAGE_ARABIC_EGYPT
107 #endif
108 };
109
110 // note that it makes no sense to translate these strings, they are
111 // shown before we set the locale anyhow
112 const wxString langNames[] =
113 {
114 _T("System default"),
115 _T("French"),
116 _T("German"),
117 _T("Russian"),
118 _T("Bulgarian"),
119 _T("Czech"),
120 _T("Polish"),
121 _T("Swedish"),
122 #if wxUSE_UNICODE || defined(__WXMOTIF__)
123 _T("Japanese"),
124 #endif
125 #if wxUSE_UNICODE
126 _T("Georgian"),
127 _T("English"),
128 _T("English (U.S.)"),
129 _T("Arabic"),
130 _T("Arabic (Egypt)")
131 #endif
132 };
133
134 // the arrays must be in sync
135 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
136 LangArraysMismatch );
137
138 // ----------------------------------------------------------------------------
139 // wxWidgets macros
140 // ----------------------------------------------------------------------------
141
142 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
143 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
144 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
145 EVT_MENU(INTERNAT_TEST, MyFrame::OnPlay)
146 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
147 EVT_MENU(INTERNAT_TEST_1, MyFrame::OnTest1)
148 EVT_MENU(INTERNAT_TEST_2, MyFrame::OnTest2)
149 EVT_MENU(INTERNAT_TEST_3, MyFrame::OnTest3)
150 END_EVENT_TABLE()
151
152 IMPLEMENT_APP(MyApp)
153
154 // ============================================================================
155 // implementation
156 // ============================================================================
157
158 // ----------------------------------------------------------------------------
159 // MyApp
160 // ----------------------------------------------------------------------------
161
162 // `Main program' equivalent, creating windows and returning main app frame
163 bool MyApp::OnInit()
164 {
165 long lng = -1;
166
167 if ( argc == 2 )
168 {
169 // the parameter must be the lang index
170 wxString tmp(argv[1]);
171 tmp.ToLong(&lng);
172 }
173
174 if ( lng == -1 )
175 {
176 lng = wxGetSingleChoiceIndex
177 (
178 _T("Please choose language:"),
179 _T("Language"),
180 WXSIZEOF(langNames),
181 langNames
182 );
183 }
184
185 if ( lng != -1 )
186 {
187 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
188 // false just because it failed to load wxstd catalog
189 if ( !m_locale.Init(langIds[lng], wxLOCALE_CONV_ENCODING) )
190 {
191 wxLogError(_T("This language is not supported by the system."));
192 return false;
193 }
194 }
195
196 // normally this wouldn't be necessary as the catalog files would be found
197 // in the default locations, but when the program is not installed the
198 // catalogs are in the build directory where we wouldn't find them by
199 // default
200 wxLocale::AddCatalogLookupPathPrefix(wxT("."));
201
202 // Initialize the catalogs we'll be using
203 m_locale.AddCatalog(wxT("internat"));
204
205 // this catalog is installed in standard location on Linux systems and
206 // shows that you may make use of the standard message catalogs as well
207 //
208 // if it's not installed on your system, it is just silently ignored
209 #ifdef __LINUX__
210 {
211 wxLogNull noLog;
212 m_locale.AddCatalog(_T("fileutils"));
213 }
214 #endif
215
216 // Create the main frame window
217 MyFrame *frame = new MyFrame(m_locale);
218
219 // Give it an icon
220 frame->SetIcon(wxICON(mondrian));
221
222 // Make a menubar
223 wxMenu *file_menu = new wxMenu;
224 file_menu->Append(wxID_ABOUT, _("&About..."));
225 file_menu->AppendSeparator();
226 file_menu->Append(wxID_EXIT, _("E&xit"));
227
228 wxMenu *test_menu = new wxMenu;
229 test_menu->Append(wxID_OPEN, _("&Open bogus file"));
230 test_menu->Append(INTERNAT_TEST, _("&Play a game"));
231 test_menu->AppendSeparator();
232 test_menu->Append(INTERNAT_TEST_1, _("&1 _() (gettext)"));
233 test_menu->Append(INTERNAT_TEST_2, _("&2 _N() (ngettext)"));
234 test_menu->Append(INTERNAT_TEST_3, _("&3 wxTRANSLATE() (gettext_noop)"));
235
236 wxMenuBar *menu_bar = new wxMenuBar;
237 menu_bar->Append(file_menu, _("&File"));
238 menu_bar->Append(test_menu, _("&Test"));
239 frame->SetMenuBar(menu_bar);
240
241 // Show the frame
242 frame->Show(true);
243 SetTopWindow(frame);
244
245 return true;
246 }
247
248 // ----------------------------------------------------------------------------
249 // MyFrame
250 // ----------------------------------------------------------------------------
251
252 // main frame constructor
253 MyFrame::MyFrame(wxLocale& locale)
254 : wxFrame(NULL,
255 wxID_ANY,
256 _("International wxWidgets App")),
257 m_locale(locale)
258 {
259 // this demonstrates RTL layout mirroring for Arabic locales
260 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
261 sizer->Add(new wxStaticText(this, wxID_ANY, _("First")),
262 wxSizerFlags().Border());
263 sizer->Add(new wxStaticText(this, wxID_ANY, _("Second")),
264 wxSizerFlags().Border());
265 SetSizer(sizer);
266 }
267
268 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
269 {
270 Close(true);
271 }
272
273 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
274 {
275 wxString localeInfo;
276 wxString locale = m_locale.GetLocale();
277 wxString sysname = m_locale.GetSysName();
278 wxString canname = m_locale.GetCanonicalName();
279
280 localeInfo.Printf(_("Language: %s\nSystem locale name:\n%s\nCanonical locale name: %s\n"),
281 locale.c_str(), sysname.c_str(), canname.c_str() );
282
283 wxMessageDialog dlg(
284 this,
285 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
286 + wxT("\n\n")
287 + localeInfo,
288 _("About Internat"),
289 wxOK | wxICON_INFORMATION
290 );
291 dlg.ShowModal();
292 }
293
294 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
295 {
296 wxString str = wxGetTextFromUser
297 (
298 _("Enter your number:"),
299 _("Try to guess my number!"),
300 wxEmptyString,
301 this
302 );
303
304 if ( str.empty() )
305 {
306 // cancelled
307 return;
308 }
309
310 long num;
311 if ( !str.ToLong(&num) || num < 0 )
312 {
313 str = _("You've probably entered an invalid number.");
314 }
315 else if ( num == 9 )
316 {
317 // this message is not translated (not in catalog) because we used _T()
318 // and not _() around it
319 str = _T("You've found a bug in this program!");
320 }
321 else if ( num == 17 )
322 {
323 str.clear();
324
325 // string must be split in two -- otherwise the translation would't be
326 // found
327 str << _("Congratulations! you've won. Here is the magic phrase:")
328 << _("cannot create fifo `%s'");
329 }
330 else
331 {
332 // this is a more implicit way to write _() but note that if you use it
333 // you must ensure that the strings get extracted in the message
334 // catalog as by default xgettext won't do it (it only knows of _(),
335 // not wxGetTranslation())
336 str = wxGetTranslation(_T("Bad luck! try again..."));
337 }
338
339 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
340 }
341
342 void MyFrame::OnOpen(wxCommandEvent&)
343 {
344 // open a bogus file -- the error message should be also translated if
345 // you've got wxstd.mo somewhere in the search path
346 wxFile file(wxT("NOTEXIST.ING"));
347 }
348
349 void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
350 {
351 const wxChar* title = _("Testing _() (gettext)");
352 wxTextEntryDialog d(this, _("Please enter text to translate"),
353 title, wxTRANSLATE("default value"));
354 if (d.ShowModal() == wxID_OK)
355 {
356 wxString v = d.GetValue();
357 wxString s(title);
358 s << _T("\n") << v << _T(" -> ")
359 << wxGetTranslation(v.c_str()) << _T("\n");
360 wxMessageBox(s);
361 }
362 }
363
364 void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
365 {
366 const wxChar* title = _("Testing _N() (ngettext)");
367 wxTextEntryDialog d(this,
368 _("Please enter range for plural forms of \"n files deleted\" phrase"),
369 title, _T("0-10"));
370 if (d.ShowModal() == wxID_OK)
371 {
372 int first, last;
373 wxSscanf(d.GetValue(), _T("%d-%d"), &first, &last);
374 wxString s(title);
375 s << _T("\n");
376 for (int n = first; n <= last; ++n)
377 {
378 s << n << _T(" ") <<
379 wxPLURAL("file deleted", "files deleted", n) <<
380 _T("\n");
381 }
382 wxMessageBox(s);
383 }
384 }
385
386 void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
387 {
388 const wxChar* lines[] =
389 {
390 wxTRANSLATE("line 1"),
391 wxTRANSLATE("line 2"),
392 wxTRANSLATE("line 3"),
393 };
394 wxString s(_("Testing wxTRANSLATE() (gettext_noop)"));
395 s << _T("\n");
396 for (size_t i = 0; i < WXSIZEOF(lines); ++i)
397 {
398 s << lines[i] << _T(" -> ") << wxGetTranslation(lines[i]) << _T("\n");
399 }
400 wxMessageBox(s);
401 }
402
403