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