]> git.saurik.com Git - wxWidgets.git/blame - samples/internat/internat.cpp
Untabified.
[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
28#include "wx/wx.h"
29#endif
30
31#include "wx/intl.h"
c7f3b78b 32#include "wx/file.h"
c801d85f
KB
33#include "wx/log.h"
34
618f2efa 35#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
47908e25
RR
36#include "mondrian.xpm"
37#endif
38
017fcf32
VZ
39// ----------------------------------------------------------------------------
40// private classes
41// ----------------------------------------------------------------------------
42
c801d85f
KB
43// Define a new application type
44class MyApp: public wxApp
45{
46public:
aec18ff7 47 virtual bool OnInit();
c7f3b78b 48
c801d85f 49protected:
aec18ff7 50 wxLocale m_locale; // locale we'll be using
c801d85f
KB
51};
52
53// Define a new frame type
54class MyFrame: public wxFrame
aec18ff7 55{
c7f3b78b 56public:
017fcf32 57 MyFrame(wxLocale& m_locale);
c7f3b78b
VZ
58
59public:
aec18ff7
MB
60 void OnQuit(wxCommandEvent& event);
61 void OnAbout(wxCommandEvent& event);
62 void OnPlay(wxCommandEvent& event);
63 void OnOpen(wxCommandEvent& event);
849a28d0
VS
64 void OnTest1(wxCommandEvent& event);
65 void OnTest2(wxCommandEvent& event);
66 void OnTest3(wxCommandEvent& event);
67
aec18ff7
MB
68 DECLARE_EVENT_TABLE()
69
70 wxLocale& m_locale;
c801d85f
KB
71};
72
017fcf32
VZ
73// ----------------------------------------------------------------------------
74// constants
75// ----------------------------------------------------------------------------
76
c801d85f 77// ID for the menu commands
c7f3b78b
VZ
78enum
79{
017fcf32
VZ
80 INTERNAT_QUIT = 1,
81 INTERNAT_TEXT,
82 INTERNAT_TEST,
849a28d0
VS
83 INTERNAT_TEST_1,
84 INTERNAT_TEST_2,
85 INTERNAT_TEST_3,
017fcf32 86 INTERNAT_OPEN
c7f3b78b 87};
c801d85f 88
017fcf32
VZ
89// ----------------------------------------------------------------------------
90// wxWindows macros
91// ----------------------------------------------------------------------------
92
c801d85f 93BEGIN_EVENT_TABLE(MyFrame, wxFrame)
017fcf32 94 EVT_MENU(INTERNAT_QUIT, MyFrame::OnQuit)
aec18ff7 95 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
017fcf32
VZ
96 EVT_MENU(INTERNAT_TEST, MyFrame::OnPlay)
97 EVT_MENU(INTERNAT_OPEN, MyFrame::OnOpen)
849a28d0
VS
98 EVT_MENU(INTERNAT_TEST_1, MyFrame::OnTest1)
99 EVT_MENU(INTERNAT_TEST_2, MyFrame::OnTest2)
100 EVT_MENU(INTERNAT_TEST_3, MyFrame::OnTest3)
c801d85f
KB
101END_EVENT_TABLE()
102
103IMPLEMENT_APP(MyApp)
104
017fcf32
VZ
105// ============================================================================
106// implementation
107// ============================================================================
108
109// ----------------------------------------------------------------------------
110// MyApp
111// ----------------------------------------------------------------------------
c801d85f 112
071cc2be 113// `Main program' equivalent, creating windows and returning main app frame
fd323a5e 114bool MyApp::OnInit()
c801d85f 115{
017fcf32
VZ
116 long lng = -1;
117
118 if ( argc == 2 )
aec18ff7 119 {
017fcf32 120 // the parameter must be the lang index
5c0ee4a8
MB
121 wxString tmp(argv[1]);
122 tmp.ToLong(&lng);
017fcf32
VZ
123 }
124
a2f26963
VZ
125 static const wxLanguage langIds[] =
126 {
127 wxLANGUAGE_DEFAULT,
128 wxLANGUAGE_FRENCH,
129 wxLANGUAGE_GERMAN,
130 wxLANGUAGE_RUSSIAN,
b28b097d 131 wxLANGUAGE_BULGARIAN,
849a28d0 132 wxLANGUAGE_CZECH,
65dc921d 133#if wxUSE_UNICODE
a2f26963 134 wxLANGUAGE_JAPANESE,
65dc921d
VS
135 wxLANGUAGE_GEORGIAN,
136#endif
a2f26963 137 wxLANGUAGE_ENGLISH,
65dc921d 138 wxLANGUAGE_ENGLISH_US
a2f26963
VZ
139 };
140
017fcf32
VZ
141 if ( lng == -1 )
142 {
a2f26963
VZ
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[] =
017fcf32 146 {
a2f26963 147 _T("System default"),
017fcf32
VZ
148 _T("French"),
149 _T("German"),
150 _T("Russian"),
b28b097d 151 _T("Bulgarian"),
849a28d0 152 _T("Czech"),
65dc921d
VS
153#if wxUSE_UNICODE
154 _T("Japanese"),
96c0a516 155 _T("Georgian"),
65dc921d 156#endif
017fcf32
VZ
157 _T("English"),
158 _T("English (U.S.)")
159 };
160
a2f26963
VZ
161 // the arrays should be in sync
162 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
163 LangArraysMismatch );
164
017fcf32
VZ
165 lng = wxGetSingleChoiceIndex
166 (
167 _T("Please choose language:"),
168 _T("Language"),
a2f26963
VZ
169 WXSIZEOF(langNames),
170 langNames
017fcf32
VZ
171 );
172 }
173
a2f26963
VZ
174 if ( lng != -1 )
175 m_locale.Init(langIds[lng]);
aec18ff7 176
017fcf32 177
aec18ff7 178 // Initialize the catalogs we'll be using
017fcf32
VZ
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
8e97b17b 185#ifdef __LINUX__
aec18ff7 186 {
017fcf32
VZ
187 wxLogNull noLog;
188 m_locale.AddCatalog(_T("fileutils"));
aec18ff7 189 }
8e97b17b 190#endif
aec18ff7
MB
191
192 // Create the main frame window
017fcf32 193 MyFrame *frame = new MyFrame(m_locale);
aec18ff7
MB
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();
017fcf32 202 file_menu->Append(INTERNAT_QUIT, _("E&xit"));
aec18ff7
MB
203
204 wxMenu *test_menu = new wxMenu;
017fcf32
VZ
205 test_menu->Append(INTERNAT_OPEN, _("&Open bogus file"));
206 test_menu->Append(INTERNAT_TEST, _("&Play a game"));
849a28d0
VS
207 test_menu->AppendSeparator();
208 test_menu->Append(INTERNAT_TEST_1, _("&1 _() (gettext)"));
209 test_menu->Append(INTERNAT_TEST_2, _("&2 _N() (ngettext)"));
4c3590e4 210 test_menu->Append(INTERNAT_TEST_3, _("&3 wxTRANSLATE() (gettext_noop)"));
aec18ff7
MB
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;
c801d85f
KB
222}
223
017fcf32
VZ
224// ----------------------------------------------------------------------------
225// MyFrame
226// ----------------------------------------------------------------------------
227
228// main frame constructor
229MyFrame::MyFrame(wxLocale& locale)
230 : wxFrame(NULL,
231 -1,
232 _("International wxWindows App"),
233 wxPoint(50, 50),
234 wxSize(350, 60)),
235 m_locale(locale)
c7f3b78b 236{
aec18ff7 237 // Empty
c7f3b78b 238}
c801d85f 239
bd7d06f2 240void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
c801d85f 241{
aec18ff7 242 Close(TRUE);
c801d85f
KB
243}
244
bd7d06f2 245void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 246{
aec18ff7 247 wxString localeInfo;
2b5f62a0
VZ
248 wxString locale = m_locale.GetLocale();
249 wxString sysname = m_locale.GetSysName();
250 wxString canname = m_locale.GetCanonicalName();
251
085c26ac 252 localeInfo.Printf(_("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"),
2b5f62a0 253 locale.c_str(), sysname.c_str(), canname.c_str() );
aec18ff7 254
085c26ac
VZ
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();
c801d85f
KB
264}
265
bd7d06f2 266void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
c7f3b78b 267{
085c26ac
VZ
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 }
aec18ff7 281
085c26ac
VZ
282 long num;
283 if ( !str.ToLong(&num) || num < 0 )
284 {
aec18ff7 285 str = _("You've probably entered an invalid number.");
085c26ac
VZ
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
2b5f62a0 291 str = _T("You've found a bug in this program!");
085c26ac
VZ
292 }
293 else if ( num == 17 )
aec18ff7 294 {
085c26ac
VZ
295 str.clear();
296
297 // string must be split in two -- otherwise the translation would't be
298 // found
aec18ff7
MB
299 str << _("Congratulations! you've won. Here is the magic phrase:")
300 << _("cannot create fifo `%s'");
301 }
085c26ac
VZ
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 }
aec18ff7
MB
310
311 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
c7f3b78b
VZ
312}
313
314void MyFrame::OnOpen(wxCommandEvent&)
315{
849a28d0
VS
316 // open a bogus file -- the error message should be also translated if
317 // you've got wxstd.mo somewhere in the search path
aec18ff7 318 wxFile file(wxT("NOTEXIST.ING"));
071cc2be 319}
085c26ac 320
849a28d0
VS
321void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
322{
323 const wxChar* title = _("Testing _() (gettext)");
324 wxTextEntryDialog d(this, _("Please enter text to translate"),
4c3590e4 325 title, wxTRANSLATE("default value"));
849a28d0
VS
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
336void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
337{
338 const wxChar* title = _("Testing _N() (ngettext)");
339 wxTextEntryDialog d(this,
dacaa6f1 340 _("Please enter range for plural forms of \"n files deleted\" phrase"),
849a28d0
VS
341 title, _T("0-10"));
342 if (d.ShowModal() == wxID_OK)
343 {
344 int first, last;
345 wxSscanf(d.GetValue(), _T("%d-%d"), &first, &last);
346 wxString s(title);
347 s << _T("\n");
348 for (int n = first; n <= last; ++n)
349 {
552973e0
VS
350 s << n << _T(" ") <<
351 wxGetTranslation(_T("file deleted"), _T("files deleted"), n) <<
352 _T("\n");
849a28d0
VS
353 }
354 wxMessageBox(s);
355 }
356}
357
358void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
359{
360 const wxChar* lines[] =
361 {
4c3590e4
VS
362 wxTRANSLATE("line 1"),
363 wxTRANSLATE("line 2"),
364 wxTRANSLATE("line 3"),
849a28d0 365 };
4c3590e4 366 wxString s(_("Testing wxTRANSLATE() (gettext_noop)"));
849a28d0
VS
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