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.

220 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_LOCALISEDSTRINGS_JUCEHEADER__
  19. #define __JUCE_LOCALISEDSTRINGS_JUCEHEADER__
  20. #include "juce_StringPairArray.h"
  21. #include "../files/juce_File.h"
  22. //==============================================================================
  23. /**
  24. Used to convert strings to localised foreign-language versions.
  25. This is basically a look-up table of strings and their translated equivalents.
  26. It can be loaded from a text file, so that you can supply a set of localised
  27. versions of strings that you use in your app.
  28. To use it in your code, simply call the translate() method on each string that
  29. might have foreign versions, and if none is found, the method will just return
  30. the original string.
  31. The translation file should start with some lines specifying a description of
  32. the language it contains, and also a list of ISO country codes where it might
  33. be appropriate to use the file. After that, each line of the file should contain
  34. a pair of quoted strings with an '=' sign.
  35. E.g. for a french translation, the file might be:
  36. @code
  37. language: French
  38. countries: fr be mc ch lu
  39. "hello" = "bonjour"
  40. "goodbye" = "au revoir"
  41. @endcode
  42. If the strings need to contain a quote character, they can use '\"' instead, and
  43. if the first non-whitespace character on a line isn't a quote, then it's ignored,
  44. (you can use this to add comments).
  45. Note that this is a singleton class, so don't create or destroy the object directly.
  46. There's also a TRANS(text) macro defined to make it easy to use the this.
  47. E.g. @code
  48. printSomething (TRANS("hello"));
  49. @endcode
  50. This macro is used in the Juce classes themselves, so your application has a chance to
  51. intercept and translate any internal Juce text strings that might be shown. (You can easily
  52. get a list of all the messages by searching for the TRANS() macro in the Juce source
  53. code).
  54. */
  55. class JUCE_API LocalisedStrings
  56. {
  57. public:
  58. //==============================================================================
  59. /** Creates a set of translations from the text of a translation file.
  60. When you create one of these, you can call setCurrentMappings() to make it
  61. the set of mappings that the system's using.
  62. */
  63. LocalisedStrings (const String& fileContents,
  64. 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,
  70. bool ignoreCaseOfKeys);
  71. /** Destructor. */
  72. ~LocalisedStrings();
  73. //==============================================================================
  74. /** Selects the current set of mappings to be used by the system.
  75. The object you pass in will be automatically deleted when no longer needed, so
  76. don't keep a pointer to it. You can also pass in zero to remove the current
  77. mappings.
  78. See also the TRANS() macro, which uses the current set to do its translation.
  79. @see translateWithCurrentMappings
  80. */
  81. static void setCurrentMappings (LocalisedStrings* newTranslations);
  82. /** Returns the currently selected set of mappings.
  83. This is the object that was last passed to setCurrentMappings(). It may
  84. be nullptr if none has been created.
  85. */
  86. static LocalisedStrings* getCurrentMappings();
  87. /** Tries to translate a string using the currently selected set of mappings.
  88. If no mapping has been set, or if the mapping doesn't contain a translation
  89. for the string, this will just return the original string.
  90. See also the TRANS() macro, which uses this method to do its translation.
  91. @see setCurrentMappings, getCurrentMappings
  92. */
  93. static String translateWithCurrentMappings (const String& text);
  94. /** Tries to translate a string using the currently selected set of mappings.
  95. If no mapping has been set, or if the mapping doesn't contain a translation
  96. for the string, this will just return the original string.
  97. See also the TRANS() macro, which uses this method to do its translation.
  98. @see setCurrentMappings, getCurrentMappings
  99. */
  100. static String translateWithCurrentMappings (const char* text);
  101. //==============================================================================
  102. /** Attempts to look up a string and return its localised version.
  103. If the string isn't found in the list, the original string will be returned.
  104. */
  105. String translate (const String& text) const;
  106. /** Attempts to look up a string and return its localised version.
  107. If the string isn't found in the list, the resultIfNotFound string will be returned.
  108. */
  109. String translate (const String& text, const String& resultIfNotFound) const;
  110. /** Returns the name of the language specified in the translation file.
  111. This is specified in the file using a line starting with "language:", e.g.
  112. @code
  113. language: german
  114. @endcode
  115. */
  116. String getLanguageName() const { return languageName; }
  117. /** Returns the list of suitable country codes listed in the translation file.
  118. These is specified in the file using a line starting with "countries:", e.g.
  119. @code
  120. countries: fr be mc ch lu
  121. @endcode
  122. The country codes are supposed to be 2-character ISO complient codes.
  123. */
  124. const StringArray& getCountryCodes() const { return countryCodes; }
  125. private:
  126. //==============================================================================
  127. String languageName;
  128. StringArray countryCodes;
  129. StringPairArray translations;
  130. void loadFromText (const String&, bool ignoreCase);
  131. JUCE_LEAK_DETECTOR (LocalisedStrings)
  132. };
  133. //==============================================================================
  134. #ifndef TRANS
  135. /** Uses the LocalisedStrings class to translate the given string literal.
  136. This macro is provided for backwards-compatibility, and just calls the translate()
  137. function. In new code, it's recommended that you just call translate() directly
  138. instead, and avoid using macros.
  139. @see translate(), LocalisedStrings
  140. */
  141. #define TRANS(stringLiteral) juce::translate (stringLiteral)
  142. #endif
  143. /** A dummy version of the TRANS macro, used to indicate a string literal that should be
  144. added to the translation file by source-code scanner tools.
  145. Wrapping a string literal in this macro has no effect, but by using it around strings
  146. that your app needs to translate at a later stage, it lets automatic code-scanning tools
  147. find this string and add it to the list of strings that need translation.
  148. */
  149. #define NEEDS_TRANS(stringLiteral) (stringLiteral)
  150. /** Uses the LocalisedStrings class to translate the given string literal.
  151. @see LocalisedStrings
  152. */
  153. String translate (const String& stringLiteral);
  154. /** Uses the LocalisedStrings class to translate the given string literal.
  155. @see LocalisedStrings
  156. */
  157. String translate (const char* stringLiteral);
  158. /** Uses the LocalisedStrings class to translate the given string literal.
  159. @see LocalisedStrings
  160. */
  161. String translate (const String& stringLiteral, const String& resultIfNotFound);
  162. #endif // __JUCE_LOCALISEDSTRINGS_JUCEHEADER__