juce_LocalisedStrings.cpp 7.1 KB

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