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,18 +266,26 @@ bool MyApp::OnInit()
if ( !wxApp::OnInit() ) if ( !wxApp::OnInit() )
return false; return false;
if ( m_setLocale )
{
if ( m_lang == wxLANGUAGE_UNKNOWN ) if ( m_lang == wxLANGUAGE_UNKNOWN )
{ {
int lng = wxGetSingleChoiceIndex int lng = wxGetSingleChoiceIndex
( (
_("Please choose language:"), "Please choose a language or cancel to skip changing it:",
_("Language"), "Language",
WXSIZEOF(langNames), WXSIZEOF(langNames),
langNames langNames
); );
m_lang = lng == -1 ? wxLANGUAGE_DEFAULT : langIds[lng]; if ( lng == -1 )
m_setLocale = false;
else
m_lang = langIds[lng];
}
} }
if ( m_setLocale )
{
// 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(m_lang, wxLOCALE_DONT_LOAD_DEFAULT) )
@@ -267,6 +294,7 @@ bool MyApp::OnInit()
// 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
// in the default locations, but when the program is not installed the // in the default locations, but when the program is not installed the