]> git.lyx.org Git - features.git/blob - src/client/Messages.cpp
27117fd5195c6f6a4b719b9eb84a961bba175da8
[features.git] / src / client / 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 #include "debug.h"
14 #include "support/filetools.h"
15 #include "support/Package.h"
16 #include "support/unicode.h"
17
18 #include <boost/current_function.hpp>
19
20 #include <cerrno>
21
22
23 namespace lyx {
24
25 using lyx::support::package;
26 using std::endl;
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                 string const locale_dir = package().locale_dir().toFilesystemEncoding();
52                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, locale_dir.c_str());
53
54         }
55
56         ~Pimpl()
57         {
58                 mssg_gl.close(cat_gl);
59         }
60
61         string const get(string const & msg) const
62         {
63                 return mssg_gl.get(cat_gl, 0, 0, msg);
64         }
65 private:
66         ///
67         string lang_;
68         ///
69         std::locale loc_gl;
70         ///
71         std::messages<char> const & mssg_gl;
72         ///
73         catalog cat_gl;
74 };
75 #else
76
77 #ifdef HAVE_LOCALE_H
78 #  include <locale.h>
79 #endif
80
81 #  if HAVE_GETTEXT
82 #    include <libintl.h>      // use the header already in the system *EK*
83 #  else
84 #    include "../intl/libintl.h"
85 #  endif
86
87 // This is a more traditional variant.
88 class Messages::Pimpl {
89 public:
90         Pimpl(string const & l)
91                 : lang_(l)
92         {
93                 //lyxerr << "Messages: language(" << l
94                 //       << ") in dir(" << dir << ")" << std::endl;
95
96         }
97
98         ~Pimpl() {}
99
100         docstring const get(string const & m) const
101         {
102                 if (m.empty())
103                         return from_ascii(m);
104
105                 char * o = setlocale(LC_ALL, 0);
106                 string old;
107                 if (o)
108                         old = o;
109                 char * n = setlocale(LC_ALL, lang_.c_str());
110                 if (!n)
111                         // If we are unable to honour the request we just
112                         // return what we got in.
113                         return from_ascii(m);
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
137                 char const * tmp = m.c_str();
138                 char const * msg = gettext(tmp);
139                 docstring translated;
140                 if (!msg) {
141                         lyxerr << "Undefined result from gettext" << endl;
142                         translated = from_ascii(tmp);
143                 } else if (msg == tmp) {
144                         //lyxerr << "Same as entered returned" << endl;
145                         translated = from_ascii(tmp);
146                 } else {
147                         LYXERR(Debug::DEBUG) << "We got a translation" << endl;
148                         char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
149                         translated = ucs4;
150                 }
151                 setlocale(LC_ALL, old.c_str());
152                 return translated;
153         }
154 private:
155         ///
156         string lang_;
157 };
158 #endif
159
160 #else // ENABLE_NLS
161 // This is the dummy variant.
162 class Messages::Pimpl {
163 public:
164         Pimpl(string const &) {}
165
166         ~Pimpl() {}
167
168         docstring const get(string const & m) const
169         {
170                 return from_ascii(m);
171         }
172 };
173 #endif
174
175
176 Messages::Messages()
177         : pimpl_(new Pimpl(""))
178 {}
179
180
181 Messages::Messages(string const & l)
182         : pimpl_(new Pimpl(l))
183 {}
184
185
186 // We need this for the sake of scoped_ptr
187 Messages::~Messages()
188 {}
189
190
191 docstring const Messages::get(string const & msg) const
192 {
193         return pimpl_->get(msg);
194 }
195
196
197 } // namespace lyx