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