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