]> git.lyx.org Git - lyx.git/blob - src/Messages.cpp
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / Messages.cpp
1 /* \file Messages.cpp
2  * This file is part of LyX, the document processor.
3  * Licence details can be found in the file COPYING.
4  *
5  * \author Lars Gullik Bjønnes
6  *
7  * Full author contact details are available in file CREDITS.
8  */
9
10 #include <config.h>
11
12 #include "Messages.h"
13
14 #include "debug.h"
15
16 #include "support/docstring.h"
17 #include "support/environment.h"
18 #include "support/filetools.h"
19 #include "support/Package.h"
20 #include "support/unicode.h"
21
22 #include <boost/current_function.hpp>
23 #include <boost/regex.hpp>
24
25 #include <cerrno>
26
27 namespace {
28 boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
29 };
30
31 #ifdef ENABLE_NLS
32
33 #ifdef HAVE_LOCALE_H
34 #  include <locale.h>
35 #endif
36
37 #  if HAVE_GETTEXT
38 #    include <libintl.h>      // use the header already in the system *EK*
39 #  else
40 #    include "../intl/libintl.h"
41 #  endif
42
43 using std::endl;
44 using std::make_pair;
45 using std::map;
46 using std::string;
47
48 namespace lyx {
49
50 using support::package;
51 using support::getEnv;
52 using support::setEnv;
53
54
55 // This version use the traditional gettext.
56 Messages::Messages(string const & l)
57         : lang_(l), warned_(false)
58 {
59         // strip off any encoding suffix, i.e., assume 8-bit po files
60         string::size_type i = lang_.find(".");
61         lang_ = lang_.substr(0, i);
62         LYXERR(Debug::DEBUG) << BOOST_CURRENT_FUNCTION
63                 << ": language(" << lang_ << ")" << endl;
64 }
65
66
67 void Messages::init()
68 {
69         errno = 0;
70         string const locale_dir = package().locale_dir().toFilesystemEncoding();
71         char const * c = bindtextdomain(PACKAGE, locale_dir.c_str());
72         int e = errno;
73         if (e) {
74                 LYXERR(Debug::DEBUG)
75                         << BOOST_CURRENT_FUNCTION << '\n'
76                         << "Error code: " << errno << '\n'
77                         << "Directory : " << package().locale_dir().absFilename() << '\n'
78                         << "Rtn value : " << c << endl;
79         }
80
81         if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
82                 LYXERR(Debug::DEBUG)
83                         << BOOST_CURRENT_FUNCTION << '\n'
84                         << "Error code: " << errno << '\n'
85                         << "Codeset   : " << ucs4_codeset << '\n'
86                         << endl;
87         }
88
89         textdomain(PACKAGE);
90 }
91
92
93 docstring const Messages::get(string const & m) const
94 {
95         if (m.empty())
96                 return docstring();
97
98         // Look for the translated string in the cache.
99         TranslationCache::iterator it = cache_.find(m);
100         if (it != cache_.end())
101                 return it->second;
102
103         // The string was not found, use gettext to generate it
104
105         string const oldLANGUAGE = getEnv("LANGUAGE");
106         string const oldLC_ALL = getEnv("LC_ALL");
107         if (!lang_.empty()) {
108                 // This GNU extension overrides any language locale
109                 // wrt gettext.
110                 setEnv("LANGUAGE", lang_);
111                 // However, setting LANGUAGE does nothing when the
112                 // locale is "C". Therefore we set the locale to
113                 // something that is believed to exist on most
114                 // systems. The idea is that one should be able to
115                 // load German documents even without having de_DE
116                 // installed.
117                 setEnv("LC_ALL", "en_US");
118 #ifdef HAVE_LC_MESSAGES
119                 setlocale(LC_MESSAGES, "");
120 #endif
121         }
122
123         char const * tmp = m.c_str();
124         char const * msg = gettext(tmp);
125         docstring translated;
126         if (!msg || msg == tmp) {
127                 if (!msg)
128                         lyxerr << "Undefined result from gettext" << endl;
129                 //else
130                 //      lyxerr << "Same as entered returned" << endl;
131                 // Some english words have different translations,
132                 // depending on context. In these cases the original
133                 // string is augmented by context information (e.g.
134                 // "To:[[as in 'From page x to page y']]" and
135                 // "To:[[as in 'From format x to format y']]".
136                 // This means that we need to filter out everything
137                 // in double square brackets at the end of the
138                 // string, otherwise the user sees bogus messages.
139                 // If we are unable to honour the request we just
140                 // return what we got in.
141                 boost::smatch sub;
142                 if (regex_match(m, sub, reg))
143                         translated = from_ascii(sub.str(1));
144                 else
145                         translated = from_ascii(tmp);
146         } else {
147                 LYXERR(Debug::DEBUG) << "We got a translation" << endl;
148                 // m is actually not a char const * but ucs4 data
149                 translated = reinterpret_cast<char_type const *>(msg);
150         }
151
152         if (!lang_.empty()) {
153                 // Reset everything as it was.
154                 setEnv("LANGUAGE", oldLANGUAGE);
155                 setEnv("LC_ALL", oldLC_ALL);
156 #ifdef HAVE_LC_MESSAGES
157                 setlocale(LC_MESSAGES, "");
158 #endif
159         }
160
161         std::pair<TranslationCache::iterator, bool> result =
162                 cache_.insert(std::make_pair(m, translated));
163
164         BOOST_ASSERT(result.second);
165
166         return result.first->second;
167 }
168
169 } // namespace lyx
170
171 #else // ENABLE_NLS
172 // This is the dummy variant.
173
174 using std::endl;
175 using std::make_pair;
176 using std::map;
177 using std::string;
178
179 namespace lyx {
180
181 Messages::Messages(string const & l) {}
182
183 void Messages::init()
184 {
185 }
186
187
188 docstring const Messages::get(string const & m) const
189 {
190         // See comment above
191         boost::smatch sub;
192         if (regex_match(m, sub, reg))
193                 return from_ascii(sub.str(1));
194         else
195                 return from_ascii(m);
196 }
197
198 } // namespace lyx
199
200 #endif
201
202 #if 0
203
204 -#include <locale>
205
206 namespace lyx {
207
208 // This version of the Pimpl utilizes the message capability of
209 // libstdc++ that is distributed with GNU G++.
210 class Messages::Pimpl {
211 public:
212         typedef std::messages<char>::catalog catalog;
213
214         Pimpl(string const & l)
215                 : lang_(l),
216                   loc_gl(lang_.c_str()),
217                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
218         {
219                 //lyxerr << "Messages: language(" << l
220                 //       << ") in dir(" << dir << ")" << endl;
221
222                 string const locale_dir = package().locale_dir().toFilesystemEncoding();
223                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, locale_dir.c_str());
224
225         }
226
227         ~Pimpl()
228         {
229                 mssg_gl.close(cat_gl);
230         }
231
232         docstring const get(string const & msg) const
233         {
234                 return mssg_gl.get(cat_gl, 0, 0, msg);
235         }
236 private:
237         ///
238         string lang_;
239         ///
240         std::locale loc_gl;
241         ///
242         std::messages<char> const & mssg_gl;
243         ///
244         catalog cat_gl;
245 };
246
247 } // namespace lyx
248
249 #endif