From f16709945b6107084c34c08830554144614077a6 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 15 Dec 2019 11:56:01 -0500 Subject: [PATCH] Define TranslatableString::Strip() --- include/audacity/Types.h | 12 ++++++++++++ src/Internat.cpp | 31 +++++++++++++++++++++++++++++++ src/ShuttleGui.cpp | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/include/audacity/Types.h b/include/audacity/Types.h index 964f7d4a4..825237c5d 100644 --- a/include/audacity/Types.h +++ b/include/audacity/Types.h @@ -455,6 +455,18 @@ public: return PluralTemp< N >{ *this, pluralStr }; } + // Translated strings may still contain menu hot-key codes (indicated by &) + // that wxWidgets interprets, and also trailing ellipses, that should be + // removed for other uses. + enum StripOptions : unsigned { + // Values to be combined with bitwise OR + MenuCodes = 0x1, + Ellipses = 0x2, + }; + TranslatableString &Strip( unsigned options = MenuCodes ) &; + TranslatableString &&Strip( unsigned options = MenuCodes ) && + { return std::move( Strip( options ) ); } + private: enum class Request { Context, // return a disambiguating context string diff --git a/src/Internat.cpp b/src/Internat.cpp index 1bed8eb76..924972d7f 100644 --- a/src/Internat.cpp +++ b/src/Internat.cpp @@ -312,6 +312,37 @@ bool TranslatableString::IsVerbatim() const return DoGetContext( mFormatter ) == NullContextName; } +TranslatableString &TranslatableString::Strip( unsigned codes ) & +{ + auto prevFormatter = mFormatter; + mFormatter = [prevFormatter, codes] + ( const wxString & str, TranslatableString::Request request ) -> wxString { + switch ( request ) { + case Request::Context: + return TranslatableString::DoGetContext( prevFormatter ); + case Request::Format: + case Request::DebugFormat: + default: { + bool debug = request == Request::DebugFormat; + auto result = + TranslatableString::DoSubstitute( prevFormatter, str, debug ); + if ( codes & MenuCodes ) + result = wxStripMenuCodes( result ); + if ( codes & Ellipses ) { + if (result.EndsWith(wxT("..."))) + result = result.Left( result.length() - 3 ); + // Also check for the single-character Unicode ellipsis + else if (result.EndsWith(wxT("\u2026"))) + result = result.Left( result.length() - 1 ); + } + return result; + } + } + }; + + return *this; +} + wxString TranslatableString::DoGetContext( const Formatter &formatter ) { return formatter ? formatter( {}, Request::Context ) : wxString{}; diff --git a/src/ShuttleGui.cpp b/src/ShuttleGui.cpp index 42bd79f00..4ed4589eb 100644 --- a/src/ShuttleGui.cpp +++ b/src/ShuttleGui.cpp @@ -2091,7 +2091,7 @@ void ShuttleGuiBase::UpdateSizersCore(bool bPrepend, int Flags, bool prompt) mpWind->SetToolTip( mItem.mToolTip.Translation() ); if ( !mItem.mName.empty() ) - mpWind->SetName( wxStripMenuCodes( mItem.mName.Translation() ) ); + mpWind->SetName( mItem.mName.Strip().Translation() ); if ( !mItem.mNameSuffix.empty() ) mpWind->SetName(