]> git.lyx.org Git - lyx.git/blob - src/messages.C
* Painter.h:
[lyx.git] / src / messages.C
1 /* \file messages.C
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 "debug.h"
13 #include "messages.h"
14 #include "support/filetools.h"
15 #include "support/environment.h"
16 #include "support/package.h"
17 #include "support/docstring.h"
18 #include "support/types.h"
19
20 #include <boost/current_function.hpp>
21 #include <boost/regex.hpp>
22
23 #include <cerrno>
24
25
26 namespace lyx {
27
28 using support::package;
29 using support::getEnv;
30 using support::setEnv;
31
32 using std::string;
33 using std::endl;
34
35 #ifdef ENABLE_NLS
36
37
38 #if 0
39
40 -#include <locale>
41
42 // This version of the Pimpl utilizes the message capability of
43 // libstdc++ that is distributed with GNU G++.
44 class Messages::Pimpl {
45 public:
46         typedef std::messages<char>::catalog catalog;
47
48         Pimpl(string const & l)
49                 : lang_(l),
50                   loc_gl(lang_.c_str()),
51                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
52         {
53                 //lyxerr << "Messages: language(" << l
54                 //       << ") in dir(" << dir << ")" << endl;
55
56                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
57
58         }
59
60         ~Pimpl()
61         {
62                 mssg_gl.close(cat_gl);
63         }
64
65         docstring const get(string const & msg) const
66         {
67                 return mssg_gl.get(cat_gl, 0, 0, msg);
68         }
69 private:
70         ///
71         string lang_;
72         ///
73         std::locale loc_gl;
74         ///
75         std::messages<char> const & mssg_gl;
76         ///
77         catalog cat_gl;
78 };
79 #else
80
81 #ifdef HAVE_LOCALE_H
82 #  include <locale.h>
83 #endif
84
85 #  if HAVE_GETTEXT
86 #    include <libintl.h>      // use the header already in the system *EK*
87 #  else
88 #    include "../intl/libintl.h"
89 #  endif
90
91 // This is a more traditional variant.
92 class Messages::Pimpl {
93 public:
94         Pimpl(string const & l)
95                 : lang_(l)
96         {
97                 if ( lang_.empty() ) {
98                         char const * lc_msgs = 0;
99 #ifdef HAVE_LC_MESSAGES
100                         lc_msgs = setlocale(LC_MESSAGES, NULL);
101 #endif
102                         lang_ = lc_msgs ? lc_msgs : "";
103                 }
104                 // strip off any encoding suffix, i.e., assume 8-bit po files
105                 string::size_type i = lang_.find(".");
106                 lang_ = lang_.substr(0, i);
107                 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
108                                      << ": language(" << lang_ << ")" << endl;
109         }
110
111         ~Pimpl() {}
112
113         docstring const get(string const & m) const
114         {
115                 if (m.empty())
116                         return from_ascii(m);
117
118                 // In this order, see support/filetools.C:
119                 string lang = getEnv("LC_ALL");
120                 if (lang.empty()) {
121                         lang = getEnv("LC_MESSAGES");
122                         if (lang.empty()) {
123                                 lang = getEnv("LANG");
124                                 if (lang.empty())
125                                         lang = "C";
126                         }
127                 }
128 #ifdef HAVE_LC_MESSAGES
129                 char const * lc_msgs = setlocale(LC_MESSAGES, lang_.c_str());
130 #endif
131                 // setlocale fails (returns NULL) if the corresponding locale
132                 // is not installed.
133                 // On windows (mingw and cygwin) it always returns NULL.
134                 // Since this method gets called for every translatable
135                 // buffer string like e.g. "Figure:" we warn only once.
136 #if !defined(_WIN32) && !defined(__CYGWIN__)
137                 static bool warned = false;
138                 if (!warned && !lc_msgs) {
139                         warned = true;
140                         lyxerr << "Locale " << lang_ << " could not be set" << endl;
141                 }
142 #endif
143                 // CTYPE controls what getmessage thinks what encoding the po file uses
144                 char const * lc_ctype = setlocale(LC_CTYPE, NULL);
145                 string oldCTYPE = lc_ctype ? lc_ctype : "";
146
147                 setlocale(LC_CTYPE, lang_.c_str());
148                 errno = 0;
149                 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
150                 int e = errno;
151                 if (e) {
152                         lyxerr[Debug::DEBUG]
153                                 << BOOST_CURRENT_FUNCTION << '\n'
154                                 << "Error code: " << errno << '\n'
155                                 << "Lang, mess: " << lang_ << " " << m << '\n'
156                                 << "Directory : " << package().locale_dir() << '\n'
157                                 << "Rtn value : " << c << endl;
158                 }
159 #ifdef WORDS_BIGENDIAN
160                 static const char * codeset = "UCS-4BE";
161 #else
162                 static const char * codeset = "UCS-4LE";
163 #endif
164                 if (!bind_textdomain_codeset(PACKAGE, codeset)) {
165                         lyxerr[Debug::DEBUG]
166                                 << BOOST_CURRENT_FUNCTION << '\n'
167                                 << "Error code: " << errno << '\n'
168                                 << "Codeset   : " << codeset << '\n'
169                                 << endl;
170                 }
171
172                 textdomain(PACKAGE);
173 #if 0
174                 const char* msg = gettext(m.c_str());
175                 string translated(msg ? msg : m);
176                 // Some english words have different translations, depending
177                 // on context. In these cases the original string is
178                 // augmented by context information (e.g.
179                 // "To:[[as in 'From page x to page y']]" and
180                 // "To:[[as in 'From format x to format y']]".
181                 // This means that we need to filter out everything in
182                 // double square brackets at the end of the string,
183                 // otherwise the user sees bogus messages.
184                 // If we are unable to honour the request we just
185                 // return what we got in.
186                 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
187                 boost::smatch sub;
188                 if (regex_match(translated, sub, reg))
189                         translated = sub.str(1);
190 #else
191                 char const * tmp = m.c_str();
192                 char const * msg = gettext(tmp);
193                 docstring translated;
194                 if (!msg) {
195                         lyxerr << "Undefined result from gettext" << endl;
196                         translated = from_ascii(tmp);
197                 } else if (msg == tmp) {
198                         //lyxerr << "Same as entered returned" << endl;
199                         translated = from_ascii(tmp);
200                 } else {
201                         lyxerr << "We got a translation" << endl;
202                         char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
203                         translated = ucs4;
204                 }
205 #endif
206 #ifdef HAVE_LC_MESSAGES
207                 setlocale(LC_MESSAGES, lang.c_str());
208 #endif
209                 setlocale(LC_CTYPE, oldCTYPE.c_str());
210                 return translated;
211         }
212 private:
213         ///
214         string lang_;
215 };
216 #endif
217
218 #else // ENABLE_NLS
219 // This is the dummy variant.
220 class Messages::Pimpl {
221 public:
222         Pimpl(string const &) {}
223
224         ~Pimpl() {}
225
226         docstring const get(string const & m) const
227         {
228                 return from_ascii(m);
229         }
230 };
231 #endif
232
233
234 Messages::Messages()
235         : pimpl_(new Pimpl(""))
236 {}
237
238
239 Messages::Messages(string const & l)
240         : pimpl_(new Pimpl(l))
241 {}
242
243
244 // We need this for the sake of scoped_ptr
245 Messages::~Messages()
246 {}
247
248
249 docstring const Messages::get(string const & msg) const
250 {
251         return pimpl_->get(msg);
252 }
253
254
255 } // namespace lyx