1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-04-30 07:39:42 +02:00

Define and use TranslatableStringArray. This lets you define "listeners"...

... for language changes, without inserting extra code to send the
notifications.
This commit is contained in:
Paul Licameli 2015-08-17 11:49:13 -04:00
parent 5c77d34a35
commit b9db3bd83d
9 changed files with 138 additions and 79 deletions

View File

@ -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 <wx/defs.h>
#include <wx/app.h>
@ -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()

View File

@ -231,6 +231,7 @@ audacity_SOURCES = \
TrackPanel.h \
TrackPanelAx.cpp \
TrackPanelAx.h \
TranslatableStringArray.h \
UndoManager.cpp \
UndoManager.h \
ViewInfo.cpp \

View File

@ -0,0 +1,80 @@
/**********************************************************************
Audacity: A Digital Audio Editor
TranslatableStringArray.h
Paul Licameli
**********************************************************************/
#ifndef __AUDACITY_TRANSLATABLE_STRING_ARRAY__
#define __AUDACITY_TRANSLATABLE_STRING_ARRAY__
#include <vector>
#include <wx/app.h>
#include <wx/event.h>
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<typename ArrayType> 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<wxArrayString> TranslatableStringArray;
#endif

View File

@ -16,6 +16,7 @@ Paul Licameli
#include "../Audacity.h"
#include "SpectrogramSettings.h"
#include "../NumberScale.h"
#include "../TranslatableStringArray.h"
#include <algorithm>
#include <wx/msgdlg.h>
@ -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)

View File

@ -62,7 +62,6 @@ public:
stNumScaleTypes,
};
static void InvalidateNames(); // in case of language change
static const wxArrayString &GetScaleNames();
static const wxArrayString &GetAlgorithmNames();

View File

@ -22,6 +22,7 @@ Paul Licameli
#include <wx/intl.h>
#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()

View File

@ -57,7 +57,6 @@ public:
stNumScaleTypes,
};
static void InvalidateNames(); // in case of language change
static const wxArrayString &GetScaleNames();
ScaleType scaleType;

View File

@ -434,6 +434,7 @@
<ClInclude Include="..\..\..\src\SseMathFuncs.h" />
<ClInclude Include="..\..\..\src\toolbars\SpectralSelectionBar.h" />
<ClInclude Include="..\..\..\src\toolbars\SpectralSelectionBarListener.h" />
<ClInclude Include="..\..\..\src\TranslatableStringArray.h" />
<ClInclude Include="..\..\..\src\WaveTrackLocation.h" />
<ClInclude Include="..\..\..\src\widgets\HelpSystem.h" />
<ClInclude Include="..\..\..\src\widgets\NumericTextCtrl.h" />

View File

@ -1708,6 +1708,9 @@
<ClInclude Include="..\..\..\src\effects\VST\VSTControlMSW.h">
<Filter>src\effects\VST</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\TranslatableStringArray.h">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="..\..\audacity.ico">