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