From b9db3bd83d9ebb57bff48612983766879470243a Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Mon, 17 Aug 2015 11:49:13 -0400 Subject: [PATCH] Define and use TranslatableStringArray. This lets you define "listeners"... ... for language changes, without inserting extra code to send the notifications. --- src/AudacityApp.cpp | 12 +-- src/Makefile.am | 1 + src/TranslatableStringArray.h | 80 ++++++++++++++++++ src/prefs/SpectrogramSettings.cpp | 83 ++++++++----------- src/prefs/SpectrogramSettings.h | 1 - src/prefs/WaveformSettings.cpp | 35 +++----- src/prefs/WaveformSettings.h | 1 - win/Projects/Audacity/Audacity.vcxproj | 1 + .../Audacity/Audacity.vcxproj.filters | 3 + 9 files changed, 138 insertions(+), 79 deletions(-) create mode 100644 src/TranslatableStringArray.h diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index bc847067e..afebf4e5d 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -22,6 +22,7 @@ It handles initialization and termination by subclassing wxApp. #endif #include "Audacity.h" // This should always be included first +#include "TranslatableStringArray.h" #include #include @@ -93,8 +94,6 @@ It handles initialization and termination by subclassing wxApp. #include "commands/Keyboard.h" #include "widgets/ErrorDialog.h" #include "prefs/DirectoriesPrefs.h" -#include "prefs/SpectrogramSettings.h" -#include "prefs/WaveformSettings.h" //temporarilly commented out till it is added to all projects //#include "Profiler.h" @@ -234,6 +233,7 @@ It handles initialization and termination by subclassing wxApp. //////////////////////////////////////////////////////////// DEFINE_EVENT_TYPE(EVT_OPEN_AUDIO_FILE); +DEFINE_EVENT_TYPE(EVT_LANGUAGE_CHANGE); #ifdef __WXGTK__ static void wxOnAssert(const wxChar *fileName, int lineNumber, const wxChar *msg) @@ -1039,9 +1039,11 @@ void AudacityApp::InitLang( const wxString & lang ) Internat::Init(); - // Some static arrays unconnected with any project want to be informed of language changes. - SpectrogramSettings::InvalidateNames(); - WaveformSettings::InvalidateNames(); + // Notify listeners of language changes + { + wxCommandEvent evt(EVT_LANGUAGE_CHANGE); + ProcessEvent(evt); + } } void AudacityApp::OnFatalException() diff --git a/src/Makefile.am b/src/Makefile.am index c779b0979..f1470e7d3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -231,6 +231,7 @@ audacity_SOURCES = \ TrackPanel.h \ TrackPanelAx.cpp \ TrackPanelAx.h \ + TranslatableStringArray.h \ UndoManager.cpp \ UndoManager.h \ ViewInfo.cpp \ diff --git a/src/TranslatableStringArray.h b/src/TranslatableStringArray.h new file mode 100644 index 000000000..89dc457f7 --- /dev/null +++ b/src/TranslatableStringArray.h @@ -0,0 +1,80 @@ +/********************************************************************** + +Audacity: A Digital Audio Editor + +TranslatableStringArray.h + +Paul Licameli + +**********************************************************************/ + +#ifndef __AUDACITY_TRANSLATABLE_STRING_ARRAY__ +#define __AUDACITY_TRANSLATABLE_STRING_ARRAY__ + +#include +#include +#include + +class wxArrayString; + +DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_LANGUAGE_CHANGE, -1); + +/* +This class can maintain a static table containing user visible strings that updates +itself properly when the language is changed in Preferences. + +Typical usage is to define a derived class, override Populate(), and then +make a singleton instance of the class. + +Populate() is called only as needed to fill the table on demand the first +time it is used after application startup or language change. +*/ + +template class TranslatableArray + : public wxEvtHandler +{ +public: + + TranslatableArray() + { + if (wxTheApp) + wxTheApp->Connect(EVT_LANGUAGE_CHANGE, + wxCommandEventHandler(TranslatableArray::Invalidate), + NULL, + this); + } + + ~TranslatableArray() + { + if (wxTheApp) + wxTheApp->Disconnect(EVT_LANGUAGE_CHANGE, + wxCommandEventHandler(TranslatableArray::Invalidate), + NULL, + this); + } + + const ArrayType& Get() + { + if (mContents.empty()) + Populate(); + return mContents; + } + +protected: + // Override this function to fill in mContents, + // typically by lines like + // mContents.push_back(_("Translate me")); + virtual void Populate() = 0; + + void Invalidate(wxCommandEvent &evt) + { + mContents.clear(); + evt.Skip(); + } + + ArrayType mContents; +}; + +typedef TranslatableArray TranslatableStringArray; + +#endif diff --git a/src/prefs/SpectrogramSettings.cpp b/src/prefs/SpectrogramSettings.cpp index ef0b6227d..0f7d0e703 100644 --- a/src/prefs/SpectrogramSettings.cpp +++ b/src/prefs/SpectrogramSettings.cpp @@ -16,6 +16,7 @@ Paul Licameli #include "../Audacity.h" #include "SpectrogramSettings.h" #include "../NumberScale.h" +#include "../TranslatableStringArray.h" #include #include @@ -142,65 +143,49 @@ SpectrogramSettings& SpectrogramSettings::defaults() return instance; } -namespace -{ - wxArrayString &scaleNamesArray() - { - static wxArrayString theArray; - return theArray; - } - - wxArrayString &algorithmNamesArray() - { - static wxArrayString theArray; - return theArray; - } -} - -//static -void SpectrogramSettings::InvalidateNames() -{ - scaleNamesArray().Clear(); - algorithmNamesArray().Clear(); -} - //static const wxArrayString &SpectrogramSettings::GetScaleNames() { - wxArrayString &theArray = scaleNamesArray(); + class ScaleNamesArray : public TranslatableStringArray + { + virtual void Populate() + { + // Keep in correspondence with enum SpectrogramSettings::ScaleType: + mContents.Add(_("Linear")); + mContents.Add(_("Logarithmic")); + /* i18n-hint: The name of a frequency scale in psychoacoustics */ + mContents.Add(_("Mel")); + /* i18n-hint: The name of a frequency scale in psychoacoustics, named for Heinrich Barkhausen */ + mContents.Add(_("Bark")); + /* i18n-hint: The name of a frequency scale in psychoacoustics, abbreviates Equivalent Rectangular Bandwidth */ + mContents.Add(_("ERBS")); + /* i18n-hint: A mathematical formula where f stands for frequency */ + mContents.Add(_("1 / f")); + } + }; - if (theArray.IsEmpty()) { - // Keep in correspondence with enum SpectrogramSettings::ScaleType: - theArray.Add(_("Linear")); - theArray.Add(_("Logarithmic")); - /* i18n-hint: The name of a frequency scale in psychoacoustics */ - theArray.Add(_("Mel")); - /* i18n-hint: The name of a frequency scale in psychoacoustics, named for Heinrich Barkhausen */ - theArray.Add(_("Bark")); - /* i18n-hint: The name of a frequency scale in psychoacoustics, abbreviates Equivalent Rectangular Bandwidth */ - theArray.Add(_("ERBS")); - /* i18n-hint: A mathematical formula where f stands for frequency */ - theArray.Add(_("1 / f")); - } - - return theArray; + static ScaleNamesArray theArray; + return theArray.Get(); } //static const wxArrayString &SpectrogramSettings::GetAlgorithmNames() { - wxArrayString &theArray = algorithmNamesArray(); + class AlgorithmNamesArray : public TranslatableStringArray + { + virtual void Populate() + { + // Keep in correspondence with enum SpectrogramSettings::Algorithm: + mContents.Add(_("Frequencies")); + /* i18n-hint: the Reassignment algorithm for spectrograms */ + mContents.Add(_("Reassignment")); + /* i18n-hint: EAC abbreviates "Enhanced Autocorrelation" */ + mContents.Add(_("Pitch (EAC)")); + } + }; - if (theArray.IsEmpty()) { - // Keep in correspondence with enum SpectrogramSettings::Algorithm: - theArray.Add(_("Frequencies")); - /* i18n-hint: the Reassignment algorithm for spectrograms */ - theArray.Add(_("Reassignment")); - /* i18n-hint: EAC abbreviates "Enhanced Autocorrelation" */ - theArray.Add(_("Pitch (EAC)")); - } - - return theArray; + static AlgorithmNamesArray theArray; + return theArray.Get(); } bool SpectrogramSettings::Validate(bool quiet) diff --git a/src/prefs/SpectrogramSettings.h b/src/prefs/SpectrogramSettings.h index 5077a188f..cdc13329b 100644 --- a/src/prefs/SpectrogramSettings.h +++ b/src/prefs/SpectrogramSettings.h @@ -62,7 +62,6 @@ public: stNumScaleTypes, }; - static void InvalidateNames(); // in case of language change static const wxArrayString &GetScaleNames(); static const wxArrayString &GetAlgorithmNames(); diff --git a/src/prefs/WaveformSettings.cpp b/src/prefs/WaveformSettings.cpp index 9872bc909..9c33bf9a8 100644 --- a/src/prefs/WaveformSettings.cpp +++ b/src/prefs/WaveformSettings.cpp @@ -22,6 +22,7 @@ Paul Licameli #include #include "../Prefs.h" +#include "../TranslatableStringArray.h" WaveformSettings::Globals::Globals() { @@ -145,33 +146,21 @@ void WaveformSettings::NextHigherDBRange() ConvertToActualDBRange(); } -namespace -{ - wxArrayString &scaleNamesArray() - { - static wxArrayString theArray; - return theArray; - } -} - -//static -void WaveformSettings::InvalidateNames() -{ - scaleNamesArray().Clear(); -} - //static const wxArrayString &WaveformSettings::GetScaleNames() { - wxArrayString &theArray = scaleNamesArray(); + class ScaleNamesArray : public TranslatableStringArray + { + virtual void Populate() + { + // Keep in correspondence with enum WaveTrack::WaveTrackDisplay: + mContents.Add(_("Linear")); + mContents.Add(_("Logarithmic")); + } + }; - if (theArray.IsEmpty()) { - // Keep in correspondence with enum WaveTrack::WaveTrackDisplay: - theArray.Add(_("Linear")); - theArray.Add(_("Logarithmic")); - } - - return theArray; + static ScaleNamesArray theArray; + return theArray.Get(); } WaveformSettings::~WaveformSettings() diff --git a/src/prefs/WaveformSettings.h b/src/prefs/WaveformSettings.h index 6765b9e48..52e7e4d41 100644 --- a/src/prefs/WaveformSettings.h +++ b/src/prefs/WaveformSettings.h @@ -57,7 +57,6 @@ public: stNumScaleTypes, }; - static void InvalidateNames(); // in case of language change static const wxArrayString &GetScaleNames(); ScaleType scaleType; diff --git a/win/Projects/Audacity/Audacity.vcxproj b/win/Projects/Audacity/Audacity.vcxproj index ca1434b3c..0a0d65330 100755 --- a/win/Projects/Audacity/Audacity.vcxproj +++ b/win/Projects/Audacity/Audacity.vcxproj @@ -434,6 +434,7 @@ + diff --git a/win/Projects/Audacity/Audacity.vcxproj.filters b/win/Projects/Audacity/Audacity.vcxproj.filters index 68e242dd8..6dcc3b1a8 100755 --- a/win/Projects/Audacity/Audacity.vcxproj.filters +++ b/win/Projects/Audacity/Audacity.vcxproj.filters @@ -1708,6 +1708,9 @@ src\effects\VST + + src +