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.

197 lines
7.4KB

  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. /** Used in the same way as the T(text) macro, this will attempt to translate a
  24. string into a localised version using the LocalisedStrings class.
  25. @see LocalisedStrings
  26. */
  27. #define TRANS(stringLiteral) \
  28. juce::LocalisedStrings::translateWithCurrentMappings (stringLiteral)
  29. //==============================================================================
  30. /**
  31. Used to convert strings to localised foreign-language versions.
  32. This is basically a look-up table of strings and their translated equivalents.
  33. It can be loaded from a text file, so that you can supply a set of localised
  34. versions of strings that you use in your app.
  35. To use it in your code, simply call the translate() method on each string that
  36. might have foreign versions, and if none is found, the method will just return
  37. the original string.
  38. The translation file should start with some lines specifying a description of
  39. the language it contains, and also a list of ISO country codes where it might
  40. be appropriate to use the file. After that, each line of the file should contain
  41. a pair of quoted strings with an '=' sign.
  42. E.g. for a french translation, the file might be:
  43. @code
  44. language: French
  45. countries: fr be mc ch lu
  46. "hello" = "bonjour"
  47. "goodbye" = "au revoir"
  48. @endcode
  49. If the strings need to contain a quote character, they can use '\"' instead, and
  50. if the first non-whitespace character on a line isn't a quote, then it's ignored,
  51. (you can use this to add comments).
  52. Note that this is a singleton class, so don't create or destroy the object directly.
  53. There's also a TRANS(text) macro defined to make it easy to use the this.
  54. E.g. @code
  55. printSomething (TRANS("hello"));
  56. @endcode
  57. This macro is used in the Juce classes themselves, so your application has a chance to
  58. intercept and translate any internal Juce text strings that might be shown. (You can easily
  59. get a list of all the messages by searching for the TRANS() macro in the Juce source
  60. code).
  61. */
  62. class JUCE_API LocalisedStrings
  63. {
  64. public:
  65. //==============================================================================
  66. /** Creates a set of translations from the text of a translation file.
  67. When you create one of these, you can call setCurrentMappings() to make it
  68. the set of mappings that the system's using.
  69. */
  70. LocalisedStrings (const String& fileContents);
  71. /** Creates a set of translations from a file.
  72. When you create one of these, you can call setCurrentMappings() to make it
  73. the set of mappings that the system's using.
  74. */
  75. LocalisedStrings (const File& fileToLoad);
  76. /** Destructor. */
  77. ~LocalisedStrings();
  78. //==============================================================================
  79. /** Selects the current set of mappings to be used by the system.
  80. The object you pass in will be automatically deleted when no longer needed, so
  81. don't keep a pointer to it. You can also pass in zero to remove the current
  82. mappings.
  83. See also the TRANS() macro, which uses the current set to do its translation.
  84. @see translateWithCurrentMappings
  85. */
  86. static void setCurrentMappings (LocalisedStrings* newTranslations);
  87. /** Returns the currently selected set of mappings.
  88. This is the object that was last passed to setCurrentMappings(). It may
  89. be 0 if none has been created.
  90. */
  91. static LocalisedStrings* getCurrentMappings();
  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 String& text);
  99. /** Tries to translate a string using the currently selected set of mappings.
  100. If no mapping has been set, or if the mapping doesn't contain a translation
  101. for the string, this will just return the original string.
  102. See also the TRANS() macro, which uses this method to do its translation.
  103. @see setCurrentMappings, getCurrentMappings
  104. */
  105. static String translateWithCurrentMappings (const char* text);
  106. //==============================================================================
  107. /** Attempts to look up a string and return its localised version.
  108. If the string isn't found in the list, the original string will be returned.
  109. */
  110. String translate (const String& text) const;
  111. /** Returns the name of the language specified in the translation file.
  112. This is specified in the file using a line starting with "language:", e.g.
  113. @code
  114. language: german
  115. @endcode
  116. */
  117. String getLanguageName() const { return languageName; }
  118. /** Returns the list of suitable country codes listed in the translation file.
  119. These is specified in the file using a line starting with "countries:", e.g.
  120. @code
  121. countries: fr be mc ch lu
  122. @endcode
  123. The country codes are supposed to be 2-character ISO complient codes.
  124. */
  125. const StringArray& getCountryCodes() const { return countryCodes; }
  126. //==============================================================================
  127. /** Indicates whether to use a case-insensitive search when looking up a string.
  128. This defaults to true.
  129. */
  130. void setIgnoresCase (bool shouldIgnoreCase);
  131. private:
  132. //==============================================================================
  133. String languageName;
  134. StringArray countryCodes;
  135. StringPairArray translations;
  136. void loadFromText (const String& fileContents);
  137. JUCE_LEAK_DETECTOR (LocalisedStrings);
  138. };
  139. #endif // __JUCE_LOCALISEDSTRINGS_JUCEHEADER__