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