Stop allowing to select the language in the internat sample

Restrict the choice to using the current system locale or not using it,
as setting any other language doesn't, and can't, work under macOS.
This commit is contained in:
Vadim Zeitlin
2021-08-10 17:12:01 +02:00
parent 4adb58b7ea
commit eb5bffd3ca

View File

@@ -57,14 +57,13 @@
class MyApp: public wxApp class MyApp: public wxApp
{ {
public: public:
MyApp() { m_lang = wxLANGUAGE_UNKNOWN; m_setLocale = true; } MyApp() { m_setLocale = true; }
virtual void OnInitCmdLine(wxCmdLineParser& parser) wxOVERRIDE; virtual void OnInitCmdLine(wxCmdLineParser& parser) wxOVERRIDE;
virtual bool OnCmdLineParsed(wxCmdLineParser& parser) wxOVERRIDE; virtual bool OnCmdLineParsed(wxCmdLineParser& parser) wxOVERRIDE;
virtual bool OnInit() wxOVERRIDE; virtual bool OnInit() wxOVERRIDE;
protected: protected:
wxLanguage m_lang; // language specified by user
bool m_setLocale; // if false, skip setting locale entirely bool m_setLocale; // if false, skip setting locale entirely
wxLocale m_locale; // locale we'll be using wxLocale m_locale; // locale we'll be using
}; };
@@ -120,59 +119,6 @@ enum
INTERNAT_MACRO_9 INTERNAT_MACRO_9
}; };
// language data
static const wxLanguage langIds[] =
{
wxLANGUAGE_DEFAULT,
wxLANGUAGE_FRENCH,
wxLANGUAGE_ITALIAN,
wxLANGUAGE_GERMAN,
wxLANGUAGE_RUSSIAN,
wxLANGUAGE_BULGARIAN,
wxLANGUAGE_CZECH,
wxLANGUAGE_POLISH,
wxLANGUAGE_SWEDISH,
#if wxUSE_UNICODE || defined(__WXMOTIF__)
wxLANGUAGE_JAPANESE,
#endif
#if wxUSE_UNICODE
wxLANGUAGE_GEORGIAN,
wxLANGUAGE_ENGLISH,
wxLANGUAGE_ENGLISH_US,
wxLANGUAGE_ARABIC,
wxLANGUAGE_ARABIC_EGYPT
#endif
};
// note that it makes no sense to translate these strings, they are
// shown before we set the locale anyhow
const wxString langNames[] =
{
"System default",
"French",
"Italian",
"German",
"Russian",
"Bulgarian",
"Czech",
"Polish",
"Swedish",
#if wxUSE_UNICODE || defined(__WXMOTIF__)
"Japanese",
#endif
#if wxUSE_UNICODE
"Georgian",
"English",
"English (U.S.)",
"Arabic",
"Arabic (Egypt)"
#endif
};
// the arrays must be in sync
wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
LangArraysMismatch );
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxWidgets macros // wxWidgets macros
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -213,10 +159,6 @@ void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
parser.AddSwitch("n", OPTION_NO_LOCALE, parser.AddSwitch("n", OPTION_NO_LOCALE,
_("skip setting locale on startup")); _("skip setting locale on startup"));
parser.AddParam(_("locale"),
wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL);
wxApp::OnInitCmdLine(parser); wxApp::OnInitCmdLine(parser);
} }
@@ -230,33 +172,6 @@ bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
m_setLocale = false; m_setLocale = false;
} }
if ( parser.GetParamCount() )
{
if ( !m_setLocale )
{
wxLogError("Locale parameter ignored when %s option is used.",
OPTION_NO_LOCALE);
return false;
}
const wxString loc = parser.GetParam();
if ( loc.empty() )
{
m_lang = wxLANGUAGE_DEFAULT;
}
else
{
const wxLanguageInfo * const lang = wxLocale::FindLanguageInfo(loc);
if ( !lang )
{
wxLogError(_("Locale \"%s\" is unknown."), loc);
return false;
}
m_lang = static_cast<wxLanguage>(lang->Language);
}
}
return true; return true;
} }
@@ -266,36 +181,39 @@ bool MyApp::OnInit()
if ( !wxApp::OnInit() ) if ( !wxApp::OnInit() )
return false; return false;
if ( m_setLocale ) // For demonstration purposes only, ask the user if they want to run the
{ // program using the current system language. In real programs, we would do
if ( m_lang == wxLANGUAGE_UNKNOWN ) // it unconditionally for localized programs -- or never do it at all for
{ // the other ones.
int lng = wxGetSingleChoiceIndex const wxLanguageInfo* const
( langInfo = wxLocale::GetLanguageInfo(wxLANGUAGE_DEFAULT);
"Please choose a language or cancel to skip changing it:", const wxString
"Language", langDesc = langInfo ? langInfo->Description
WXSIZEOF(langNames), : "the default system locale";
langNames if ( m_setLocale &&
); wxMessageBox
if ( lng == -1 ) (
m_setLocale = false; wxString::Format
else (
m_lang = langIds[lng]; "Would you like to use the program in %s?",
} langDesc
} ),
"wxWidgets i18n (internat) sample",
if ( m_setLocale ) wxYES_NO
) == wxYES )
{ {
// don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
// false just because it failed to load wxstd catalog // false just because it failed to load wxstd catalog
if ( !m_locale.Init(m_lang, wxLOCALE_DONT_LOAD_DEFAULT) ) if ( !m_locale.Init(wxLANGUAGE_DEFAULT, wxLOCALE_DONT_LOAD_DEFAULT) )
{ {
wxLogWarning(_("This language is not supported by the system.")); wxLogWarning("Failed to initialize the default system locale.");
// continue nevertheless
} }
} }
// Independently of whether we set the locale or not, we always load the
// translations (for the default system language) here.
// normally this wouldn't be necessary as the catalog files would be found // normally this wouldn't be necessary as the catalog files would be found
// in the default locations, but when the program is not installed the // in the default locations, but when the program is not installed the
// catalogs are in the build directory where we wouldn't find them by // catalogs are in the build directory where we wouldn't find them by
@@ -306,12 +224,11 @@ bool MyApp::OnInit()
wxTranslations* const trans = new wxTranslations(); wxTranslations* const trans = new wxTranslations();
wxTranslations::Set(trans); wxTranslations::Set(trans);
// Initialize the catalogs we'll be using // Initialize the catalogs we'll be using.
const wxLanguageInfo* pInfo = wxLocale::GetLanguageInfo(m_lang);
if ( !trans->AddCatalog("internat") ) if ( !trans->AddCatalog("internat") )
{ {
wxLogError(_("Couldn't find/load the 'internat' catalog for locale '%s'."), wxLogError(_("Couldn't find/load 'internat' catalog for %s."),
pInfo ? pInfo->GetLocaleName() : _("unknown")); langDesc);
} }
// Now try to add wxstd.mo so that loading "NOTEXIST.ING" file will produce // Now try to add wxstd.mo so that loading "NOTEXIST.ING" file will produce