diff --git a/modules/juce_core/text/juce_LocalisedStrings.cpp b/modules/juce_core/text/juce_LocalisedStrings.cpp index f2098a0b22..1d70e4e8a3 100644 --- a/modules/juce_core/text/juce_LocalisedStrings.cpp +++ b/modules/juce_core/text/juce_LocalisedStrings.cpp @@ -43,11 +43,17 @@ LocalisedStrings::~LocalisedStrings() //============================================================================== String LocalisedStrings::translate (const String& text) const { + if (fallback != nullptr && ! translations.containsKey (text)) + return fallback->translate (text); + return translations.getValue (text, text); } String LocalisedStrings::translate (const String& text, const String& resultIfNotFound) const { + if (fallback != nullptr && ! translations.containsKey (text)) + return fallback->translate (text, resultIfNotFound); + return translations.getValue (text, resultIfNotFound); } @@ -73,7 +79,7 @@ namespace SpinLock currentMappingsLock; ScopedPointer currentMappings; - int findCloseQuote (const String& text, int startPos) + static int findCloseQuote (const String& text, int startPos) { juce_wchar lastChar = 0; String::CharPointerType t (text.getCharPointer() + startPos); @@ -92,7 +98,7 @@ namespace return startPos; } - String unescapeString (const String& s) + static String unescapeString (const String& s) { return s.replace ("\\\"", "\"") .replace ("\\\'", "\'") @@ -141,6 +147,8 @@ void LocalisedStrings::loadFromText (const String& fileContents, bool ignoreCase countryCodes.removeEmptyStrings(); } } + + translations.minimiseStorageOverheads(); } void LocalisedStrings::addStrings (const LocalisedStrings& other) @@ -151,6 +159,11 @@ void LocalisedStrings::addStrings (const LocalisedStrings& other) translations.addArray (other.translations); } +void LocalisedStrings::setFallback (LocalisedStrings* f) +{ + fallback = f; +} + //============================================================================== void LocalisedStrings::setCurrentMappings (LocalisedStrings* newTranslations) { diff --git a/modules/juce_core/text/juce_LocalisedStrings.h b/modules/juce_core/text/juce_LocalisedStrings.h index 69e9df9b76..acf1da8283 100644 --- a/modules/juce_core/text/juce_LocalisedStrings.h +++ b/modules/juce_core/text/juce_LocalisedStrings.h @@ -183,11 +183,18 @@ public: */ void addStrings (const LocalisedStrings&); + /** Gives this object a set of strings to use as a fallback if a string isn't found. + The object that is passed-in will be owned and deleted by this object + when no longer needed. It can be nullptr to clear the existing fallback object. + */ + void setFallback (LocalisedStrings* fallbackStrings); + private: //============================================================================== String languageName; StringArray countryCodes; StringPairArray translations; + ScopedPointer fallback; void loadFromText (const String&, bool ignoreCase); diff --git a/modules/juce_core/text/juce_StringPairArray.cpp b/modules/juce_core/text/juce_StringPairArray.cpp index 7912cd976e..3275d2d4aa 100644 --- a/modules/juce_core/text/juce_StringPairArray.cpp +++ b/modules/juce_core/text/juce_StringPairArray.cpp @@ -78,6 +78,11 @@ String StringPairArray::getValue (StringRef key, const String& defaultReturnValu return defaultReturnValue; } +bool StringPairArray::containsKey (StringRef key) const noexcept +{ + return keys.contains (key); +} + void StringPairArray::set (const String& key, const String& value) { const int i = keys.indexOf (key, ignoreCase); diff --git a/modules/juce_core/text/juce_StringPairArray.h b/modules/juce_core/text/juce_StringPairArray.h index add4a60c09..e1c774d124 100644 --- a/modules/juce_core/text/juce_StringPairArray.h +++ b/modules/juce_core/text/juce_StringPairArray.h @@ -85,6 +85,8 @@ public: */ String getValue (StringRef, const String& defaultReturnValue) const; + /** Returns true if the given key exists. */ + bool containsKey (StringRef key) const noexcept; /** Returns a list of all keys in the array. */ const StringArray& getAllKeys() const noexcept { return keys; }