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/core/translator/Translator.cpp
22 
23  \brief This class is designed for dealing with multi-language text translation in TerraLib.
24 
25  \author Matheus Cavassan Zaglia
26  \author Gilberto Ribeiro de Queiroz
27  */
28 
29 // Boost
30 #include <boost/locale.hpp>
31 #include <boost/format.hpp>
32 
33 // TerraLib
34 #include "Translator.h"
35 #include "../Exception.h"
36 #include "../utils/Platform.h"
37 
39 {
40  static Translator instance;
41 
42  return instance;
43 }
44 
45 std::string te::core::Translator::translate(const std::string& message)
46 {
47  return translate(message.c_str());
48 }
49 
50 std::string te::core::Translator::translate(const char* message)
51 {
52 
53 #ifdef TERRALIB_TRANSLATOR_ENABLED
54  for(const auto& dir : m_textDomainMap)
55  {
56  for(const auto& domain : dir.second)
57  {
58 
59  boost::locale::generator gen;
60  gen.add_messages_domain(domain);
61  gen.add_messages_path(dir.first);
62 
63  std::locale::global(gen(m_locale));
64 
65  std::string translated = boost::locale::translate(message).str(std::locale());
66 
67  if(translated != message)
68  return translated;
69  }
70  }
71  return message;
72 #else
73  return message;
74 #endif
75 }
76 
77 std::string
78 te::core::Translator::translate(const std::string& msg1,
79  const std::string& msg2,
80  unsigned int n)
81 {
82  return translate(msg1.c_str(), msg2.c_str(), n);
83 }
84 
85 std::string
87  const char* msg2,
88  unsigned int n)
89 {
90 #ifdef TERRALIB_TRANSLATOR_ENABLED
91  for(const auto& dir : m_textDomainMap)
92  {
93  for(const auto& domain : dir.second)
94  {
95  boost::locale::generator gen;
96  gen.add_messages_domain(domain);
97  gen.add_messages_path(dir.first);
98 
99  std::locale::global(gen(m_locale));
100 
101  std::string translated = boost::locale::translate(msg1, msg2, n).str(std::locale());
102 
103  if(n == 1 && translated != msg1)
104  return translated;
105  else if(n > 1 && translated != msg2)
106  return translated;
107  }
108  }
109  return ((n == 1) ? msg1 : msg2);
110 #else
111  return ((n == 1) ? msg1 : msg2);
112 #endif
113 }
114 
115 void te::core::Translator::setLocale(const std::string& locale)
116 {
117  m_locale = locale + ".UTF-8";
118 }
119 
120 void te::core::Translator::addTextDomain(const std::string& textDomain, const std::string& dir)
121 {
122  if(exist(textDomain))
123  {
124  boost::format err_msg(TE_TR("The text domain %1% already exist."));
125  throw Exception() << te::ErrorDescription((err_msg % textDomain).str());
126  }
127  m_textDomainMap[dir].push_back(textDomain);
128 }
129 
130 
131 bool te::core::Translator::exist(const std::string& textDomain)
132 {
133  for(const auto& dir : m_textDomainMap)
134  {
135  auto domain = std::find(dir.second.begin(), dir.second.end(), textDomain);
136  if(domain != dir.second.end())
137  return true;
138  }
139  return false;
140 }
141 
void setLocale(const std::string &locale)
It sets the locale for the Translator.
Definition: Translator.cpp:115
static Translator & instance()
Definition: Translator.cpp:38
This singleton is designed to deal with multi-language text translation in TerraLib.
Definition: Translator.h:85
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
std::string translate(const std::string &message)
It tries to translate the specified text string.
Definition: Translator.cpp:45
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
void addTextDomain(const std::string &textDomain, const std::string &dir)
It adds a new text domain (text catalog).
Definition: Translator.cpp:120
std::string m_locale
If not empty, it is the current locale.
Definition: Translator.h:198
This class is designed for dealing with multi-language text translation in TerraLib.
Base exception class for TerraLib Core Runtime Library.
bool exist(const std::string &textDomain)
It returns true if the text domain (text catalog) exists and false otherwise.
Definition: Translator.cpp:131
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