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);
192JUCE_API
String translate (
const String& text) {
return juce::translate (text, text); }
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;
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