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.

246 lines
10.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #pragma once
  24. //==============================================================================
  25. /**
  26. Used to convert strings to localised foreign-language versions.
  27. This is basically a look-up table of strings and their translated equivalents.
  28. It can be loaded from a text file, so that you can supply a set of localised
  29. versions of strings that you use in your app.
  30. To use it in your code, simply call the translate() method on each string that
  31. might have foreign versions, and if none is found, the method will just return
  32. the original string.
  33. The translation file should start with some lines specifying a description of
  34. the language it contains, and also a list of ISO country codes where it might
  35. be appropriate to use the file. After that, each line of the file should contain
  36. a pair of quoted strings with an '=' sign.
  37. E.g. for a french translation, the file might be:
  38. @code
  39. language: French
  40. countries: fr be mc ch lu
  41. "hello" = "bonjour"
  42. "goodbye" = "au revoir"
  43. @endcode
  44. If the strings need to contain a quote character, they can use '\"' instead, and
  45. if the first non-whitespace character on a line isn't a quote, then it's ignored,
  46. (you can use this to add comments).
  47. Note that this is a singleton class, so don't create or destroy the object directly.
  48. There's also a TRANS(text) macro defined to make it easy to use the this.
  49. E.g. @code
  50. printSomething (TRANS("hello"));
  51. @endcode
  52. This macro is used in the Juce classes themselves, so your application has a chance to
  53. intercept and translate any internal Juce text strings that might be shown. (You can easily
  54. get a list of all the messages by searching for the TRANS() macro in the Juce source
  55. code).
  56. */
  57. class JUCE_API LocalisedStrings
  58. {
  59. public:
  60. //==============================================================================
  61. /** Creates a set of translations from the text of a translation file.
  62. When you create one of these, you can call setCurrentMappings() to make it
  63. the set of mappings that the system's using.
  64. */
  65. LocalisedStrings (const String& fileContents, bool ignoreCaseOfKeys);
  66. /** Creates a set of translations from a file.
  67. When you create one of these, you can call setCurrentMappings() to make it
  68. the set of mappings that the system's using.
  69. */
  70. LocalisedStrings (const File& fileToLoad, bool ignoreCaseOfKeys);
  71. LocalisedStrings (const LocalisedStrings&);
  72. LocalisedStrings& operator= (const LocalisedStrings&);
  73. /** Destructor. */
  74. ~LocalisedStrings();
  75. //==============================================================================
  76. /** Selects the current set of mappings to be used by the system.
  77. The object you pass in will be automatically deleted when no longer needed, so
  78. don't keep a pointer to it. You can also pass in nullptr to remove the current
  79. mappings.
  80. See also the TRANS() macro, which uses the current set to do its translation.
  81. @see translateWithCurrentMappings
  82. */
  83. static void setCurrentMappings (LocalisedStrings* newTranslations);
  84. /** Returns the currently selected set of mappings.
  85. This is the object that was last passed to setCurrentMappings(). It may
  86. be nullptr if none has been created.
  87. */
  88. static LocalisedStrings* getCurrentMappings();
  89. /** Tries to translate a string using the currently selected set of mappings.
  90. If no mapping has been set, or if the mapping doesn't contain a translation
  91. for the string, this will just return the original string.
  92. See also the TRANS() macro, which uses this method to do its translation.
  93. @see setCurrentMappings, getCurrentMappings
  94. */
  95. static String translateWithCurrentMappings (const String& text);
  96. /** Tries to translate a string using the currently selected set of mappings.
  97. If no mapping has been set, or if the mapping doesn't contain a translation
  98. for the string, this will just return the original string.
  99. See also the TRANS() macro, which uses this method to do its translation.
  100. @see setCurrentMappings, getCurrentMappings
  101. */
  102. static String translateWithCurrentMappings (const char* text);
  103. //==============================================================================
  104. /** Attempts to look up a string and return its localised version.
  105. If the string isn't found in the list, the original string will be returned.
  106. */
  107. String translate (const String& text) const;
  108. /** Attempts to look up a string and return its localised version.
  109. If the string isn't found in the list, the resultIfNotFound string will be returned.
  110. */
  111. String translate (const String& text, const String& resultIfNotFound) const;
  112. /** Returns the name of the language specified in the translation file.
  113. This is specified in the file using a line starting with "language:", e.g.
  114. @code
  115. language: german
  116. @endcode
  117. */
  118. String getLanguageName() const { return languageName; }
  119. /** Returns the list of suitable country codes listed in the translation file.
  120. These is specified in the file using a line starting with "countries:", e.g.
  121. @code
  122. countries: fr be mc ch lu
  123. @endcode
  124. The country codes are supposed to be 2-character ISO complient codes.
  125. */
  126. const StringArray& getCountryCodes() const { return countryCodes; }
  127. /** Provides access to the actual list of mappings. */
  128. const StringPairArray& getMappings() const { return translations; }
  129. //==============================================================================
  130. /** Adds and merges another set of translations into this set.
  131. Note that the language name and country codes of the new LocalisedStrings
  132. object must match that of this object - an assertion will be thrown if they
  133. don't match.
  134. Any existing values will have their mappings overwritten by the new ones.
  135. */
  136. void addStrings (const LocalisedStrings&);
  137. /** Gives this object a set of strings to use as a fallback if a string isn't found.
  138. The object that is passed-in will be owned and deleted by this object
  139. when no longer needed. It can be nullptr to clear the existing fallback object.
  140. */
  141. void setFallback (LocalisedStrings* fallbackStrings);
  142. private:
  143. //==============================================================================
  144. String languageName;
  145. StringArray countryCodes;
  146. StringPairArray translations;
  147. ScopedPointer<LocalisedStrings> fallback;
  148. friend struct ContainerDeletePolicy<LocalisedStrings>;
  149. void loadFromText (const String&, bool ignoreCase);
  150. JUCE_LEAK_DETECTOR (LocalisedStrings)
  151. };
  152. //==============================================================================
  153. #ifndef TRANS
  154. /** Uses the LocalisedStrings class to translate the given string literal.
  155. This macro is provided for backwards-compatibility, and just calls the translate()
  156. function. In new code, it's recommended that you just call translate() directly
  157. instead, and avoid using macros.
  158. @see translate(), LocalisedStrings
  159. */
  160. #define TRANS(stringLiteral) juce::translate (stringLiteral)
  161. #endif
  162. /** A dummy version of the TRANS macro, used to indicate a string literal that should be
  163. added to the translation file by source-code scanner tools.
  164. Wrapping a string literal in this macro has no effect, but by using it around strings
  165. that your app needs to translate at a later stage, it lets automatic code-scanning tools
  166. find this string and add it to the list of strings that need translation.
  167. */
  168. #define NEEDS_TRANS(stringLiteral) (stringLiteral)
  169. /** Uses the LocalisedStrings class to translate the given string literal.
  170. @see LocalisedStrings
  171. */
  172. JUCE_API String translate (const String& stringLiteral);
  173. /** Uses the LocalisedStrings class to translate the given string literal.
  174. @see LocalisedStrings
  175. */
  176. JUCE_API String translate (const char* stringLiteral);
  177. /** Uses the LocalisedStrings class to translate the given string literal.
  178. @see LocalisedStrings
  179. */
  180. JUCE_API String translate (CharPointer_UTF8 stringLiteral);
  181. /** Uses the LocalisedStrings class to translate the given string literal.
  182. @see LocalisedStrings
  183. */
  184. JUCE_API String translate (const String& stringLiteral, const String& resultIfNotFound);