1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-16 00:34:19 +00:00

Added a LocalisedString::setFallback() method.

This commit is contained in:
jules 2013-12-29 11:02:59 +00:00
parent 2b0f3f45e0
commit ab053c5503
4 changed files with 29 additions and 2 deletions

View file

@ -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<LocalisedStrings> 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)
{

View file

@ -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<LocalisedStrings> fallback;
void loadFromText (const String&, bool ignoreCase);

View file

@ -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);

View file

@ -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; }