33 loadFromText (
fileToLoad.loadFileAsString(), ignoreCase);
37 : languageName (
other.languageName), countryCodes (
other.countryCodes),
38 translations (
other.translations), fallback (createCopyIfNotNull (
other.fallback.get()))
42LocalisedStrings& LocalisedStrings::operator= (
const LocalisedStrings& other)
44 languageName = other.languageName;
45 countryCodes = other.countryCodes;
46 translations = other.translations;
47 fallback.reset (createCopyIfNotNull (other.fallback.get()));
58 if (fallback !=
nullptr && ! translations.
containsKey (text))
59 return fallback->translate (text);
61 return translations.
getValue (text, text);
66 if (fallback !=
nullptr && ! translations.
containsKey (text))
74 #if JUCE_CHECK_MEMORY_LEAKS
88 LeakAvoidanceTrick leakAvoidanceTrick;
91 SpinLock currentMappingsLock;
92 std::unique_ptr<LocalisedStrings> currentMappings;
94 static int findCloseQuote (
const String& text,
int startPos)
96 juce_wchar lastChar = 0;
97 auto t = text.getCharPointer() + startPos;
101 auto c = t.getAndAdvance();
103 if (c == 0 || (c ==
'"' && lastChar !=
'\\'))
113 static String unescapeString (
const String& s)
115 return s.replace (
"\\\"",
"\"")
116 .replace (
"\\\'",
"\'")
117 .replace (
"\\t",
"\t")
118 .replace (
"\\r",
"\r")
119 .replace (
"\\n",
"\n");
123void LocalisedStrings::loadFromText (
const String& fileContents,
bool ignoreCase)
128 lines.addLines (fileContents);
130 for (
auto& l : lines)
132 auto line = l.trim();
134 if (line.startsWithChar (
'"'))
136 auto closeQuote = findCloseQuote (line, 1);
137 auto originalText = unescapeString (line.substring (1, closeQuote));
139 if (originalText.isNotEmpty())
141 auto openingQuote = findCloseQuote (line, closeQuote + 1);
142 closeQuote = findCloseQuote (line, openingQuote + 1);
143 auto newText = unescapeString (line.substring (openingQuote + 1, closeQuote));
145 if (newText.isNotEmpty())
146 translations.
set (originalText, newText);
149 else if (line.startsWithIgnoreCase (
"language:"))
153 else if (line.startsWithIgnoreCase (
"countries:"))
155 countryCodes.
addTokens (line.substring (10).trim(),
true);
166 jassert (languageName ==
other.languageName);
167 jassert (countryCodes ==
other.countryCodes);
193JUCE_API String translate (
const char* text) {
return juce::translate (String (text)); }
194JUCE_API String translate (CharPointer_UTF8 text) {
return juce::translate (String (text)); }
196JUCE_API String translate (
const String& text,
const String& resultIfNotFound)
201 return mappings->translate (text, resultIfNotFound);
203 return resultIfNotFound;
Holds a resizable array of primitive or copy-by-value objects.
Array()=default
Creates an empty array.
Represents a local file or directory.
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.
~LocalisedStrings()
Destructor.
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.
GenericScopedLock< SpinLock > ScopedLockType
Provides the type of scoped lock to use for locking a SpinLock.
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.
#define JUCE_API
This macro is added to all JUCE public class declarations.