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;
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 !=
'\\'))
111 return s.
replace (
"\\\"",
"\"")
119void LocalisedStrings::loadFromText (
const String& fileContents,
bool ignoreCase)
121 translations.setIgnoresCase (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:"))
147 languageName = line.substring (9).trim();
149 else if (line.startsWithIgnoreCase (
"countries:"))
151 countryCodes.addTokens (line.substring (10).trim(),
true);
153 countryCodes.removeEmptyStrings();
157 translations.minimiseStorageOverheads();
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)); }
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
String replace(StringRef stringToReplace, StringRef stringToInsertInstead, bool ignoreCase=false) const