1 // Example for use of GNU gettext.
2 // This file is in the public domain.
3
4 // Source code of the C++ program.
5
6 #include <wx/wx.h>
7 #include <wx/intl.h>
8
9 /* Get getpid() declaration. */
10 #if HAVE_UNISTD_H
11 # include <unistd.h>
12 #endif
13
14 class MyApp: public wxApp
15 {
16 public:
17 virtual bool OnInit();
18 private:
19 // wxWidgets has the concept of a "current locale". It is the one returned
20 // by wxGetLocale() and implicitly used by wxGetTranslation.
21 // But there is no way to explicitly set this current locale! Rather, it is
22 // always set to the last constructed locale(!), and is modified when a
23 // locale is destroyed. In such a way that the current locale points to
24 // invalid memory after you do
25 // wxLocale *a = new wxLocale;
26 // wxLocale *b = new wxLocale;
27 // delete a;
28 // delete b;
29 // So, to avoid problems, we use exactly one instance of wxLocale, and keep
30 // it alive for the entire application lifetime.
31 wxLocale appLocale;
32 };
33
34 class MyFrame: public wxFrame
35 {
36 public:
37 MyFrame();
38 };
39
40 // This defines the main() function.
IMPLEMENT_APP(MyApp)41 IMPLEMENT_APP(MyApp)
42
43 bool MyApp::OnInit()
44 {
45 // First, register the base directory where to look up .mo files.
46 wxLocale::AddCatalogLookupPathPrefix(wxT(LOCALEDIR));
47 // Second, initialize the locale and set the application-wide message domain.
48 appLocale.Init();
49 appLocale.AddCatalog(wxT("hello-c++-wxwidgets"));
50 // Now wxGetLocale() is initialized appropriately.
51
52 // Then only start building the GUI elements of the application.
53
54 // Create the main frame window.
55 MyFrame *frame = new MyFrame();
56
57 // Show the frame.
58 frame->Show(true);
59 SetTopWindow(frame);
60
61 return true;
62 }
63
MyFrame()64 MyFrame::MyFrame()
65 : wxFrame(NULL, wxID_ANY, _T("Hello example"))
66 {
67 wxStaticText *label1 =
68 new wxStaticText(this, wxID_ANY, _("Hello, world!"));
69
70 wxString label2text =
71 wxString::Format(_("This program is running as process number %d."),
72 getpid());
73 wxStaticText *label2 =
74 new wxStaticText(this, wxID_ANY, label2text);
75
76 wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
77 topSizer->Add(label1);
78 topSizer->Add(label2);
79 SetSizer(topSizer);
80 }
81