The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. LocalisedStrings::LocalisedStrings (const String& fileContents, bool ignoreCase)
  24. {
  25. loadFromText (fileContents, ignoreCase);
  26. }
  27. LocalisedStrings::LocalisedStrings (const File& fileToLoad, bool ignoreCase)
  28. {
  29. loadFromText (fileToLoad.loadFileAsString(), ignoreCase);
  30. }
  31. LocalisedStrings::LocalisedStrings (const LocalisedStrings& other)
  32. : languageName (other.languageName), countryCodes (other.countryCodes),
  33. translations (other.translations), fallback (createCopyIfNotNull (other.fallback.get()))
  34. {
  35. }
  36. LocalisedStrings& LocalisedStrings::operator= (const LocalisedStrings& other)
  37. {
  38. languageName = other.languageName;
  39. countryCodes = other.countryCodes;
  40. translations = other.translations;
  41. fallback = createCopyIfNotNull (other.fallback.get());
  42. return *this;
  43. }
  44. LocalisedStrings::~LocalisedStrings()
  45. {
  46. }
  47. //==============================================================================
  48. String LocalisedStrings::translate (const String& text) const
  49. {
  50. if (fallback != nullptr && ! translations.containsKey (text))
  51. return fallback->translate (text);
  52. return translations.getValue (text, text);
  53. }
  54. String LocalisedStrings::translate (const String& text, const String& resultIfNotFound) const
  55. {
  56. if (fallback != nullptr && ! translations.containsKey (text))
  57. return fallback->translate (text, resultIfNotFound);
  58. return translations.getValue (text, resultIfNotFound);
  59. }
  60. namespace
  61. {
  62. #if JUCE_CHECK_MEMORY_LEAKS
  63. // By using this object to force a LocalisedStrings object to be created
  64. // before the currentMappings object, we can force the static order-of-destruction to
  65. // delete the currentMappings object first, which avoids a bogus leak warning.
  66. // (Oddly, just creating a LocalisedStrings on the stack doesn't work in gcc, it
  67. // has to be created with 'new' for this to work..)
  68. struct LeakAvoidanceTrick
  69. {
  70. LeakAvoidanceTrick()
  71. {
  72. const ScopedPointer<LocalisedStrings> dummy (new LocalisedStrings (String(), false));
  73. }
  74. };
  75. LeakAvoidanceTrick leakAvoidanceTrick;
  76. #endif
  77. SpinLock currentMappingsLock;
  78. ScopedPointer<LocalisedStrings> currentMappings;
  79. static int findCloseQuote (const String& text, int startPos)
  80. {
  81. juce_wchar lastChar = 0;
  82. String::CharPointerType t (text.getCharPointer() + startPos);
  83. for (;;)
  84. {
  85. const juce_wchar c = t.getAndAdvance();
  86. if (c == 0 || (c == '"' && lastChar != '\\'))
  87. break;
  88. lastChar = c;
  89. ++startPos;
  90. }
  91. return startPos;
  92. }
  93. static String unescapeString (const String& s)
  94. {
  95. return s.replace ("\\\"", "\"")
  96. .replace ("\\\'", "\'")
  97. .replace ("\\t", "\t")
  98. .replace ("\\r", "\r")
  99. .replace ("\\n", "\n");
  100. }
  101. }
  102. void LocalisedStrings::loadFromText (const String& fileContents, bool ignoreCase)
  103. {
  104. translations.setIgnoresCase (ignoreCase);
  105. StringArray lines;
  106. lines.addLines (fileContents);
  107. for (int i = 0; i < lines.size(); ++i)
  108. {
  109. String line (lines[i].trim());
  110. if (line.startsWithChar ('"'))
  111. {
  112. int closeQuote = findCloseQuote (line, 1);
  113. const String originalText (unescapeString (line.substring (1, closeQuote)));
  114. if (originalText.isNotEmpty())
  115. {
  116. const int openingQuote = findCloseQuote (line, closeQuote + 1);
  117. closeQuote = findCloseQuote (line, openingQuote + 1);
  118. const String newText (unescapeString (line.substring (openingQuote + 1, closeQuote)));
  119. if (newText.isNotEmpty())
  120. translations.set (originalText, newText);
  121. }
  122. }
  123. else if (line.startsWithIgnoreCase ("language:"))
  124. {
  125. languageName = line.substring (9).trim();
  126. }
  127. else if (line.startsWithIgnoreCase ("countries:"))
  128. {
  129. countryCodes.addTokens (line.substring (10).trim(), true);
  130. countryCodes.trim();
  131. countryCodes.removeEmptyStrings();
  132. }
  133. }
  134. translations.minimiseStorageOverheads();
  135. }
  136. void LocalisedStrings::addStrings (const LocalisedStrings& other)
  137. {
  138. jassert (languageName == other.languageName);
  139. jassert (countryCodes == other.countryCodes);
  140. translations.addArray (other.translations);
  141. }
  142. void LocalisedStrings::setFallback (LocalisedStrings* f)
  143. {
  144. fallback = f;
  145. }
  146. //==============================================================================
  147. void LocalisedStrings::setCurrentMappings (LocalisedStrings* newTranslations)
  148. {
  149. const SpinLock::ScopedLockType sl (currentMappingsLock);
  150. currentMappings = newTranslations;
  151. }
  152. LocalisedStrings* LocalisedStrings::getCurrentMappings()
  153. {
  154. return currentMappings;
  155. }
  156. String LocalisedStrings::translateWithCurrentMappings (const String& text) { return juce::translate (text); }
  157. String LocalisedStrings::translateWithCurrentMappings (const char* text) { return juce::translate (text); }
  158. JUCE_API String translate (const String& text) { return juce::translate (text, text); }
  159. JUCE_API String translate (const char* text) { return juce::translate (String (text)); }
  160. JUCE_API String translate (CharPointer_UTF8 text) { return juce::translate (String (text)); }
  161. JUCE_API String translate (const String& text, const String& resultIfNotFound)
  162. {
  163. const SpinLock::ScopedLockType sl (currentMappingsLock);
  164. if (const LocalisedStrings* const mappings = LocalisedStrings::getCurrentMappings())
  165. return mappings->translate (text, resultIfNotFound);
  166. return resultIfNotFound;
  167. }