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