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