28 loadFromText (fileContents, ignoreCase);
37 : languageName (other.languageName), countryCodes (other.countryCodes),
42LocalisedStrings& LocalisedStrings::operator= (
const LocalisedStrings& other)
44 languageName = other.languageName;
45 countryCodes = other.countryCodes;
46 translations = other.translations;
54 if (fallback !=
nullptr && ! translations.
containsKey (text))
55 return fallback->translate (text);
57 return translations.
getValue (text, text);
62 if (fallback !=
nullptr && ! translations.
containsKey (text))
63 return fallback->translate (text, resultIfNotFound);
65 return translations.
getValue (text, resultIfNotFound);
70 #if JUCE_CHECK_MEMORY_LEAKS
76 struct LeakAvoidanceTrick
84 LeakAvoidanceTrick leakAvoidanceTrick;
87 SpinLock currentMappingsLock;
90 static int findCloseQuote (
const String& text,
int startPos)
93 auto t = text.getCharPointer() + startPos;
97 auto c = t.getAndAdvance();
99 if (c == 0 || (c ==
'"' && lastChar !=
'\\'))
109 static String unescapeString (
const String& s)
111 return s.replace (
"\\\"",
"\"")
112 .replace (
"\\\'",
"\'")
113 .replace (
"\\t",
"\t")
114 .replace (
"\\r",
"\r")
115 .replace (
"\\n",
"\n");
119void LocalisedStrings::loadFromText (
const String& fileContents,
bool ignoreCase)
124 lines.addLines (fileContents);
126 for (
auto& l : lines)
128 auto line = l.trim();
130 if (line.startsWithChar (
'"'))
132 auto closeQuote = findCloseQuote (line, 1);
133 auto originalText = unescapeString (line.substring (1, closeQuote));
135 if (originalText.isNotEmpty())
137 auto openingQuote = findCloseQuote (line, closeQuote + 1);
138 closeQuote = findCloseQuote (line, openingQuote + 1);
139 auto newText = unescapeString (line.substring (openingQuote + 1, closeQuote));
141 if (newText.isNotEmpty())
142 translations.
set (originalText, newText);
145 else if (line.startsWithIgnoreCase (
"language:"))
149 else if (line.startsWithIgnoreCase (
"countries:"))
151 countryCodes.
addTokens (line.substring (10).trim(),
true);
162 jassert (languageName == other.languageName);
163 jassert (countryCodes == other.countryCodes);
165 translations.
addArray (other.translations);
177 currentMappings.
reset (newTranslations);
182 return currentMappings.
get();
197 return mappings->translate (text, resultIfNotFound);
199 return resultIfNotFound;
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...
Represents a local file or directory.
String loadFileAsString() const
Reads a file into memory as a string.
Automatically locks and unlocks a mutex object.
Used to convert strings to localised foreign-language versions.
static void setCurrentMappings(LocalisedStrings *newTranslations)
Selects the current set of mappings to be used by the system.
static String translateWithCurrentMappings(const String &text)
Tries to translate a string using the currently selected set of mappings.
String translate(const String &text) const
Attempts to look up a string and return its localised version.
static LocalisedStrings * getCurrentMappings()
Returns the currently selected set of mappings.
void addStrings(const LocalisedStrings &)
Adds and merges another set of translations into this set.
LocalisedStrings(const String &fileContents, bool ignoreCaseOfKeys)
Creates a set of translations from the text of a translation file.
void setFallback(LocalisedStrings *fallbackStrings)
Gives this object a set of strings to use as a fallback if a string isn't found.
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Breaks up a string into tokens and adds them to this array.
void setIgnoresCase(bool shouldIgnoreCase)
Indicates whether to use a case-insensitive search when looking up a key string.
String getValue(StringRef, const String &defaultReturnValue) const
Finds the value corresponding to a key string.
bool containsKey(StringRef key) const noexcept
Returns true if the given key exists.
void set(const String &key, const String &value)
Adds or amends a key/value pair.
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
void addArray(const StringPairArray &other)
Adds the items from another array to this one.
String trim() const
Returns a copy of this string with any whitespace characters removed from the start and end.
String substring(int startIndex, int endIndex) const
Returns a subsection of the string.
Type * createCopyIfNotNull(const Type *objectToCopy)
If a pointer is non-null, this returns a new copy of the object that it points to,...
wchar_t juce_wchar
A platform-independent 32-bit unicode character type.
JUCE_API String translate(const String &text)
Uses the LocalisedStrings class to translate the given string literal.