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