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