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