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.

241 lines
9.5KB

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