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.

250 lines
10KB

  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. #ifndef JUCE_LOCALISEDSTRINGS_H_INCLUDED
  24. #define JUCE_LOCALISEDSTRINGS_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Used to convert strings to localised foreign-language versions.
  28. This is basically a look-up table of strings and their translated equivalents.
  29. It can be loaded from a text file, so that you can supply a set of localised
  30. versions of strings that you use in your app.
  31. To use it in your code, simply call the translate() method on each string that
  32. might have foreign versions, and if none is found, the method will just return
  33. the original string.
  34. The translation file should start with some lines specifying a description of
  35. the language it contains, and also a list of ISO country codes where it might
  36. be appropriate to use the file. After that, each line of the file should contain
  37. a pair of quoted strings with an '=' sign.
  38. E.g. for a french translation, the file might be:
  39. @code
  40. language: French
  41. countries: fr be mc ch lu
  42. "hello" = "bonjour"
  43. "goodbye" = "au revoir"
  44. @endcode
  45. If the strings need to contain a quote character, they can use '\"' instead, and
  46. if the first non-whitespace character on a line isn't a quote, then it's ignored,
  47. (you can use this to add comments).
  48. Note that this is a singleton class, so don't create or destroy the object directly.
  49. There's also a TRANS(text) macro defined to make it easy to use the this.
  50. E.g. @code
  51. printSomething (TRANS("hello"));
  52. @endcode
  53. This macro is used in the Juce classes themselves, so your application has a chance to
  54. intercept and translate any internal Juce text strings that might be shown. (You can easily
  55. get a list of all the messages by searching for the TRANS() macro in the Juce source
  56. code).
  57. */
  58. class JUCE_API LocalisedStrings
  59. {
  60. public:
  61. //==============================================================================
  62. /** Creates a set of translations from the text of a translation file.
  63. When you create one of these, you can call setCurrentMappings() to make it
  64. the set of mappings that the system's using.
  65. */
  66. LocalisedStrings (const String& fileContents, bool ignoreCaseOfKeys);
  67. /** Creates a set of translations from a file.
  68. When you create one of these, you can call setCurrentMappings() to make it
  69. the set of mappings that the system's using.
  70. */
  71. LocalisedStrings (const File& fileToLoad, bool ignoreCaseOfKeys);
  72. LocalisedStrings (const LocalisedStrings&);
  73. LocalisedStrings& operator= (const LocalisedStrings&);
  74. /** Destructor. */
  75. ~LocalisedStrings();
  76. //==============================================================================
  77. /** Selects the current set of mappings to be used by the system.
  78. The object you pass in will be automatically deleted when no longer needed, so
  79. don't keep a pointer to it. You can also pass in nullptr to remove the current
  80. mappings.
  81. See also the TRANS() macro, which uses the current set to do its translation.
  82. @see translateWithCurrentMappings
  83. */
  84. static void setCurrentMappings (LocalisedStrings* newTranslations);
  85. /** Returns the currently selected set of mappings.
  86. This is the object that was last passed to setCurrentMappings(). It may
  87. be nullptr if none has been created.
  88. */
  89. static LocalisedStrings* getCurrentMappings();
  90. /** Tries to translate a string using the currently selected set of mappings.
  91. If no mapping has been set, or if the mapping doesn't contain a translation
  92. for the string, this will just return the original string.
  93. See also the TRANS() macro, which uses this method to do its translation.
  94. @see setCurrentMappings, getCurrentMappings
  95. */
  96. static String translateWithCurrentMappings (const String& text);
  97. /** Tries to translate a string using the currently selected set of mappings.
  98. If no mapping has been set, or if the mapping doesn't contain a translation
  99. for the string, this will just return the original string.
  100. See also the TRANS() macro, which uses this method to do its translation.
  101. @see setCurrentMappings, getCurrentMappings
  102. */
  103. static String translateWithCurrentMappings (const char* text);
  104. //==============================================================================
  105. /** Attempts to look up a string and return its localised version.
  106. If the string isn't found in the list, the original string will be returned.
  107. */
  108. String translate (const String& text) const;
  109. /** Attempts to look up a string and return its localised version.
  110. If the string isn't found in the list, the resultIfNotFound string will be returned.
  111. */
  112. String translate (const String& text, const String& resultIfNotFound) const;
  113. /** Returns the name of the language specified in the translation file.
  114. This is specified in the file using a line starting with "language:", e.g.
  115. @code
  116. language: german
  117. @endcode
  118. */
  119. String getLanguageName() const { return languageName; }
  120. /** Returns the list of suitable country codes listed in the translation file.
  121. These is specified in the file using a line starting with "countries:", e.g.
  122. @code
  123. countries: fr be mc ch lu
  124. @endcode
  125. The country codes are supposed to be 2-character ISO complient codes.
  126. */
  127. const StringArray& getCountryCodes() const { return countryCodes; }
  128. /** Provides access to the actual list of mappings. */
  129. const StringPairArray& getMappings() const { return translations; }
  130. //==============================================================================
  131. /** Adds and merges another set of translations into this set.
  132. Note that the language name and country codes of the new LocalisedStrings
  133. object must match that of this object - an assertion will be thrown if they
  134. don't match.
  135. Any existing values will have their mappings overwritten by the new ones.
  136. */
  137. void addStrings (const LocalisedStrings&);
  138. /** Gives this object a set of strings to use as a fallback if a string isn't found.
  139. The object that is passed-in will be owned and deleted by this object
  140. when no longer needed. It can be nullptr to clear the existing fallback object.
  141. */
  142. void setFallback (LocalisedStrings* fallbackStrings);
  143. private:
  144. //==============================================================================
  145. String languageName;
  146. StringArray countryCodes;
  147. StringPairArray translations;
  148. ScopedPointer<LocalisedStrings> fallback;
  149. friend struct ContainerDeletePolicy<LocalisedStrings>;
  150. void loadFromText (const String&, bool ignoreCase);
  151. JUCE_LEAK_DETECTOR (LocalisedStrings)
  152. };
  153. //==============================================================================
  154. #ifndef TRANS
  155. /** Uses the LocalisedStrings class to translate the given string literal.
  156. This macro is provided for backwards-compatibility, and just calls the translate()
  157. function. In new code, it's recommended that you just call translate() directly
  158. instead, and avoid using macros.
  159. @see translate(), LocalisedStrings
  160. */
  161. #define TRANS(stringLiteral) juce::translate (stringLiteral)
  162. #endif
  163. /** A dummy version of the TRANS macro, used to indicate a string literal that should be
  164. added to the translation file by source-code scanner tools.
  165. Wrapping a string literal in this macro has no effect, but by using it around strings
  166. that your app needs to translate at a later stage, it lets automatic code-scanning tools
  167. find this string and add it to the list of strings that need translation.
  168. */
  169. #define NEEDS_TRANS(stringLiteral) (stringLiteral)
  170. /** Uses the LocalisedStrings class to translate the given string literal.
  171. @see LocalisedStrings
  172. */
  173. JUCE_API String translate (const String& stringLiteral);
  174. /** Uses the LocalisedStrings class to translate the given string literal.
  175. @see LocalisedStrings
  176. */
  177. JUCE_API String translate (const char* stringLiteral);
  178. /** Uses the LocalisedStrings class to translate the given string literal.
  179. @see LocalisedStrings
  180. */
  181. JUCE_API String translate (CharPointer_UTF8 stringLiteral);
  182. /** Uses the LocalisedStrings class to translate the given string literal.
  183. @see LocalisedStrings
  184. */
  185. JUCE_API String translate (const String& stringLiteral, const String& resultIfNotFound);
  186. #endif // JUCE_LOCALISEDSTRINGS_H_INCLUDED