All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Translator.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/Translator.cpp
22 
23  \brief This class is designed for dealing with multi-language text translation in TerraLib.
24  */
25 
26 // TerraLib
27 #include "Translator.h"
28 
29 #ifdef TERRALIB_TRANSLATOR_ENABLED
30 
31 // TerraLib
32 #include "Exception.h"
33 
34 // GNU Text Utilities
35 #ifdef TERRALIB_GNU_GETTEXT_ENABLED
36 #include <libintl.h>
37 #endif
38 
39 const char* te::common::Translator::translate(const std::string& message, const std::string& textDomain)
40 {
41  return translate(message.c_str(), textDomain.c_str());
42 }
43 
44 const char* te::common::Translator::translate(const char* message, const char* textDomain)
45 {
46 #ifdef TERRALIB_GNU_GETTEXT_ENABLED
47  return dgettext(textDomain, message);
48 #else
49  return message;
50 #endif
51 }
52 
53 const char* te::common::Translator::translate(const std::string& textDomain,
54  const std::string& msg1,
55  const std::string& msg2,
56  unsigned int n)
57 {
58  return translate(textDomain.c_str(), msg1.c_str(), msg2.c_str(), n);
59 }
60 
61 const char* te::common::Translator::translate(const char* textDomain,
62  const char* msg1,
63  const char* msg2,
64  unsigned int n)
65 {
66 #ifdef TERRALIB_GNU_GETTEXT_ENABLED
67  return dngettext(textDomain, msg1, msg2, n);
68 #else
69  return ((n == 1) ? msg1 : msg2);
70 #endif
71 }
72 
73 const char* te::common::Translator::addTextDomain(const std::string& textDomain, const std::string& textDomainDir)
74 {
75  if(exist(textDomain))
76  throw Exception(TE_TR("The text domain already exist."));
77 
78  m_textDomainMap[textDomain] = textDomainDir;
79 
80 #ifdef TERRALIB_GNU_GETTEXT_ENABLED
81  return bindtextdomain(textDomain.c_str(), textDomainDir.c_str());
82 #else
83  return "";
84 #endif
85 }
86 
87 const char* te::common::Translator::setTextDomainCodeSet(const std::string& textDomain, const std::string& codeset)
88 {
89  if(exist(textDomain) == false)
90  throw Exception(TE_TR("The text domain doesn't exist."));
91 
92 #ifdef TERRALIB_GNU_GETTEXT_ENABLED
93  return bind_textdomain_codeset(textDomain.c_str(), codeset.c_str());
94 #else
95  return "";
96 #endif
97 }
98 
99 bool te::common::Translator::exist(const std::string& textDomain)
100 {
101  std::map<std::string, std::string>::const_iterator it = m_textDomainMap.find(textDomain);
102 
103  if(it != m_textDomainMap.end())
104  return true;
105  else
106  return false;
107 }
108 
109 std::string te::common::Translator::getTextDomainDir(const std::string& textDomain)
110 {
111  std::map<std::string, std::string>::const_iterator it = m_textDomainMap.find(textDomain);
112 
113  if(it != m_textDomainMap.end())
114  return it->second;
115  else
116  return std::string("");
117 }
118 
119 te::common::Translator::Translator()
120 {
121 }
122 
123 te::common::Translator::~Translator()
124 {
125 }
126 
127 #endif // TERRALIB_TRANSLATOR_ENABLED
128 
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
This class is designed for dealing with multi-language text translation in TerraLib.