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.

203 lines
6.6KB

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