Translator.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file terralib/core/translator/Translator.h
23 
24  \brief This class is designed for dealing with multi-language text translation in TerraLib.
25 
26  \author Matheus Cavassan Zaglia
27  \author Gilberto Ribeiro de Queiroz
28 */
29 
30 #ifndef __TERRALIB_CORE_TRANSLATOR_TRANSLATOR_H__
31 #define __TERRALIB_CORE_TRANSLATOR_TRANSLATOR_H__
32 
33 // TerraLib
34 #include "../Config.h"
35 
36 // STL
37 #include <string>
38 #include <map>
39 #include <vector>
40 
41 namespace te
42 {
43  namespace core
44  {
45  /*!
46  \class Translator
47 
48  \brief This singleton is designed to deal with multi-language text translation in TerraLib.
49 
50  The Translator job is to manage "Internationalization" of the TerraLib messages.
51  This class is all about Native Language Support.
52  For each string you want to have a translation, you have to use
53  the special macro TR_TR("string") or TE_TR_PLURAL("string1","string2", int). This macro does nothing with your code;
54  its job is just to mark the code fragment that you want to translate and it does
55  all the job of calling the translation for you. The translations
56  are created with the GNU Gettext Utilities.
57 
58  We provide a cmake module that searchs for all the necessary GNU Gettext tools, and adds a macro that creates the files for
59  your translations.
60  This macro searchs in a given directory for all ".cpp" files and for the given keywords inside the found files,
61  then automatically generates the ".po", ".pot" and ".mo" files when you build your project.
62  The macro implementation can be found in the file build/cmake/modules/FindGettext.cmake.
63 
64  \code
65  #include <terralib/core/translator/Translator.h>
66  ...
67  TE_ADD_TEXT_DOMAIN("terralib_mod_core");
68  ...
69  std::string msg = TE_TR("My Message");
70  ...
71  or
72  ...
73  std::cout << TE_TR_PLURAL("One message", "Two messages", 1);
74  ...
75  \endcode
76 
77  Note that all marked messages will be part of a .pot file from where you can
78  create several po documents, one for each idiom. These .po files
79  can be used to generate a .mo binary file with the translation.
80 
81  \warning It is supposed that PO files are UTF-8 encoded.
82 
83  \ingroup core
84  */
86  {
87 
88  public:
89 
90  static Translator& instance();
91 
92  /*!
93  \brief It tries to translate the specified text string.
94 
95  If no translation is available it will return the
96  original message (always in English).
97 
98  \param message The text to be translated.
99 
100  \return A string of the translated message. You must not delete the memory pointed by the returned pointer.
101 
102  \note The returned message is UTF-8 encoded.
103  */
104  std::string translate(const std::string& message);
105 
106  /*!
107  \brief It tries to translate the specified text string.
108 
109  If no translation is available it will return the
110  original message (always in English).
111 
112  \param message The text to be translated.
113 
114  \return A string of the translated message. You must not delete the memory pointed by the returned pointer.
115 
116  \note The returned message is UTF-8 encoded.
117  */
118  std::string translate(const char* message);
119 
120  /*!
121  \brief It tries to translate the specified text string accounting for plural forms.
122 
123  If no translation is available it will return the
124  original message (always in English).
125 
126  \param msg1 The singular form of the text to be translated.
127  \param msg2 The plural form of the text to be translated.
128  \param n This parameter is used to determine the plural form.
129 
130  \return A string of the translated message. You must not delete the memory pointed by the returned pointer.
131 
132  \note The returned message is UTF-8 encoded.
133  */
134  std::string translate(const std::string& msg1,
135  const std::string& msg2,
136  unsigned int n);
137 
138  /*!
139  \brief It tries to translate the specified text string accounting for plural forms.
140 
141  If no translation is available it will return the
142  original message (always in English).
143 
144  \param msg1 The singular form of the text to be translated.
145  \param msg2 The plural form of the text to be translated.
146  \param n This parameter is used to determine the plural form.
147 
148  \return A string of the translated message. You must not delete the memory pointed by the returned pointer.
149 
150  \note The returned message is UTF-8 encoded.
151  */
152  std::string translate(const char* msg1,
153  const char* msg2,
154  unsigned int n);
155 
156  /*!
157  \brief It sets the locale for the Translator.
158 
159  \param locale A string of the new locale.
160  */
161  void setLocale(const std::string &locale);
162 
163  /*!
164  \brief It adds a new text domain (text catalog).
165 
166  \param textDomain A given message domain (just a name). A text domain is the name of the catalog used to translate the message.
167 
168  \param dir Where the text domain is located.
169 
170  \exception Exception If the text domain already exists it raises an exception. If you are not sure about the existence of a text domain, use the exist() method.
171  */
172  void addTextDomain(const std::string& textDomain, const std::string& dir);
173 
174 
175  /*!
176  \brief It returns true if the text domain (text catalog) exists and false otherwise.
177 
178  \param textDomain A given message domain (just a name).
179  */
180  bool exist(const std::string& textDomain);
181 
182  private:
183 
184  /*! \brief Singleton constructor must be private or protected. */
186 
187  /*! \brief Singleton destructor must be private or protected. */
189 
190  /*! \brief Singleton copy constructor must be private or protected. */
191  Translator(Translator const&); //No copy allowed
192 
193  /*! \brief Singleton copy assignment operator must be private or protected. */
194  Translator& operator=(Translator const&); //No copy allowed
195 
196  private:
197 
198  std::string m_locale; //!< If not empty, it is the current locale.
199  std::map<std::string, std::vector<std::string> > m_textDomainMap; //!< A vector from text domains to base directory for the message catalog.
200  };
201 
202  } // end namespace core
203 } // end namespace te
204 
205 // Check if the TR macro has already been defined
206 // by another application... if so, it will output
207 // an error message and stop compiling!
208 #ifdef TERRALIB_TRANSLATOR_ENABLED
209 #ifdef TE_TR
210 #error "The TE_TR macro has been already defined by another application or code. Please, inform TerraLib Development Team <terralib-team@dpi.inpe.br>, we will be glad to help solving this problem!"
211 #endif
212 #endif
213 
214 /*!
215  \def TE_ADD_TEXT_DOMAIN
216 
217  \brief It adds the given text domain and its directory to the multilingual system.
218 
219  \param domain A given message domain (just a name). A text domain is the name of the catalog used to translate the message.
220  \param dir Where the text domain is located.
221 */
222 #ifdef TERRALIB_TRANSLATOR_ENABLED
223  #define TE_ADD_TEXT_DOMAIN(domain, dir) te::core::Translator::instance().addTextDomain(domain, dir)
224 #else
225  #define TE_ADD_TEXT_DOMAIN(domain, dir) ((void)0)
226 #endif
227 
228 /*!
229  \def TE_TR
230 
231  \brief It marks a string in order to get translated.
232 
233  \param message The text to be translated.
234 
235  Example of usage:
236  \code
237  std::cout << TE_TR("My message!");
238 
239  throw Exception(TE_TR("My other message!"));
240  \endcode
241  */
242 #define TE_TR(message) te::core::Translator::instance().translate(message).c_str()
243 
244 /*!
245  \def TE_TR_PLURAL
246 
247  \brief It marks a string in order to get translated according to plural form.
248 
249  \param message1 The singular form of the text to be translated.
250  \param message2 The plural form of the text to be translated.
251  \param n This parameter is used to determine the plural form.
252 
253  Example of usage:
254  \code
255  int n = f(...);
256 
257  std::cout << TE_TR_PLURAL("One Message!", "Two Messages", n);
258 
259  throw Exception(TE_TR_PLURAL("One Message!", "Two Messages", n));
260  \endcode
261 
262  In the above example, the parameter n can be
263  a threshold that helps to choose between the first or the second construction.
264  If your trabslation file is configured with a theashold of 1,
265  indicating that if n > 1 must choose the second construction,
266  the plural versin will be choosed, otherwise, it will choose the
267  singular form (the fisrt one).
268  */
269 #define TE_TR_PLURAL(message1, message2, n) te::core::Translator::instance().translate(message1, message2, n).c_str()
270 
271 /*!
272  \def TE_TR_LANGUAGE
273 
274  \brief It sets the locale for the Translator.
275 
276  \param locale A string of the new locale.
277  */
278 #define TE_TR_LANGUAGE(locale) te::core::Translator::instance().setLocale(locale)
279 
280 #endif // __TERRALIB_CORE_TRANSLATOR_TRANSLATOR_H__
This singleton is designed to deal with multi-language text translation in TerraLib.
Definition: Translator.h:85
#define TECOREEXPORT
Definition: Config.h:52
TerraLib.
std::string m_locale
If not empty, it is the current locale.
Definition: Translator.h:198
~Translator()
Singleton destructor must be private or protected.
Definition: Translator.h:188
Translator()
Singleton constructor must be private or protected.
Definition: Translator.h:185
std::map< std::string, std::vector< std::string > > m_textDomainMap
A vector from text domains to base directory for the message catalog.
Definition: Translator.h:199