- wxString str = wxGetTextFromUser(_("Enter your number:"),
- _("Try to guess my number!"),
- "", this);
- if ( str.IsEmpty() )
- return;
-
- int num;
- wxSscanf(str, wxT("%d"), &num);
- if ( num == 0 )
- str = _("You've probably entered an invalid number.");
- else if ( num == 9 ) // this message is not translated (not in catalog)
- str = "You've found a bug in this program!";
- else if ( num != 17 ) // a more implicit way to write _()
- str = wxGetTranslation(wxT("Bad luck! try again..."));
- else {
- str.Empty();
- // string must be split in two -- otherwise the translation won't be found
- str << _("Congratulations! you've won. Here is the magic phrase:")
- << _("cannot create fifo `%s'");
- }
-
- wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
+ wxString str = wxGetTextFromUser
+ (
+ _("Enter your number:"),
+ _("Try to guess my number!"),
+ wxEmptyString,
+ this
+ );
+
+ if ( str.empty() )
+ {
+ // cancelled
+ return;
+ }
+
+ long num;
+ if ( !str.ToLong(&num) || num < 0 )
+ {
+ str = _("You've probably entered an invalid number.");
+ }
+ else if ( num == 9 )
+ {
+ // this message is not translated (not in catalog) because we used wxT()
+ // and not _() around it
+ str = wxT("You've found a bug in this program!");
+ }
+ else if ( num == 17 )
+ {
+ str.clear();
+
+ // string must be split in two -- otherwise the translation would't be
+ // found
+ str << _("Congratulations! you've won. Here is the magic phrase:")
+ << _("cannot create fifo `%s'");
+ }
+ else
+ {
+ // this is a more implicit way to write _() but note that if you use it
+ // you must ensure that the strings get extracted in the message
+ // catalog as by default xgettext won't do it; it only knows of _(),
+ // not of wxTRANSLATE(). As internat's readme.txt says you should thus
+ // call xgettext with -kwxTRANSLATE.
+ str = wxGetTranslation(wxTRANSLATE("Bad luck! try again..."));
+
+ // note also that if we want 'str' to contain a localized string
+ // we need to use wxGetTranslation explicitly as wxTRANSLATE just
+ // tells xgettext to extract the string but has no effect on the
+ // runtime of the program!
+ }
+
+ wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
+}
+
+void MyFrame::OnTestLocaleAvail(wxCommandEvent& WXUNUSED(event))
+{
+ static wxString s_locale;
+ wxString locale = wxGetTextFromUser
+ (
+ _("Enter the locale to test"),
+ wxGetTextFromUserPromptStr,
+ s_locale,
+ this
+ );
+ if ( locale.empty() )
+ return;
+
+ s_locale = locale;
+ const wxLanguageInfo * const info = wxLocale::FindLanguageInfo(s_locale);
+ if ( !info )
+ {
+ wxLogError(_("Locale \"%s\" is unknown."), s_locale.c_str());
+ return;
+ }
+
+ if ( wxLocale::IsAvailable(info->Language) )
+ {
+ wxLogMessage(_("Locale \"%s\" is available."), s_locale.c_str());
+ }
+ else
+ {
+ wxLogWarning(_("Locale \"%s\" is not available."), s_locale.c_str());
+ }
+}
+
+void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
+{
+ // open a bogus file -- the error message should be also translated if
+ // you've got wxstd.mo somewhere in the search path (see MyApp::OnInit)
+ wxFile file("NOTEXIST.ING");
+}
+
+void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
+{
+ const wxString& title = _("Testing _() (gettext)");
+
+ // NOTE: using the wxTRANSLATE() macro here we won't show a localized
+ // string in the text entry dialog; we'll simply show the un-translated
+ // string; however if the user press "ok" without altering the text,
+ // since the "default value" string has been extracted by xgettext
+ // the wxGetTranslation call later will manage to return a localized
+ // string
+ wxTextEntryDialog d(this, _("Please enter text to translate"),
+ title, wxTRANSLATE("default value"));
+
+ if (d.ShowModal() == wxID_OK)
+ {
+ wxString v = d.GetValue();
+ wxString s(title);
+ s << "\n" << v << " -> "
+ << wxGetTranslation(v.c_str()) << "\n";
+ wxMessageBox(s);
+ }
+}
+
+void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
+{
+ const wxString& title = _("Testing _N() (ngettext)");
+ wxTextEntryDialog d(this,
+ _("Please enter range for plural forms of \"n files deleted\" phrase"),
+ title, "0-10");
+
+ if (d.ShowModal() == wxID_OK)
+ {
+ int first, last;
+ wxSscanf(d.GetValue(), "%d-%d", &first, &last);
+ wxString s(title);
+ s << "\n";
+ for (int n = first; n <= last; ++n)
+ {
+ s << n << " " <<
+ wxPLURAL("file deleted", "files deleted", n) <<
+ "\n";
+ }
+ wxMessageBox(s);
+ }
+}
+
+void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
+{
+ const char* lines[] =
+ {
+ wxTRANSLATE("line 1"),
+ wxTRANSLATE("line 2"),
+ wxTRANSLATE("line 3"),
+ };
+
+ wxString s(_("Testing wxTRANSLATE() (gettext_noop)"));
+ s << "\n";
+ for (size_t i = 0; i < WXSIZEOF(lines); ++i)
+ {
+ s << lines[i] << " -> " << wxGetTranslation(lines[i]) << "\n";
+ }
+ wxMessageBox(s);