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.

185 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, bool ignoreCase)
  19. {
  20. loadFromText (fileContents, ignoreCase);
  21. }
  22. LocalisedStrings::LocalisedStrings (const File& fileToLoad, bool ignoreCase)
  23. {
  24. loadFromText (fileToLoad.loadFileAsString(), ignoreCase);
  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(), false));
  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, bool ignoreCase)
  81. {
  82. translations.setIgnoresCase (ignoreCase);
  83. StringArray lines;
  84. lines.addLines (fileContents);
  85. for (int i = 0; i < lines.size(); ++i)
  86. {
  87. String line (lines[i].trim());
  88. if (line.startsWithChar ('"'))
  89. {
  90. int closeQuote = findCloseQuote (line, 1);
  91. const String originalText (unescapeString (line.substring (1, closeQuote)));
  92. if (originalText.isNotEmpty())
  93. {
  94. const int openingQuote = findCloseQuote (line, closeQuote + 1);
  95. closeQuote = findCloseQuote (line, openingQuote + 1);
  96. const String newText (unescapeString (line.substring (openingQuote + 1, closeQuote)));
  97. if (newText.isNotEmpty())
  98. translations.set (originalText, newText);
  99. }
  100. }
  101. else if (line.startsWithIgnoreCase ("language:"))
  102. {
  103. languageName = line.substring (9).trim();
  104. }
  105. else if (line.startsWithIgnoreCase ("countries:"))
  106. {
  107. countryCodes.addTokens (line.substring (10).trim(), true);
  108. countryCodes.trim();
  109. countryCodes.removeEmptyStrings();
  110. }
  111. }
  112. }
  113. //==============================================================================
  114. void LocalisedStrings::setCurrentMappings (LocalisedStrings* newTranslations)
  115. {
  116. const SpinLock::ScopedLockType sl (currentMappingsLock);
  117. currentMappings = newTranslations;
  118. }
  119. LocalisedStrings* LocalisedStrings::getCurrentMappings()
  120. {
  121. return currentMappings;
  122. }
  123. String LocalisedStrings::translateWithCurrentMappings (const String& text)
  124. {
  125. return juce::translate (text);
  126. }
  127. String LocalisedStrings::translateWithCurrentMappings (const char* text)
  128. {
  129. return juce::translate (String (text));
  130. }
  131. String translate (const String& text)
  132. {
  133. return translate (text, text);
  134. }
  135. String translate (const char* const literal)
  136. {
  137. const String text (literal);
  138. return translate (text, text);
  139. }
  140. String translate (const String& text, const String& resultIfNotFound)
  141. {
  142. const SpinLock::ScopedLockType sl (currentMappingsLock);
  143. if (const LocalisedStrings* const mappings = LocalisedStrings::getCurrentMappings())
  144. return mappings->translate (text, resultIfNotFound);
  145. return resultIfNotFound;
  146. }