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.

238 lines
9.5KB

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