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