]> git.lyx.org Git - lyx.git/blob - src/Messages.cpp
* src/frontends/qt4/qlkey.h: add AltGr as modifier key (fix bug 4026)
[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 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.cpp:
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         string const locale_dir = package().locale_dir().toFilesystemEncoding();
116         char const * c = bindtextdomain(PACKAGE, locale_dir.c_str());
117         int e = errno;
118         if (e) {
119                 LYXERR(Debug::DEBUG)
120                 << BOOST_CURRENT_FUNCTION << '\n'
121                         << "Error code: " << errno << '\n'
122                         << "Lang, mess: " << lang_ << " " << m << '\n'
123                         << "Directory : " << package().locale_dir().absFilename() << '\n'
124                         << "Rtn value : " << c << endl;
125         }
126
127         if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
128                 LYXERR(Debug::DEBUG)
129                 << BOOST_CURRENT_FUNCTION << '\n'
130                         << "Error code: " << errno << '\n'
131                         << "Codeset   : " << ucs4_codeset << '\n'
132                         << endl;
133         }
134
135         textdomain(PACKAGE);
136         char const * tmp = m.c_str();
137         char const * msg = gettext(tmp);
138         docstring translated;
139         if (!msg || msg == tmp) {
140                 if (!msg)
141                         lyxerr << "Undefined result from gettext" << endl;
142                 //else
143                 //      lyxerr << "Same as entered returned" << endl;
144                 // Some english words have different translations,
145                 // depending on context. In these cases the original
146                 // string is augmented by context information (e.g.
147                 // "To:[[as in 'From page x to page y']]" and
148                 // "To:[[as in 'From format x to format y']]".
149                 // This means that we need to filter out everything
150                 // in double square brackets at the end of the
151                 // string, otherwise the user sees bogus messages.
152                 // If we are unable to honour the request we just
153                 // return what we got in.
154                 boost::smatch sub;
155                 if (regex_match(m, sub, reg))
156                         translated = from_ascii(sub.str(1));
157                 else
158                         translated = from_ascii(tmp);
159         } else {
160                 LYXERR(Debug::DEBUG) << "We got a translation" << endl;
161                 char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
162                 translated = ucs4;
163         }
164 #ifdef HAVE_LC_MESSAGES
165         setlocale(LC_MESSAGES, lang.c_str());
166 #endif
167         setlocale(LC_CTYPE, oldCTYPE.c_str());
168
169         std::pair<TranslationCache::iterator, bool> result =
170                 cache_.insert(std::make_pair(m, translated));
171
172         BOOST_ASSERT(result.second);
173
174         return result.first->second;
175 }
176
177 } // namespace lyx
178
179 #else // ENABLE_NLS
180 // This is the dummy variant.
181
182 using std::endl;
183 using std::make_pair;
184 using std::map;
185 using std::string;
186
187 namespace lyx {
188
189 Messages::Messages(string const & l) {}
190
191
192 docstring const Messages::get(string const & m) const
193 {
194         // See comment above
195         boost::smatch sub;
196         if (regex_match(m, sub, reg))
197                 return from_ascii(sub.str(1));
198         else
199                 return from_ascii(m);
200 }
201
202 } // namespace lyx
203
204 #endif
205
206 #if 0
207
208 -#include <locale>
209
210 namespace lyx {
211
212 // This version of the Pimpl utilizes the message capability of
213 // libstdc++ that is distributed with GNU G++.
214 class Messages::Pimpl {
215 public:
216         typedef std::messages<char>::catalog catalog;
217
218         Pimpl(string const & l)
219                 : lang_(l),
220                   loc_gl(lang_.c_str()),
221                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
222         {
223                 //lyxerr << "Messages: language(" << l
224                 //       << ") in dir(" << dir << ")" << endl;
225
226                 string const locale_dir = package().locale_dir().toFilesystemEncoding();
227                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, locale_dir.c_str());
228
229         }
230
231         ~Pimpl()
232         {
233                 mssg_gl.close(cat_gl);
234         }
235
236         docstring const get(string const & msg) const
237         {
238                 return mssg_gl.get(cat_gl, 0, 0, msg);
239         }
240 private:
241         ///
242         string lang_;
243         ///
244         std::locale loc_gl;
245         ///
246         std::messages<char> const & mssg_gl;
247         ///
248         catalog cat_gl;
249 };
250
251 } // namespace lyx
252
253 #endif