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