2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_LOCALISEDSTRINGS_JUCEHEADER__
27 #define __JUCE_LOCALISEDSTRINGS_JUCEHEADER__
29 #include "juce_StringPairArray.h"
30 #include "../core/juce_Singleton.h"
31 #include "../io/files/juce_File.h"
34 //==============================================================================
35 /** Used in the same way as the T(text) macro, this will attempt to translate a
36 string into a localised version using the LocalisedStrings class.
40 #define TRANS(stringLiteral) \
41 JUCE_NAMESPACE::LocalisedStrings::translateWithCurrentMappings (stringLiteral)
45 //==============================================================================
47 Used to convert strings to localised foreign-language versions.
49 This is basically a look-up table of strings and their translated equivalents.
50 It can be loaded from a text file, so that you can supply a set of localised
51 versions of strings that you use in your app.
53 To use it in your code, simply call the translate() method on each string that
54 might have foreign versions, and if none is found, the method will just return
57 The translation file should start with some lines specifying a description of
58 the language it contains, and also a list of ISO country codes where it might
59 be appropriate to use the file. After that, each line of the file should contain
60 a pair of quoted strings with an '=' sign.
62 E.g. for a french translation, the file might be:
66 countries: fr be mc ch lu
69 "goodbye" = "au revoir"
72 If the strings need to contain a quote character, they can use '\"' instead, and
73 if the first non-whitespace character on a line isn't a quote, then it's ignored,
74 (you can use this to add comments).
76 Note that this is a singleton class, so don't create or destroy the object directly.
77 There's also a TRANS(text) macro defined to make it easy to use the this.
80 printSomething (TRANS("hello"));
83 This macro is used in the Juce classes themselves, so your application has a chance to
84 intercept and translate any internal Juce text strings that might be shown. (You can easily
85 get a list of all the messages by searching for the TRANS() macro in the Juce source
88 class JUCE_API LocalisedStrings
91 //==============================================================================
92 /** Creates a set of translations from the text of a translation file.
94 When you create one of these, you can call setCurrentMappings() to make it
95 the set of mappings that the system's using.
97 LocalisedStrings (const String
& fileContents
);
99 /** Creates a set of translations from a file.
101 When you create one of these, you can call setCurrentMappings() to make it
102 the set of mappings that the system's using.
104 LocalisedStrings (const File
& fileToLoad
);
109 //==============================================================================
110 /** Selects the current set of mappings to be used by the system.
112 The object you pass in will be automatically deleted when no longer needed, so
113 don't keep a pointer to it. You can also pass in zero to remove the current
116 See also the TRANS() macro, which uses the current set to do its translation.
118 @see translateWithCurrentMappings
120 static void setCurrentMappings (LocalisedStrings
* newTranslations
);
122 /** Returns the currently selected set of mappings.
124 This is the object that was last passed to setCurrentMappings(). It may
125 be 0 if none has been created.
127 static LocalisedStrings
* getCurrentMappings();
129 /** Tries to translate a string using the currently selected set of mappings.
131 If no mapping has been set, or if the mapping doesn't contain a translation
132 for the string, this will just return the original string.
134 See also the TRANS() macro, which uses this method to do its translation.
136 @see setCurrentMappings, getCurrentMappings
138 static String
translateWithCurrentMappings (const String
& text
);
140 /** Tries to translate a string using the currently selected set of mappings.
142 If no mapping has been set, or if the mapping doesn't contain a translation
143 for the string, this will just return the original string.
145 See also the TRANS() macro, which uses this method to do its translation.
147 @see setCurrentMappings, getCurrentMappings
149 static String
translateWithCurrentMappings (const char* text
);
151 //==============================================================================
152 /** Attempts to look up a string and return its localised version.
154 If the string isn't found in the list, the original string will be returned.
156 String
translate (const String
& text
) const;
158 /** Returns the name of the language specified in the translation file.
160 This is specified in the file using a line starting with "language:", e.g.
165 String
getLanguageName() const { return languageName
; }
167 /** Returns the list of suitable country codes listed in the translation file.
169 These is specified in the file using a line starting with "countries:", e.g.
171 countries: fr be mc ch lu
174 The country codes are supposed to be 2-character ISO complient codes.
176 const StringArray
& getCountryCodes() const { return countryCodes
; }
179 //==============================================================================
180 /** Indicates whether to use a case-insensitive search when looking up a string.
181 This defaults to true.
183 void setIgnoresCase (bool shouldIgnoreCase
);
186 //==============================================================================
188 StringArray countryCodes
;
189 StringPairArray translations
;
191 void loadFromText (const String
& fileContents
);
193 JUCE_LEAK_DETECTOR (LocalisedStrings
);
197 #endif // __JUCE_LOCALISEDSTRINGS_JUCEHEADER__