don't report setlocale failure if trying to set Unicode-only language
[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
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 tmp(argv[1]);
113 tmp.ToLong(&lng);
114 }
115
116 static const wxLanguage langIds[] =
117 {
118 wxLANGUAGE_DEFAULT,
119 wxLANGUAGE_FRENCH,
120 wxLANGUAGE_GERMAN,
121 wxLANGUAGE_RUSSIAN,
122 #if wxUSE_UNICODE
123 wxLANGUAGE_JAPANESE,
124 wxLANGUAGE_GEORGIAN,
125 #endif
126 wxLANGUAGE_ENGLISH,
127 wxLANGUAGE_ENGLISH_US
128 };
129
130 if ( lng == -1 )
131 {
132 // note that it makes no sense to translate these strings, they are
133 // shown before we set the locale anyhow
134 const wxString langNames[] =
135 {
136 _T("System default"),
137 _T("French"),
138 _T("German"),
139 _T("Russian"),
140 #if wxUSE_UNICODE
141 _T("Japanese"),
142 _T("Georgian (no translation)"),
143 #endif
144 _T("English"),
145 _T("English (U.S.)")
146 };
147
148 // the arrays should be in sync
149 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
150 LangArraysMismatch );
151
152 lng = wxGetSingleChoiceIndex
153 (
154 _T("Please choose language:"),
155 _T("Language"),
156 WXSIZEOF(langNames),
157 langNames
158 );
159 }
160
161 if ( lng != -1 )
162 m_locale.Init(langIds[lng]);
163
164
165 // Initialize the catalogs we'll be using
166 m_locale.AddCatalog(wxT("internat"));
167
168 // this catalog is installed in standard location on Linux systems and
169 // shows that you may make use of the standard message catalogs as well
170 //
171 // if it's not installed on your system, it is just silently ignored
172 #ifdef __LINUX__
173 {
174 wxLogNull noLog;
175 m_locale.AddCatalog(_T("fileutils"));
176 }
177 #endif
178
179 // Create the main frame window
180 MyFrame *frame = new MyFrame(m_locale);
181
182 // Give it an icon
183 frame->SetIcon(wxICON(mondrian));
184
185 // Make a menubar
186 wxMenu *file_menu = new wxMenu;
187 file_menu->Append(wxID_ABOUT, _("&About..."));
188 file_menu->AppendSeparator();
189 file_menu->Append(INTERNAT_QUIT, _("E&xit"));
190
191 wxMenu *test_menu = new wxMenu;
192 test_menu->Append(INTERNAT_OPEN, _("&Open bogus file"));
193 test_menu->Append(INTERNAT_TEST, _("&Play a game"));
194
195 wxMenuBar *menu_bar = new wxMenuBar;
196 menu_bar->Append(file_menu, _("&File"));
197 menu_bar->Append(test_menu, _("&Test"));
198 frame->SetMenuBar(menu_bar);
199
200 // Show the frame
201 frame->Show(TRUE);
202 SetTopWindow(frame);
203
204 return TRUE;
205 }
206
207 // ----------------------------------------------------------------------------
208 // MyFrame
209 // ----------------------------------------------------------------------------
210
211 // main frame constructor
212 MyFrame::MyFrame(wxLocale& locale)
213 : wxFrame(NULL,
214 -1,
215 _("International wxWindows App"),
216 wxPoint(50, 50),
217 wxSize(350, 60)),
218 m_locale(locale)
219 {
220 // Empty
221 }
222
223 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
224 {
225 Close(TRUE);
226 }
227
228 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
229 {
230 wxString localeInfo;
231 wxString locale = m_locale.GetLocale();
232 wxString sysname = m_locale.GetSysName();
233 wxString canname = m_locale.GetCanonicalName();
234
235 localeInfo.Printf(_("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"),
236 locale.c_str(), sysname.c_str(), canname.c_str() );
237
238 wxMessageDialog
239 (
240 this,
241 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
242 + wxT("\n\n")
243 + localeInfo,
244 _("About Internat"),
245 wxOK | wxICON_INFORMATION
246 ).ShowModal();
247 }
248
249 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
250 {
251 wxString str = wxGetTextFromUser
252 (
253 _("Enter your number:"),
254 _("Try to guess my number!"),
255 wxEmptyString,
256 this
257 );
258
259 if ( str.empty() )
260 {
261 // cancelled
262 return;
263 }
264
265 long num;
266 if ( !str.ToLong(&num) || num < 0 )
267 {
268 str = _("You've probably entered an invalid number.");
269 }
270 else if ( num == 9 )
271 {
272 // this message is not translated (not in catalog) because we used _T()
273 // and not _() around it
274 str = _T("You've found a bug in this program!");
275 }
276 else if ( num == 17 )
277 {
278 str.clear();
279
280 // string must be split in two -- otherwise the translation would't be
281 // found
282 str << _("Congratulations! you've won. Here is the magic phrase:")
283 << _("cannot create fifo `%s'");
284 }
285 else
286 {
287 // this is a more implicit way to write _() but note that if you use it
288 // you must ensure that the strings get extracted in the message
289 // catalog as by default xgettext won't do it (it only knows of _(),
290 // not wxGetTranslation())
291 str = wxGetTranslation(_T("Bad luck! try again..."));
292 }
293
294 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
295 }
296
297 void MyFrame::OnOpen(wxCommandEvent&)
298 {
299 // open a bogus file -- the error message should be also translated if you've
300 // got wxstd.mo somewhere in the search path
301 wxFile file(wxT("NOTEXIST.ING"));
302 }
303