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