JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_LocalisedStrings.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
27{
28 loadFromText (fileContents, ignoreCase);
29}
30
31LocalisedStrings::LocalisedStrings (const File& fileToLoad, bool ignoreCase)
32{
33 loadFromText (fileToLoad.loadFileAsString(), ignoreCase);
34}
35
37 : languageName (other.languageName), countryCodes (other.countryCodes),
38 translations (other.translations), fallback (createCopyIfNotNull (other.fallback.get()))
39{
40}
41
42LocalisedStrings& LocalisedStrings::operator= (const LocalisedStrings& other)
43{
44 languageName = other.languageName;
45 countryCodes = other.countryCodes;
46 translations = other.translations;
47 fallback.reset (createCopyIfNotNull (other.fallback.get()));
48 return *this;
49}
50
51//==============================================================================
53{
54 if (fallback != nullptr && ! translations.containsKey (text))
55 return fallback->translate (text);
56
57 return translations.getValue (text, text);
58}
59
61{
62 if (fallback != nullptr && ! translations.containsKey (text))
63 return fallback->translate (text, resultIfNotFound);
64
65 return translations.getValue (text, resultIfNotFound);
66}
67
68namespace
69{
70 #if JUCE_CHECK_MEMORY_LEAKS
71 // By using this object to force a LocalisedStrings object to be created
72 // before the currentMappings object, we can force the static order-of-destruction to
73 // delete the currentMappings object first, which avoids a bogus leak warning.
74 // (Oddly, just creating a LocalisedStrings on the stack doesn't work in gcc, it
75 // has to be created with 'new' for this to work..)
77 {
79 {
81 }
82 };
83
85 #endif
86
87 SpinLock currentMappingsLock;
89
90 static int findCloseQuote (const String& text, int startPos)
91 {
93 auto t = text.getCharPointer() + startPos;
94
95 for (;;)
96 {
97 auto c = t.getAndAdvance();
98
99 if (c == 0 || (c == '"' && lastChar != '\\'))
100 break;
101
102 lastChar = c;
103 ++startPos;
104 }
105
106 return startPos;
107 }
108
109 static String unescapeString (const String& s)
110 {
111 return s.replace ("\\\"", "\"")
112 .replace ("\\\'", "\'")
113 .replace ("\\t", "\t")
114 .replace ("\\r", "\r")
115 .replace ("\\n", "\n");
116 }
117}
118
119void LocalisedStrings::loadFromText (const String& fileContents, bool ignoreCase)
120{
121 translations.setIgnoresCase (ignoreCase);
122
123 StringArray lines;
124 lines.addLines (fileContents);
125
126 for (auto& l : lines)
127 {
128 auto line = l.trim();
129
130 if (line.startsWithChar ('"'))
131 {
132 auto closeQuote = findCloseQuote (line, 1);
133 auto originalText = unescapeString (line.substring (1, closeQuote));
134
135 if (originalText.isNotEmpty())
136 {
137 auto openingQuote = findCloseQuote (line, closeQuote + 1);
139 auto newText = unescapeString (line.substring (openingQuote + 1, closeQuote));
140
141 if (newText.isNotEmpty())
142 translations.set (originalText, newText);
143 }
144 }
145 else if (line.startsWithIgnoreCase ("language:"))
146 {
147 languageName = line.substring (9).trim();
148 }
149 else if (line.startsWithIgnoreCase ("countries:"))
150 {
151 countryCodes.addTokens (line.substring (10).trim(), true);
152 countryCodes.trim();
153 countryCodes.removeEmptyStrings();
154 }
155 }
156
157 translations.minimiseStorageOverheads();
158}
159
161{
162 jassert (languageName == other.languageName);
163 jassert (countryCodes == other.countryCodes);
164
165 translations.addArray (other.translations);
166}
167
169{
170 fallback.reset (f);
171}
172
173//==============================================================================
179
184
187
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)); }
191
192JUCE_API String translate (const String& text, const String& resultIfNotFound)
193{
195
196 if (auto* mappings = LocalisedStrings::getCurrentMappings())
197 return mappings->translate (text, resultIfNotFound);
198
199 return resultIfNotFound;
200}
201
202} // namespace juce
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...
Represents a local file or directory.
Definition juce_File.h:45
String loadFileAsString() const
Reads a file into memory as a string.
Automatically locks and unlocks a mutex object.
Used to convert strings to localised foreign-language versions.
static void setCurrentMappings(LocalisedStrings *newTranslations)
Selects the current set of mappings to be used by the system.
static String translateWithCurrentMappings(const String &text)
Tries to translate a string using the currently selected set of mappings.
String translate(const String &text) const
Attempts to look up a string and return its localised version.
static LocalisedStrings * getCurrentMappings()
Returns the currently selected set of mappings.
void addStrings(const LocalisedStrings &)
Adds and merges another set of translations into this set.
LocalisedStrings(const String &fileContents, bool ignoreCaseOfKeys)
Creates a set of translations from the text of a translation file.
void setFallback(LocalisedStrings *fallbackStrings)
Gives this object a set of strings to use as a fallback if a string isn't found.
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Breaks up a string into tokens and adds them to this array.
void setIgnoresCase(bool shouldIgnoreCase)
Indicates whether to use a case-insensitive search when looking up a key string.
String getValue(StringRef, const String &defaultReturnValue) const
Finds the value corresponding to a key string.
bool containsKey(StringRef key) const noexcept
Returns true if the given key exists.
void set(const String &key, const String &value)
Adds or amends a key/value pair.
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
void addArray(const StringPairArray &other)
Adds the items from another array to this one.
The JUCE String class!
Definition juce_String.h:53
String trim() const
Returns a copy of this string with any whitespace characters removed from the start and end.
String substring(int startIndex, int endIndex) const
Returns a subsection of the string.
#define jassert(expression)
Platform-independent assertion macro.
JUCE Namespace.
Type * createCopyIfNotNull(const Type *objectToCopy)
If a pointer is non-null, this returns a new copy of the object that it points to,...
Definition juce_Memory.h:60
wchar_t juce_wchar
A platform-independent 32-bit unicode character type.
JUCE_API String translate(const String &text)
Uses the LocalisedStrings class to translate the given string literal.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88