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.

215 lines
8.3KB

  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. /** Creates a set of translations from a file.
  65. When you create one of these, you can call setCurrentMappings() to make it
  66. the set of mappings that the system's using.
  67. */
  68. LocalisedStrings (const File& fileToLoad);
  69. /** Destructor. */
  70. ~LocalisedStrings();
  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 zero 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 0 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 complient codes.
  121. */
  122. const StringArray& getCountryCodes() const { return countryCodes; }
  123. //==============================================================================
  124. /** Indicates whether to use a case-insensitive search when looking up a string.
  125. This defaults to true.
  126. */
  127. void setIgnoresCase (bool shouldIgnoreCase);
  128. private:
  129. //==============================================================================
  130. String languageName;
  131. StringArray countryCodes;
  132. StringPairArray translations;
  133. void loadFromText (const String& fileContents);
  134. JUCE_LEAK_DETECTOR (LocalisedStrings);
  135. };
  136. //==============================================================================
  137. #ifndef TRANS
  138. /** Uses the LocalisedStrings class to translate the given string literal.
  139. This macro is provided for backwards-compatibility, and just calls the translate()
  140. function. In new code, it's recommended that you just call translate() directly
  141. instead, and avoid using macros.
  142. @see translate(), LocalisedStrings
  143. */
  144. #define TRANS(stringLiteral) juce::translate (stringLiteral)
  145. #endif
  146. /** Uses the LocalisedStrings class to translate the given string literal.
  147. @see LocalisedStrings
  148. */
  149. String translate (const String& stringLiteral);
  150. /** Uses the LocalisedStrings class to translate the given string literal.
  151. @see LocalisedStrings
  152. */
  153. String translate (const char* stringLiteral);
  154. /** Uses the LocalisedStrings class to translate the given string literal.
  155. @see LocalisedStrings
  156. */
  157. String translate (const String& stringLiteral, const String& resultIfNotFound);
  158. #endif // __JUCE_LOCALISEDSTRINGS_JUCEHEADER__