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.

189 lines
5.7KB

  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. LocalisedStrings::LocalisedStrings (const String& fileContents)
  19. {
  20. loadFromText (fileContents);
  21. }
  22. LocalisedStrings::LocalisedStrings (const File& fileToLoad)
  23. {
  24. loadFromText (fileToLoad.loadFileAsString());
  25. }
  26. LocalisedStrings::~LocalisedStrings()
  27. {
  28. }
  29. //==============================================================================
  30. String LocalisedStrings::translate (const String& text) const
  31. {
  32. return translations.getValue (text, text);
  33. }
  34. String LocalisedStrings::translate (const String& text, const String& resultIfNotFound) const
  35. {
  36. return translations.getValue (text, resultIfNotFound);
  37. }
  38. namespace
  39. {
  40. #if JUCE_CHECK_MEMORY_LEAKS
  41. // By using this object to force a LocalisedStrings object to be created
  42. // before the currentMappings object, we can force the static order-of-destruction to
  43. // delete the currentMappings object first, which avoids a bogus leak warning.
  44. // (Oddly, just creating a LocalisedStrings on the stack doesn't work in gcc, it
  45. // has to be created with 'new' for this to work..)
  46. struct LeakAvoidanceTrick
  47. {
  48. LeakAvoidanceTrick()
  49. {
  50. const ScopedPointer<LocalisedStrings> dummy (new LocalisedStrings (String()));
  51. }
  52. };
  53. LeakAvoidanceTrick leakAvoidanceTrick;
  54. #endif
  55. SpinLock currentMappingsLock;
  56. ScopedPointer<LocalisedStrings> currentMappings;
  57. int findCloseQuote (const String& text, int startPos)
  58. {
  59. juce_wchar lastChar = 0;
  60. String::CharPointerType t (text.getCharPointer() + startPos);
  61. for (;;)
  62. {
  63. const juce_wchar c = t.getAndAdvance();
  64. if (c == 0 || (c == '"' && lastChar != '\\'))
  65. break;
  66. lastChar = c;
  67. ++startPos;
  68. }
  69. return startPos;
  70. }
  71. String unescapeString (const String& s)
  72. {
  73. return s.replace ("\\\"", "\"")
  74. .replace ("\\\'", "\'")
  75. .replace ("\\t", "\t")
  76. .replace ("\\r", "\r")
  77. .replace ("\\n", "\n");
  78. }
  79. }
  80. void LocalisedStrings::loadFromText (const String& fileContents)
  81. {
  82. StringArray lines;
  83. lines.addLines (fileContents);
  84. for (int i = 0; i < lines.size(); ++i)
  85. {
  86. String line (lines[i].trim());
  87. if (line.startsWithChar ('"'))
  88. {
  89. int closeQuote = findCloseQuote (line, 1);
  90. const String originalText (unescapeString (line.substring (1, closeQuote)));
  91. if (originalText.isNotEmpty())
  92. {
  93. const int openingQuote = findCloseQuote (line, closeQuote + 1);
  94. closeQuote = findCloseQuote (line, openingQuote + 1);
  95. const String newText (unescapeString (line.substring (openingQuote + 1, closeQuote)));
  96. if (newText.isNotEmpty())
  97. translations.set (originalText, newText);
  98. }
  99. }
  100. else if (line.startsWithIgnoreCase ("language:"))
  101. {
  102. languageName = line.substring (9).trim();
  103. }
  104. else if (line.startsWithIgnoreCase ("countries:"))
  105. {
  106. countryCodes.addTokens (line.substring (10).trim(), true);
  107. countryCodes.trim();
  108. countryCodes.removeEmptyStrings();
  109. }
  110. }
  111. }
  112. void LocalisedStrings::setIgnoresCase (const bool shouldIgnoreCase)
  113. {
  114. translations.setIgnoresCase (shouldIgnoreCase);
  115. }
  116. //==============================================================================
  117. void LocalisedStrings::setCurrentMappings (LocalisedStrings* newTranslations)
  118. {
  119. const SpinLock::ScopedLockType sl (currentMappingsLock);
  120. currentMappings = newTranslations;
  121. }
  122. LocalisedStrings* LocalisedStrings::getCurrentMappings()
  123. {
  124. return currentMappings;
  125. }
  126. String LocalisedStrings::translateWithCurrentMappings (const String& text)
  127. {
  128. return juce::translate (text);
  129. }
  130. String LocalisedStrings::translateWithCurrentMappings (const char* text)
  131. {
  132. return juce::translate (String (text));
  133. }
  134. String translate (const String& text)
  135. {
  136. return translate (text, text);
  137. }
  138. String translate (const char* const literal)
  139. {
  140. const String text (literal);
  141. return translate (text, text);
  142. }
  143. String translate (const String& text, const String& resultIfNotFound)
  144. {
  145. const SpinLock::ScopedLockType sl (currentMappingsLock);
  146. const LocalisedStrings* const currentMappings = LocalisedStrings::getCurrentMappings();
  147. if (currentMappings != nullptr)
  148. return currentMappings->translate (text, resultIfNotFound);
  149. return resultIfNotFound;
  150. }