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.

195 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. LocalisedStrings::LocalisedStrings (const String& fileContents)
  21. {
  22. loadFromText (fileContents);
  23. }
  24. LocalisedStrings::LocalisedStrings (const File& fileToLoad)
  25. {
  26. loadFromText (fileToLoad.loadFileAsString());
  27. }
  28. LocalisedStrings::~LocalisedStrings()
  29. {
  30. }
  31. //==============================================================================
  32. String LocalisedStrings::translate (const String& text) const
  33. {
  34. return translations.getValue (text, text);
  35. }
  36. String LocalisedStrings::translate (const String& text, const String& resultIfNotFound) const
  37. {
  38. return translations.getValue (text, resultIfNotFound);
  39. }
  40. namespace
  41. {
  42. #if JUCE_CHECK_MEMORY_LEAKS
  43. // By using this object to force a LocalisedStrings object to be created
  44. // before the currentMappings object, we can force the static order-of-destruction to
  45. // delete the currentMappings object first, which avoids a bogus leak warning.
  46. // (Oddly, just creating a LocalisedStrings on the stack doesn't work in gcc, it
  47. // has to be created with 'new' for this to work..)
  48. struct LeakAvoidanceTrick
  49. {
  50. LeakAvoidanceTrick()
  51. {
  52. const ScopedPointer<LocalisedStrings> dummy (new LocalisedStrings (String()));
  53. }
  54. };
  55. LeakAvoidanceTrick leakAvoidanceTrick;
  56. #endif
  57. SpinLock currentMappingsLock;
  58. ScopedPointer<LocalisedStrings> currentMappings;
  59. int findCloseQuote (const String& text, int startPos)
  60. {
  61. juce_wchar lastChar = 0;
  62. String::CharPointerType t (text.getCharPointer() + startPos);
  63. for (;;)
  64. {
  65. const juce_wchar c = t.getAndAdvance();
  66. if (c == 0 || (c == '"' && lastChar != '\\'))
  67. break;
  68. lastChar = c;
  69. ++startPos;
  70. }
  71. return startPos;
  72. }
  73. String unescapeString (const String& s)
  74. {
  75. return s.replace ("\\\"", "\"")
  76. .replace ("\\\'", "\'")
  77. .replace ("\\t", "\t")
  78. .replace ("\\r", "\r")
  79. .replace ("\\n", "\n");
  80. }
  81. }
  82. void LocalisedStrings::loadFromText (const String& fileContents)
  83. {
  84. StringArray lines;
  85. lines.addLines (fileContents);
  86. for (int i = 0; i < lines.size(); ++i)
  87. {
  88. String line (lines[i].trim());
  89. if (line.startsWithChar ('"'))
  90. {
  91. int closeQuote = findCloseQuote (line, 1);
  92. const String originalText (unescapeString (line.substring (1, closeQuote)));
  93. if (originalText.isNotEmpty())
  94. {
  95. const int openingQuote = findCloseQuote (line, closeQuote + 1);
  96. closeQuote = findCloseQuote (line, openingQuote + 1);
  97. const String newText (unescapeString (line.substring (openingQuote + 1, closeQuote)));
  98. if (newText.isNotEmpty())
  99. translations.set (originalText, newText);
  100. }
  101. }
  102. else if (line.startsWithIgnoreCase ("language:"))
  103. {
  104. languageName = line.substring (9).trim();
  105. }
  106. else if (line.startsWithIgnoreCase ("countries:"))
  107. {
  108. countryCodes.addTokens (line.substring (10).trim(), true);
  109. countryCodes.trim();
  110. countryCodes.removeEmptyStrings();
  111. }
  112. }
  113. }
  114. void LocalisedStrings::setIgnoresCase (const bool shouldIgnoreCase)
  115. {
  116. translations.setIgnoresCase (shouldIgnoreCase);
  117. }
  118. //==============================================================================
  119. void LocalisedStrings::setCurrentMappings (LocalisedStrings* newTranslations)
  120. {
  121. const SpinLock::ScopedLockType sl (currentMappingsLock);
  122. currentMappings = newTranslations;
  123. }
  124. LocalisedStrings* LocalisedStrings::getCurrentMappings()
  125. {
  126. return currentMappings;
  127. }
  128. String LocalisedStrings::translateWithCurrentMappings (const String& text)
  129. {
  130. return juce::translate (text);
  131. }
  132. String LocalisedStrings::translateWithCurrentMappings (const char* text)
  133. {
  134. return juce::translate (String (text));
  135. }
  136. String translate (const String& text)
  137. {
  138. return translate (text, text);
  139. }
  140. String translate (const char* const literal)
  141. {
  142. const String text (literal);
  143. return translate (text, text);
  144. }
  145. String translate (const String& text, const String& resultIfNotFound)
  146. {
  147. const SpinLock::ScopedLockType sl (currentMappingsLock);
  148. const LocalisedStrings* const currentMappings = LocalisedStrings::getCurrentMappings();
  149. if (currentMappings != nullptr)
  150. return currentMappings->translate (text, resultIfNotFound);
  151. return resultIfNotFound;
  152. }
  153. END_JUCE_NAMESPACE