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.

225 lines
9.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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_JUCEHEADER__
  22. #define __JUCE_LOCALISEDSTRINGS_JUCEHEADER__
  23. #include "juce_StringPairArray.h"
  24. #include "../files/juce_File.h"
  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,
  67. bool ignoreCaseOfKeys);
  68. /** Creates a set of translations from a file.
  69. When you create one of these, you can call setCurrentMappings() to make it
  70. the set of mappings that the system's using.
  71. */
  72. LocalisedStrings (const File& fileToLoad,
  73. bool ignoreCaseOfKeys);
  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 zero 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. private:
  131. //==============================================================================
  132. String languageName;
  133. StringArray countryCodes;
  134. StringPairArray translations;
  135. void loadFromText (const String&, bool ignoreCase);
  136. JUCE_LEAK_DETECTOR (LocalisedStrings)
  137. };
  138. //==============================================================================
  139. #ifndef TRANS
  140. /** Uses the LocalisedStrings class to translate the given string literal.
  141. This macro is provided for backwards-compatibility, and just calls the translate()
  142. function. In new code, it's recommended that you just call translate() directly
  143. instead, and avoid using macros.
  144. @see translate(), LocalisedStrings
  145. */
  146. #define TRANS(stringLiteral) juce::translate (stringLiteral)
  147. #endif
  148. /** A dummy version of the TRANS macro, used to indicate a string literal that should be
  149. added to the translation file by source-code scanner tools.
  150. Wrapping a string literal in this macro has no effect, but by using it around strings
  151. that your app needs to translate at a later stage, it lets automatic code-scanning tools
  152. find this string and add it to the list of strings that need translation.
  153. */
  154. #define NEEDS_TRANS(stringLiteral) (stringLiteral)
  155. /** Uses the LocalisedStrings class to translate the given string literal.
  156. @see LocalisedStrings
  157. */
  158. String translate (const String& stringLiteral);
  159. /** Uses the LocalisedStrings class to translate the given string literal.
  160. @see LocalisedStrings
  161. */
  162. String translate (const char* stringLiteral);
  163. /** Uses the LocalisedStrings class to translate the given string literal.
  164. @see LocalisedStrings
  165. */
  166. String translate (const String& stringLiteral, const String& resultIfNotFound);
  167. #endif // __JUCE_LOCALISEDSTRINGS_JUCEHEADER__