]>
git.saurik.com Git - wxWidgets.git/blob - samples/internat/internat.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Demonstrates internationalisation (i18n) support
4 // Author: Vadim Zeitlin/Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
35 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
36 #include "mondrian.xpm"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // Define a new application type
44 class MyApp
: public wxApp
47 virtual bool OnInit();
50 wxLocale m_locale
; // locale we'll be using
53 // Define a new frame type
54 class MyFrame
: public wxFrame
57 MyFrame(wxLocale
& m_locale
);
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
);
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 // ID for the menu commands
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
94 EVT_MENU(INTERNAT_QUIT
, MyFrame::OnQuit
)
95 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
96 EVT_MENU(INTERNAT_TEST
, MyFrame::OnPlay
)
97 EVT_MENU(INTERNAT_OPEN
, MyFrame::OnOpen
)
98 EVT_MENU(INTERNAT_TEST_1
, MyFrame::OnTest1
)
99 EVT_MENU(INTERNAT_TEST_2
, MyFrame::OnTest2
)
100 EVT_MENU(INTERNAT_TEST_3
, MyFrame::OnTest3
)
105 // ============================================================================
107 // ============================================================================
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 // `Main program' equivalent, creating windows and returning main app frame
120 // the parameter must be the lang index
121 wxString
tmp(argv
[1]);
125 static const wxLanguage langIds
[] =
131 wxLANGUAGE_BULGARIAN
,
138 wxLANGUAGE_ENGLISH_US
143 // note that it makes no sense to translate these strings, they are
144 // shown before we set the locale anyhow
145 const wxString langNames
[] =
147 _T("System default"),
161 // the arrays should be in sync
162 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames
) == WXSIZEOF(langIds
),
163 LangArraysMismatch
);
165 lng
= wxGetSingleChoiceIndex
167 _T("Please choose language:"),
175 m_locale
.Init(langIds
[lng
]);
178 // Initialize the catalogs we'll be using
179 m_locale
.AddCatalog(wxT("internat"));
181 // this catalog is installed in standard location on Linux systems and
182 // shows that you may make use of the standard message catalogs as well
184 // if it's not installed on your system, it is just silently ignored
188 m_locale
.AddCatalog(_T("fileutils"));
192 // Create the main frame window
193 MyFrame
*frame
= new MyFrame(m_locale
);
196 frame
->SetIcon(wxICON(mondrian
));
199 wxMenu
*file_menu
= new wxMenu
;
200 file_menu
->Append(wxID_ABOUT
, _("&About..."));
201 file_menu
->AppendSeparator();
202 file_menu
->Append(INTERNAT_QUIT
, _("E&xit"));
204 wxMenu
*test_menu
= new wxMenu
;
205 test_menu
->Append(INTERNAT_OPEN
, _("&Open bogus file"));
206 test_menu
->Append(INTERNAT_TEST
, _("&Play a game"));
207 test_menu
->AppendSeparator();
208 test_menu
->Append(INTERNAT_TEST_1
, _("&1 _() (gettext)"));
209 test_menu
->Append(INTERNAT_TEST_2
, _("&2 _N() (ngettext)"));
210 test_menu
->Append(INTERNAT_TEST_3
, _("&3 wxTRANSLATE() (gettext_noop)"));
212 wxMenuBar
*menu_bar
= new wxMenuBar
;
213 menu_bar
->Append(file_menu
, _("&File"));
214 menu_bar
->Append(test_menu
, _("&Test"));
215 frame
->SetMenuBar(menu_bar
);
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 // main frame constructor
229 MyFrame::MyFrame(wxLocale
& locale
)
232 _("International wxWindows App"),
240 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
245 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
248 wxString locale
= m_locale
.GetLocale();
249 wxString sysname
= m_locale
.GetSysName();
250 wxString canname
= m_locale
.GetCanonicalName();
252 localeInfo
.Printf(_("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"),
253 locale
.c_str(), sysname
.c_str(), canname
.c_str() );
258 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
262 wxOK
| wxICON_INFORMATION
266 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
268 wxString str
= wxGetTextFromUser
270 _("Enter your number:"),
271 _("Try to guess my number!"),
283 if ( !str
.ToLong(&num
) || num
< 0 )
285 str
= _("You've probably entered an invalid number.");
289 // this message is not translated (not in catalog) because we used _T()
290 // and not _() around it
291 str
= _T("You've found a bug in this program!");
293 else if ( num
== 17 )
297 // string must be split in two -- otherwise the translation would't be
299 str
<< _("Congratulations! you've won. Here is the magic phrase:")
300 << _("cannot create fifo `%s'");
304 // this is a more implicit way to write _() but note that if you use it
305 // you must ensure that the strings get extracted in the message
306 // catalog as by default xgettext won't do it (it only knows of _(),
307 // not wxGetTranslation())
308 str
= wxGetTranslation(_T("Bad luck! try again..."));
311 wxMessageBox(str
, _("Result"), wxOK
| wxICON_INFORMATION
);
314 void MyFrame::OnOpen(wxCommandEvent
&)
316 // open a bogus file -- the error message should be also translated if
317 // you've got wxstd.mo somewhere in the search path
318 wxFile
file(wxT("NOTEXIST.ING"));
321 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
323 const wxChar
* title
= _("Testing _() (gettext)");
324 wxTextEntryDialog
d(this, _("Please enter text to translate"),
325 title
, wxTRANSLATE("default value"));
326 if (d
.ShowModal() == wxID_OK
)
328 wxString v
= d
.GetValue();
330 s
<< _T("\n") << v
<< _T(" -> ")
331 << wxGetTranslation(v
.c_str()) << _T("\n");
336 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
338 const wxChar
* title
= _("Testing _N() (ngettext)");
339 wxTextEntryDialog
d(this,
340 _("Please enter range for plural forms of \"n files deleted\" phrase"),
342 if (d
.ShowModal() == wxID_OK
)
345 wxSscanf(d
.GetValue(), _T("%d-%d"), &first
, &last
);
348 for (int n
= first
; n
<= last
; ++n
)
351 wxGetTranslation(_T("file deleted"), _T("files deleted"), n
) <<
358 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
360 const wxChar
* lines
[] =
362 wxTRANSLATE("line 1"),
363 wxTRANSLATE("line 2"),
364 wxTRANSLATE("line 3"),
366 wxString
s(_("Testing wxTRANSLATE() (gettext_noop)"));
368 for (size_t i
= 0; i
< WXSIZEOF(lines
); ++i
)
370 s
<< lines
[i
] << _T(" -> ") << wxGetTranslation(lines
[i
]) << _T("\n");