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.

173 lines
5.3KB

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