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.

182 lines
6.2KB

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