28 loadFromText (fileContents, ignoreCase);
37 : languageName (other.languageName), countryCodes (other.countryCodes),
38 translations (other.translations), fallback (createCopyIfNotNull (other.fallback.get()))
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;
92 std::unique_ptr<LocalisedStrings> currentMappings;
94 static int findCloseQuote (
const String& text,
int startPos)
96 juce_wchar lastChar = 0;
103 if (c == 0 || (c ==
'"' && lastChar !=
'\\'))
115 return s.
replace (
"\\\"",
"\"")
123 void LocalisedStrings::loadFromText (
const String& fileContents,
bool ignoreCase)
125 translations.setIgnoresCase (ignoreCase);
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:"))
151 languageName = line.substring (9).trim();
153 else if (line.startsWithIgnoreCase (
"countries:"))
155 countryCodes.addTokens (line.substring (10).trim(),
true);
157 countryCodes.removeEmptyStrings();
161 translations.minimiseStorageOverheads();
166 jassert (languageName == other.languageName);
167 jassert (countryCodes == other.countryCodes);
169 translations.addArray (other.translations);
181 currentMappings.reset (newTranslations);
186 return currentMappings.get();
201 return mappings->translate (text, resultIfNotFound);
203 return resultIfNotFound;
String translate(const String &text) const
Attempts to look up a string and return its localised version.
#define JUCE_API
This macro is added to all JUCE public class declarations.
void setFallback(LocalisedStrings *fallbackStrings)
Gives this object a set of strings to use as a fallback if a string isn't found.
static void setCurrentMappings(LocalisedStrings *newTranslations)
Selects the current set of mappings to be used by the system.
~LocalisedStrings()
Destructor.
CharPointerType getCharPointer() const noexcept
Returns the character pointer currently being used to store this string.
int addLines(StringRef stringToBreakUp)
Breaks up a string into lines and adds them to this array.
String getValue(StringRef, const String &defaultReturnValue) const
Finds the value corresponding to a key string.
A special array for holding a list of strings.
LocalisedStrings(const String &fileContents, bool ignoreCaseOfKeys)
Creates a set of translations from the text of a translation file.
static LocalisedStrings * getCurrentMappings()
Returns the currently selected set of mappings.
A simple spin-lock class that can be used as a simple, low-overhead mutex for uncontended situations...
Used to convert strings to localised foreign-language versions.
String loadFileAsString() const
Reads a file into memory as a string.
juce_wchar getAndAdvance() noexcept
Returns the character that this pointer is currently pointing to, and then advances the pointer to po...
static String translateWithCurrentMappings(const String &text)
Tries to translate a string using the currently selected set of mappings.
Represents a local file or directory.
void addStrings(const LocalisedStrings &)
Adds and merges another set of translations into this set.
String replace(StringRef stringToReplace, StringRef stringToInsertInstead, bool ignoreCase=false) const
Replaces all occurrences of a substring with another string.
bool containsKey(StringRef key) const noexcept
Returns true if the given key exists.
Automatically locks and unlocks a mutex object.
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...