juce_LocalisedStrings.h 10 KB

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