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