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