From 4a4f8ebe4ed83fbd7c6e9c96fd2c699c487a4b1b Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 25 Apr 2019 16:34:34 -0400 Subject: [PATCH] Move language setting and event from AudacityApp.h to GUIPrefs.h --- src/AudacityApp.cpp | 104 ++------------------------------ src/AudacityApp.h | 14 ----- src/Prefs.cpp | 4 +- src/effects/nyquist/Nyquist.cpp | 4 +- src/prefs/GUIPrefs.cpp | 102 ++++++++++++++++++++++++++++++- src/prefs/GUIPrefs.h | 12 ++++ 6 files changed, 120 insertions(+), 120 deletions(-) diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index f950edaa0..a720aa12f 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -26,8 +26,6 @@ It handles initialization and termination by subclassing wxApp. #include #endif -#include "TranslatableStringArray.h" - #include // for wxUSE_* macros #include #include @@ -99,6 +97,7 @@ It handles initialization and termination by subclassing wxApp. #include "ondemand/ODManager.h" #include "widgets/ErrorDialog.h" #include "prefs/DirectoriesPrefs.h" +#include "prefs/GUIPrefs.h" #include "tracks/ui/Scrubbing.h" #include "widgets/FileHistory.h" @@ -245,8 +244,6 @@ It handles initialization and termination by subclassing wxApp. /// Custom events //////////////////////////////////////////////////////////// -wxDEFINE_EVENT(EVT_LANGUAGE_CHANGE, wxCommandEvent); - #if 0 #ifdef __WXGTK__ static void wxOnAssert(const wxChar *fileName, int lineNumber, const wxChar *msg) @@ -941,97 +938,6 @@ wxLanguageInfo userLangs[] = }; #endif -wxString AudacityApp::InitLang( wxString langCode ) -{ - if ( langCode.empty() ) - langCode = gPrefs->Read(wxT("/Locale/Language"), wxEmptyString); - - // Use the system default language if one wasn't specified or if the user selected System. - if (langCode.empty()) - { - langCode = GetSystemLanguageCode(); - } - - // Initialize the language - return SetLang(langCode); -} - -wxString AudacityApp::SetLang( const wxString & lang ) -{ - wxString result = lang; - - mLocale.reset(); - -#if defined(__WXMAC__) - // This should be reviewed again during the wx3 conversion. - - // On OSX, if the LANG environment variable isn't set when - // using a language like Japanese, an assertion will trigger - // because conversion to Japanese from "?" doesn't return a - // valid length, so make OSX happy by defining/overriding - // the LANG environment variable with U.S. English for now. - wxSetEnv(wxT("LANG"), wxT("en_US.UTF-8")); -#endif - - const wxLanguageInfo *info = NULL; - if (!lang.empty()) { - info = wxLocale::FindLanguageInfo(lang); - if (!info) - ::AudacityMessageBox(wxString::Format(_("Language \"%s\" is unknown"), lang)); - } - if (!info) - { - result = GetSystemLanguageCode(); - info = wxLocale::FindLanguageInfo(result); - if (!info) - return result; - } - mLocale = std::make_unique(info->Language); - - for( const auto &path : FileNames::AudacityPathList() ) - mLocale->AddCatalogLookupPathPrefix( path ); - - // LL: Must add the wxWidgets catalog manually since the search - // paths were not set up when mLocale was created. The - // catalogs are search in LIFO order, so add wxstd first. - mLocale->AddCatalog(wxT("wxstd")); - -// AUDACITY_NAME is legitimately used on some *nix configurations. -#ifdef AUDACITY_NAME - mLocale->AddCatalog(wxT(AUDACITY_NAME)); -#else - mLocale->AddCatalog(IPC_APPL); -#endif - - // Initialize internationalisation (number formats etc.) - // - // This must go _after_ creating the wxLocale instance because - // creating the wxLocale instance sets the application-wide locale. - - Internat::Init(); - - // Notify listeners of language changes - { - wxCommandEvent evt(EVT_LANGUAGE_CHANGE); - ProcessEvent(evt); - } - - // PRL: Moved this, do it only after language intialized - // Unused strings that we want to be translated, even though - // we're not using them yet... - wxString future1 = _("Master Gain Control"); - - return result; -} - -wxString AudacityApp::GetLang() const -{ - if (mLocale) - return mLocale->GetSysName(); - else - return {}; -} - void AudacityApp::OnFatalException() { #if defined(EXPERIMENTAL_CRASH_REPORT) @@ -1112,9 +1018,9 @@ void AudacityApp::GenerateCrashReport(wxDebugReport::Context ctx) if (ctx == wxDebugReport::Context_Current) { - auto saveLang = GetLang(); - InitLang( wxT("en") ); - auto cleanup = finally( [&]{ InitLang( saveLang ); } ); + auto saveLang = GUIPrefs::GetLang(); + GUIPrefs::InitLang( wxT("en") ); + auto cleanup = finally( [&]{ GUIPrefs::InitLang( saveLang ); } ); rpt.AddText(wxT("audiodev.txt"), gAudioIO->GetDeviceInfo(), wxT("Audio Device Info")); #ifdef EXPERIMENTAL_MIDI_OUT @@ -1235,8 +1141,6 @@ bool AudacityApp::OnInit() std::unique_ptr < wxLog > { wxLog::SetActiveTarget(safenew AudacityLogger) }; // DELETE old - mLocale = NULL; - #if defined(__WXMAC__) // Disable window animation wxSystemOptions::SetOption(wxMAC_WINDOW_PLAIN_TRANSITION, 1); diff --git a/src/AudacityApp.h b/src/AudacityApp.h index 47c4f2a84..148954602 100644 --- a/src/AudacityApp.h +++ b/src/AudacityApp.h @@ -59,18 +59,6 @@ class AudacityApp final : public wxApp { int FilterEvent(wxEvent & event) override; - // If no input language given, defaults first to choice in preferences, then - // to system language. - // Returns the language actually used which is not lang if lang cannot be found. - wxString InitLang( wxString lang = {} ); - - // If no input language given, defaults to system language. - // Returns the language actually used which is not lang if lang cannot be found. - wxString SetLang( const wxString & lang ); - - // Returns the last language code that was set - wxString GetLang() const; - // These are currently only used on Mac OS, where it's // possible to have a menu bar but no windows open. It doesn't // hurt any other platforms, though. @@ -131,8 +119,6 @@ class AudacityApp final : public wxApp { private: std::unique_ptr mCmdHandler; - std::unique_ptr mLocale; - std::unique_ptr mChecker; wxTimer mTimer; diff --git a/src/Prefs.cpp b/src/Prefs.cpp index 31bfbb0f4..29d940c09 100755 --- a/src/Prefs.cpp +++ b/src/Prefs.cpp @@ -60,10 +60,10 @@ #include #include -#include "AudacityApp.h" #include "FileNames.h" #include "Languages.h" +#include "prefs/GUIPrefs.h" #include "widgets/ErrorDialog.h" std::unique_ptr ugPrefs {}; @@ -216,7 +216,7 @@ void InitPreferences() } } - langCode = wxGetApp().InitLang( langCode ); + langCode = GUIPrefs::InitLang( langCode ); // User requested that the preferences be completely reset if (resetPrefs) diff --git a/src/effects/nyquist/Nyquist.cpp b/src/effects/nyquist/Nyquist.cpp index 2fd25c8b8..a22b74a0f 100644 --- a/src/effects/nyquist/Nyquist.cpp +++ b/src/effects/nyquist/Nyquist.cpp @@ -54,7 +54,6 @@ effects from this one class. #include #include "../EffectManager.h" -#include "../../AudacityApp.h" #include "../../DirManager.h" #include "../../FileNames.h" #include "../../LabelTrack.h" @@ -70,6 +69,7 @@ effects from this one class. #include "../../widgets/ErrorDialog.h" #include "../../Prefs.h" #include "../../wxFileNameWrapper.h" +#include "../../prefs/GUIPrefs.h" #include "../../prefs/WaveformSettings.h" #include "../../widgets/NumericTextCtrl.h" #include "../../widgets/ProgressDialog.h" @@ -655,7 +655,7 @@ bool NyquistEffect::Process() mProps += wxString::Format(wxT("(putprop '*AUDACITY* (list %d %d %d) 'VERSION)\n"), AUDACITY_VERSION, AUDACITY_RELEASE, AUDACITY_REVISION); wxString lang = gPrefs->Read(wxT("/Locale/Language"), wxT("")); - lang = (lang.empty())? wxGetApp().SetLang(lang) : lang; + lang = (lang.empty())? GUIPrefs::SetLang(lang) : lang; mProps += wxString::Format(wxT("(putprop '*AUDACITY* \"%s\" 'LANGUAGE)\n"), lang); mProps += wxString::Format(wxT("(setf *DECIMAL-SEPARATOR* #\\%c)\n"), wxNumberFormatter::GetDecimalSeparator()); diff --git a/src/prefs/GUIPrefs.cpp b/src/prefs/GUIPrefs.cpp index 6408cca04..756d51dfb 100644 --- a/src/prefs/GUIPrefs.cpp +++ b/src/prefs/GUIPrefs.cpp @@ -22,9 +22,10 @@ #include "../Experimental.h" +#include #include -#include "../AudacityApp.h" +#include "../FileNames.h" #include "../Languages.h" #include "../Theme.h" #include "../Prefs.h" @@ -34,6 +35,10 @@ #include "ThemePrefs.h" #include "../AColor.h" +#include "../TranslatableStringArray.h" +#include "../widgets/ErrorDialog.h" + +wxDEFINE_EVENT(EVT_LANGUAGE_CHANGE, wxCommandEvent); GUIPrefs::GUIPrefs(wxWindow * parent, wxWindowID winid) /* i18n-hint: refers to Audacity's user interface settings */ @@ -256,7 +261,7 @@ bool GUIPrefs::Commit() // If language has changed, we want to change it now, not on the next reboot. wxString lang = gPrefs->Read(wxT("/Locale/Language"), wxT("")); - wxString usedLang = wxGetApp().SetLang(lang); + wxString usedLang = SetLang(lang); // Bug 1523: Previously didn't check no-language (=System Language) if (!(lang.empty()) && (lang != usedLang)) { // lang was not usable and is not system language. We got overridden. @@ -267,6 +272,99 @@ bool GUIPrefs::Commit() return true; } +wxString GUIPrefs::InitLang( wxString langCode ) +{ + if ( langCode.empty() ) + langCode = gPrefs->Read(wxT("/Locale/Language"), wxEmptyString); + + // Use the system default language if one wasn't specified or if the user selected System. + if (langCode.empty()) + { + langCode = GetSystemLanguageCode(); + } + + // Initialize the language + return SetLang(langCode); +} + +static std::unique_ptr sLocale; + +wxString GUIPrefs::SetLang( const wxString & lang ) +{ + wxString result = lang; + + sLocale.reset(); + +#if defined(__WXMAC__) + // This should be reviewed again during the wx3 conversion. + + // On OSX, if the LANG environment variable isn't set when + // using a language like Japanese, an assertion will trigger + // because conversion to Japanese from "?" doesn't return a + // valid length, so make OSX happy by defining/overriding + // the LANG environment variable with U.S. English for now. + wxSetEnv(wxT("LANG"), wxT("en_US.UTF-8")); +#endif + + const wxLanguageInfo *info = NULL; + if (!lang.empty()) { + info = wxLocale::FindLanguageInfo(lang); + if (!info) + ::AudacityMessageBox(wxString::Format(_("Language \"%s\" is unknown"), lang)); + } + if (!info) + { + result = GetSystemLanguageCode(); + info = wxLocale::FindLanguageInfo(result); + if (!info) + return result; + } + sLocale = std::make_unique(info->Language); + + for( const auto &path : FileNames::AudacityPathList() ) + sLocale->AddCatalogLookupPathPrefix( path ); + + // LL: Must add the wxWidgets catalog manually since the search + // paths were not set up when mLocale was created. The + // catalogs are search in LIFO order, so add wxstd first. + sLocale->AddCatalog(wxT("wxstd")); + +// AUDACITY_NAME is legitimately used on some *nix configurations. +#ifdef AUDACITY_NAME + sLocale->AddCatalog(wxT(AUDACITY_NAME)); +#else + sLocale->AddCatalog(IPC_APPL); +#endif + + // Initialize internationalisation (number formats etc.) + // + // This must go _after_ creating the wxLocale instance because + // creating the wxLocale instance sets the application-wide locale. + + Internat::Init(); + + // Notify listeners of language changes + { + wxCommandEvent evt(EVT_LANGUAGE_CHANGE); + wxTheApp->ProcessEvent(evt); + } + + // PRL: Moved this, do it only after language intialized + // Unused strings that we want to be translated, even though + // we're not using them yet... + wxString future1 = _("Master Gain Control"); + + return result; +} + +wxString GUIPrefs::GetLang() +{ + if (sLocale) + return sLocale->GetSysName(); + else + return {}; +} + PrefsPanel::Factory GUIPrefsFactory = [](wxWindow *parent, wxWindowID winid) { diff --git a/src/prefs/GUIPrefs.h b/src/prefs/GUIPrefs.h index b738289d5..d6f913cb4 100644 --- a/src/prefs/GUIPrefs.h +++ b/src/prefs/GUIPrefs.h @@ -37,6 +37,18 @@ class GUIPrefs final : public PrefsPanel static void GetRangeChoices( wxArrayStringEx *pChoices, wxArrayStringEx *pCodes); + // If no input language given, defaults first to choice in preferences, then + // to system language. + // Returns the language actually used which is not lang if lang cannot be found. + static wxString InitLang( wxString lang = {} ); + + // If no input language given, defaults to system language. + // Returns the language actually used which is not lang if lang cannot be found. + static wxString SetLang( const wxString & lang ); + + // Returns the last language code that was set + static wxString GetLang(); + private: void Populate();