allow choosing language from the command line; small cleanup
[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 and Markus Holzem
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
65 DECLARE_EVENT_TABLE()
66
67 wxLocale& m_locale;
68 };
69
70 // ----------------------------------------------------------------------------
71 // constants
72 // ----------------------------------------------------------------------------
73
74 // ID for the menu commands
75 enum
76 {
77 INTERNAT_QUIT = 1,
78 INTERNAT_TEXT,
79 INTERNAT_TEST,
80 INTERNAT_OPEN
81 };
82
83 // ----------------------------------------------------------------------------
84 // wxWindows macros
85 // ----------------------------------------------------------------------------
86
87 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
88 EVT_MENU(INTERNAT_QUIT, MyFrame::OnQuit)
89 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
90 EVT_MENU(INTERNAT_TEST, MyFrame::OnPlay)
91 EVT_MENU(INTERNAT_OPEN, MyFrame::OnOpen)
92 END_EVENT_TABLE()
93
94 IMPLEMENT_APP(MyApp)
95
96 // ============================================================================
97 // implementation
98 // ============================================================================
99
100 // ----------------------------------------------------------------------------
101 // MyApp
102 // ----------------------------------------------------------------------------
103
104 // `Main program' equivalent, creating windows and returning main app frame
105 bool MyApp::OnInit()
106 {
107 long lng = -1;
108
109 if ( argc == 2 )
110 {
111 // the parameter must be the lang index
112 wxString(argv[1]).ToLong(&lng);
113 }
114
115 if ( lng == -1 )
116 {
117 const wxString langs[] =
118 {
119 _T("(System default)"),
120 _T("French"),
121 _T("German"),
122 _T("Russian"),
123 _T("English"),
124 _T("English (U.S.)")
125 };
126
127 lng = wxGetSingleChoiceIndex
128 (
129 _T("Please choose language:"),
130 _T("Language"),
131 WXSIZEOF(langs),
132 langs
133 );
134 }
135
136 switch ( lng )
137 {
138 case 0 : m_locale.Init(wxLANGUAGE_DEFAULT); break;
139 case 1 : m_locale.Init(wxLANGUAGE_FRENCH); break;
140 case 2 : m_locale.Init(wxLANGUAGE_GERMAN); break;
141 case 3 : m_locale.Init(wxLANGUAGE_RUSSIAN); break;
142 case 4 : m_locale.Init(wxLANGUAGE_ENGLISH); break;
143 case -1:
144 case 5 : m_locale.Init(wxLANGUAGE_ENGLISH_US); break;
145 }
146
147
148 // Initialize the catalogs we'll be using
149 m_locale.AddCatalog(wxT("internat"));
150
151 // this catalog is installed in standard location on Linux systems and
152 // shows that you may make use of the standard message catalogs as well
153 //
154 // if it's not installed on your system, it is just silently ignored
155 #ifdef __LINUX__
156 {
157 wxLogNull noLog;
158 m_locale.AddCatalog(_T("fileutils"));
159 }
160 #endif
161
162 // Create the main frame window
163 MyFrame *frame = new MyFrame(m_locale);
164
165 // Give it an icon
166 frame->SetIcon(wxICON(mondrian));
167
168 // Make a menubar
169 wxMenu *file_menu = new wxMenu;
170 file_menu->Append(wxID_ABOUT, _("&About..."));
171 file_menu->AppendSeparator();
172 file_menu->Append(INTERNAT_QUIT, _("E&xit"));
173
174 wxMenu *test_menu = new wxMenu;
175 test_menu->Append(INTERNAT_OPEN, _("&Open bogus file"));
176 test_menu->Append(INTERNAT_TEST, _("&Play a game"));
177
178 wxMenuBar *menu_bar = new wxMenuBar;
179 menu_bar->Append(file_menu, _("&File"));
180 menu_bar->Append(test_menu, _("&Test"));
181 frame->SetMenuBar(menu_bar);
182
183 // Show the frame
184 frame->Show(TRUE);
185 SetTopWindow(frame);
186
187 return TRUE;
188 }
189
190 // ----------------------------------------------------------------------------
191 // MyFrame
192 // ----------------------------------------------------------------------------
193
194 // main frame constructor
195 MyFrame::MyFrame(wxLocale& locale)
196 : wxFrame(NULL,
197 -1,
198 _("International wxWindows App"),
199 wxPoint(50, 50),
200 wxSize(350, 60)),
201 m_locale(locale)
202 {
203 // Empty
204 }
205
206 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
207 {
208 Close(TRUE);
209 }
210
211 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
212 {
213 wxString localeInfo;
214 wxString locale = m_locale.GetLocale();
215 wxString sysname = m_locale.GetSysName();
216 wxString canname = m_locale.GetCanonicalName();
217
218 localeInfo.Printf( _("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"),
219 locale.c_str(), sysname.c_str(), canname.c_str() );
220
221 wxMessageDialog(this, wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
222 + wxT("\n\n") + localeInfo,
223 _("About Internat"), wxOK | wxICON_INFORMATION).ShowModal();
224 }
225
226 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
227 {
228 wxString str = wxGetTextFromUser(_("Enter your number:"),
229 _("Try to guess my number!"), wxEmptyString, this);
230
231 if ( str.IsEmpty() ) return;
232
233 int num;
234 wxSscanf(str, wxT("%d"), &num);
235 if ( num == 0 )
236 str = _("You've probably entered an invalid number.");
237 else if ( num == 9 ) // this message is not translated (not in catalog)
238 str = _T("You've found a bug in this program!");
239 else if ( num != 17 ) // a more implicit way to write _()
240 str = wxGetTranslation(wxT("Bad luck! try again..."));
241 else
242 {
243 str.Empty();
244 // string must be split in two -- otherwise the translation won't be found
245 str << _("Congratulations! you've won. Here is the magic phrase:")
246 << _("cannot create fifo `%s'");
247 }
248
249 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
250 }
251
252 void MyFrame::OnOpen(wxCommandEvent&)
253 {
254 // open a bogus file -- the error message should be also translated if you've
255 // got wxstd.mo somewhere in the search path
256 wxFile file(wxT("NOTEXIST.ING"));
257 }