28 loadFromText (fileContents, ignoreCase);
37 : languageName (other.languageName), countryCodes (other.countryCodes),
38 translations (other.translations), fallback (createCopyIfNotNull (other.fallback.get()))
42 LocalisedStrings& 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))
67 return fallback->translate (text, resultIfNotFound);
69 return translations.
getValue (text, resultIfNotFound);
74 #if JUCE_CHECK_MEMORY_LEAKS
80 struct LeakAvoidanceTrick
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");
123 void 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);
169 translations.
addArray (other.translations);
181 currentMappings.reset (newTranslations);
186 return currentMappings.get();
193 JUCE_API String translate (
const char* text) {
return juce::translate (String (text)); }
194 JUCE_API String translate (CharPointer_UTF8 text) {
return juce::translate (String (text)); }
196 JUCE_API String translate (
const String& text,
const String& resultIfNotFound)
201 return mappings->translate (text, resultIfNotFound);
203 return resultIfNotFound;