From 8cb4e70064717b23c5798a80482e33ef24aa1765 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 14 Jul 2017 19:54:45 +0200 Subject: [PATCH] Refactor wxLocale initialization code Don't call wxSetlocale() in DoInit() any more, this was redundant when it was called from Init(wxLanguage) overload. Add new DoCommonPostInit() called from both Init() overloads after setting the locale in whichever way they do it to avoid code duplication. As a side effect of this change, the error message given if the locale can't be set is the same now independently of the ctor/Init() overload used (previously it differed depending on whether a wxLanguage or the name of the language as string was passed) and it's always logged using wxLogWarning() and not it in one case and wxLogError() in the other one. --- include/wx/intl.h | 17 +++++- src/common/intl.cpp | 125 +++++++++++++++++++++----------------------- 2 files changed, 76 insertions(+), 66 deletions(-) diff --git a/include/wx/intl.h b/include/wx/intl.h index f1a9dafd9b..d66e3043d8 100644 --- a/include/wx/intl.h +++ b/include/wx/intl.h @@ -333,9 +333,11 @@ public: static void DestroyLanguagesDB(); private: - bool DoInit(const wxString& name, + // This method is trivial and just updates the corresponding member + // variables without doing anything else. + void DoInit(const wxString& name, const wxString& shortName, - const wxString& locale); + int language); // copy default table of languages from global static array to // m_langugagesInfo, called by InitLanguagesDB @@ -344,6 +346,17 @@ private: // initialize the member fields to default values void DoCommonInit(); + // After trying to set locale, call this method to give the appropriate + // error if it couldn't be set (success == false) and to load the + // translations for the given language, if necessary. + // + // The return value is the same as "success" parameter. + bool DoCommonPostInit(bool success, + const wxString& name, + const wxString& shortName, + bool bLoadDefault); + + wxString m_strLocale, // this locale name m_strShort; // short name for the locale int m_language; // this locale wxLanguage value diff --git a/src/common/intl.cpp b/src/common/intl.cpp index 9efc542bf2..4386df0bd4 100644 --- a/src/common/intl.cpp +++ b/src/common/intl.cpp @@ -288,33 +288,6 @@ bool wxLocale::Init(const wxString& name, wxS("wxLocale::Init with bConvertEncoding=false is no longer supported, add charset to your catalogs") ); #endif - bool ret = DoInit(name, shortName, locale); - - // NB: don't use 'lang' here, 'language' may be wxLANGUAGE_DEFAULT - wxTranslations *t = wxTranslations::Get(); - if ( t ) - { - t->SetLanguage(shortName); - - if ( bLoadDefault ) - t->AddStdCatalog(); - } - - return ret; -} - -bool wxLocale::DoInit(const wxString& name, - const wxString& shortName, - const wxString& locale) -{ - wxASSERT_MSG( !m_initialized, - wxS("you can't call wxLocale::Init more than once") ); - - m_initialized = true; - m_strLocale = name; - m_strShort = shortName; - m_language = wxLANGUAGE_UNKNOWN; - // change current locale (default: same as long name) wxString szLocale(locale); if ( szLocale.empty() ) @@ -326,27 +299,69 @@ bool wxLocale::DoInit(const wxString& name, wxS("no locale to set in wxLocale::Init()") ); } - if ( !wxSetlocale(LC_ALL, szLocale) ) - { - wxLogError(_("locale '%s' cannot be set."), szLocale); - } - // the short name will be used to look for catalog files as well, // so we need something here - if ( m_strShort.empty() ) { + wxString strShort(shortName); + if ( strShort.empty() ) { // FIXME I don't know how these 2 letter abbreviations are formed, // this wild guess is surely wrong if ( !szLocale.empty() ) { - m_strShort += (wxChar)wxTolower(szLocale[0]); + strShort += (wxChar)wxTolower(szLocale[0]); if ( szLocale.length() > 1 ) - m_strShort += (wxChar)wxTolower(szLocale[1]); + strShort += (wxChar)wxTolower(szLocale[1]); } } - return true; + DoInit(name, strShort, wxLANGUAGE_UNKNOWN); + + const bool ret = wxSetlocale(LC_ALL, szLocale) != NULL; + + return DoCommonPostInit(ret, szLocale, shortName, bLoadDefault); } +void wxLocale::DoInit(const wxString& name, + const wxString& shortName, + int language) +{ + wxASSERT_MSG( !m_initialized, + wxS("you can't call wxLocale::Init more than once") ); + + m_initialized = true; + m_strLocale = name; + m_strShort = shortName; + m_language = language; +} + +bool wxLocale::DoCommonPostInit(bool success, + const wxString& name, + const wxString& shortName, + bool bLoadDefault) +{ + if ( !success ) + { + wxLogWarning(_("Cannot set locale to language \"%s\"."), name); + + // As we failed to change locale, there is no need to restore the + // previous one: it's still valid. + free(const_cast(m_pszOldLocale)); + m_pszOldLocale = NULL; + + // continue nevertheless and try to load at least the translations for + // this language + } + + wxTranslations *t = wxTranslations::Get(); + if ( t ) + { + t->SetLanguage(shortName); + + if ( bLoadDefault ) + t->AddStdCatalog(); + } + + return success; +} #if defined(__UNIX__) && wxUSE_UNICODE && !defined(__WXMAC__) static const char *wxSetlocaleTryUTF8(int c, const wxString& lc) @@ -419,6 +434,7 @@ bool wxLocale::Init(int language, int flags) } const wxString& name = info->Description; + DoInit(name, info->CanonicalName, lang); // Set the locale: #if defined(__UNIX__) && !defined(__WXMAC__) @@ -551,35 +567,16 @@ bool wxLocale::Init(int language, int flags) #endif #ifndef WX_NO_LOCALE_SUPPORT - if ( !retloc ) - { - wxLogWarning(_("Cannot set locale to language \"%s\"."), name); - - // As we failed to change locale, there is no need to restore the - // previous one: it's still valid. - free(const_cast(m_pszOldLocale)); - m_pszOldLocale = NULL; - - // continue nevertheless and try to load at least the translations for - // this language - } - - const bool ret = DoInit(name, info->CanonicalName, retloc); - - if (IsOk()) // setlocale() succeeded - m_language = lang; - // NB: don't use 'lang' here, 'language' - wxTranslations *t = wxTranslations::Get(); - if ( t ) - { - t->SetLanguage(static_cast(language)); - - if ( flags & wxLOCALE_LOAD_DEFAULT ) - t->AddStdCatalog(); - } - - return ret; + return DoCommonPostInit + ( + retloc != NULL, + name, + language == wxLANGUAGE_DEFAULT + ? wxString() + : info->CanonicalName, + flags & wxLOCALE_LOAD_DEFAULT + ); #endif // !WX_NO_LOCALE_SUPPORT }