28 loadFromText (fileContents, 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()));
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;
88 std::unique_ptr<LocalisedStrings> currentMappings;
90 static int findCloseQuote (
const String& text,
int startPos)
92 juce_wchar lastChar = 0;
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();
188JUCE_API
String translate (
const String& text) {
return juce::translate (text, text); }
189JUCE_API String translate (
const char* text) {
return juce::translate (String (text)); }
190JUCE_API String translate (CharPointer_UTF8 text) {
return juce::translate (String (text)); }
192JUCE_API String translate (
const String& text,
const String& resultIfNotFound)
197 return mappings->translate (text, resultIfNotFound);
199 return resultIfNotFound;
String loadFileAsString() const
static void setCurrentMappings(LocalisedStrings *newTranslations)
static String translateWithCurrentMappings(const String &text)
String translate(const String &text) const
static LocalisedStrings * getCurrentMappings()
void addStrings(const LocalisedStrings &)
LocalisedStrings(const String &fileContents, bool ignoreCaseOfKeys)
void setFallback(LocalisedStrings *fallbackStrings)
GenericScopedLock< SpinLock > ScopedLockType
void removeEmptyStrings(bool removeWhitespaceStrings=true)
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
void setIgnoresCase(bool shouldIgnoreCase)
String getValue(StringRef, const String &defaultReturnValue) const
bool containsKey(StringRef key) const noexcept
void set(const String &key, const String &value)
void minimiseStorageOverheads()
void addArray(const StringPairArray &other)
String substring(int startIndex, int endIndex) const