Add an option to skip changing locale in the internat sample

Show that we can use the translations to the user language even without
changing the locale.
This commit is contained in:
Vadim Zeitlin
2021-08-07 18:27:13 +02:00
parent e0cd2c637d
commit 4adb58b7ea

View File

@@ -57,7 +57,7 @@
class MyApp: public wxApp class MyApp: public wxApp
{ {
public: public:
MyApp() { m_lang = wxLANGUAGE_UNKNOWN; } MyApp() { m_lang = wxLANGUAGE_UNKNOWN; 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;
@@ -65,6 +65,7 @@ public:
protected: protected:
wxLanguage m_lang; // language specified by user wxLanguage m_lang; // language specified by user
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
}; };
@@ -204,8 +205,14 @@ wxIMPLEMENT_APP(MyApp);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// command line arguments handling // command line arguments handling
static const char* OPTION_NO_LOCALE = "no-locale";
void MyApp::OnInitCmdLine(wxCmdLineParser& parser) void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
{ {
parser.AddSwitch("n", OPTION_NO_LOCALE,
_("skip setting locale on startup"));
parser.AddParam(_("locale"), parser.AddParam(_("locale"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL); wxCMD_LINE_PARAM_OPTIONAL);
@@ -218,8 +225,20 @@ bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
if ( !wxApp::OnCmdLineParsed(parser) ) if ( !wxApp::OnCmdLineParsed(parser) )
return false; return false;
if ( parser.Found(OPTION_NO_LOCALE) )
{
m_setLocale = false;
}
if ( parser.GetParamCount() ) 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(); const wxString loc = parser.GetParam();
if ( loc.empty() ) if ( loc.empty() )
{ {
@@ -247,25 +266,34 @@ bool MyApp::OnInit()
if ( !wxApp::OnInit() ) if ( !wxApp::OnInit() )
return false; return false;
if ( m_lang == wxLANGUAGE_UNKNOWN ) if ( m_setLocale )
{ {
int lng = wxGetSingleChoiceIndex if ( m_lang == wxLANGUAGE_UNKNOWN )
( {
_("Please choose language:"), int lng = wxGetSingleChoiceIndex
_("Language"), (
WXSIZEOF(langNames), "Please choose a language or cancel to skip changing it:",
langNames "Language",
); WXSIZEOF(langNames),
m_lang = lng == -1 ? wxLANGUAGE_DEFAULT : langIds[lng]; langNames
);
if ( lng == -1 )
m_setLocale = false;
else
m_lang = langIds[lng];
}
} }
// don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return if ( m_setLocale )
// false just because it failed to load wxstd catalog
if ( !m_locale.Init(m_lang, wxLOCALE_DONT_LOAD_DEFAULT) )
{ {
wxLogWarning(_("This language is not supported by the system.")); // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
// false just because it failed to load wxstd catalog
if ( !m_locale.Init(m_lang, wxLOCALE_DONT_LOAD_DEFAULT) )
{
wxLogWarning(_("This language is not supported by the system."));
// continue nevertheless // continue nevertheless
}
} }
// 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