]> git.lyx.org Git - lyx.git/blob - src/client/messages.C
compile fix
[lyx.git] / src / client / 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 #include "debug.h"
14 #include "support/filetools.h"
15 #include "support/package.h"
16
17 #include <cerrno>
18
19
20 namespace lyx {
21
22 using lyx::support::package;
23 using std::endl;
24 using std::string;
25
26
27 #ifdef ENABLE_NLS
28
29
30 #if 0
31
32 -#include <locale>
33
34 // This version of the Pimpl utilizes the message capability of
35 // libstdc++ that is distributed with GNU G++.
36 class Messages::Pimpl {
37 public:
38         typedef std::messages<char>::catalog catalog;
39
40         Pimpl(string const & l)
41                 : lang_(l),
42                   loc_gl(lang_.c_str()),
43                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
44         {
45                 //lyxerr << "Messages: language(" << l
46                 //       << ") in dir(" << dir << ")" << std::endl;
47
48                 cat_gl = mssg_gl.open(PACKAGE, loc_gl,
49                                       package().locale_dir().c_str());
50
51         }
52
53         ~Pimpl()
54         {
55                 mssg_gl.close(cat_gl);
56         }
57
58         string const get(string const & msg) const
59         {
60                 return mssg_gl.get(cat_gl, 0, 0, msg);
61         }
62 private:
63         ///
64         string lang_;
65         ///
66         std::locale loc_gl;
67         ///
68         std::messages<char> const & mssg_gl;
69         ///
70         catalog cat_gl;
71 };
72 #else
73
74 #ifdef HAVE_LOCALE_H
75 #  include <locale.h>
76 #endif
77
78 #  if HAVE_GETTEXT
79 #    include <libintl.h>      // use the header already in the system *EK*
80 #  else
81 #    include "../intl/libintl.h"
82 #  endif
83
84 // This is a more traditional variant.
85 class Messages::Pimpl {
86 public:
87         Pimpl(string const & l)
88                 : lang_(l)
89         {
90                 //lyxerr << "Messages: language(" << l
91                 //       << ") in dir(" << dir << ")" << std::endl;
92
93         }
94
95         ~Pimpl() {}
96
97         docstring const get(string const & m) const
98         {
99                 if (m.empty())
100                         return from_ascii(m);
101
102                 char * o = setlocale(LC_ALL, 0);
103                 string old;
104                 if (o)
105                         old = o;
106                 char * n = setlocale(LC_ALL, lang_.c_str());
107                 if (!n)
108                         // If we are unable to honour the request we just
109                         // return what we got in.
110                         return from_ascii(m);
111                 errno = 0;
112                 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
113                 int e = errno;
114                 if (e) {
115                         lyxerr[Debug::DEBUG]
116                                 << BOOST_CURRENT_FUNCTION << '\n'
117                                 << "Error code: " << errno << '\n'
118                                 << "Lang, mess: " << lang_ << " " << m << '\n'
119                                 << "Directory : " << package().locale_dir() << '\n'
120                                 << "Rtn value : " << c << endl;
121                 }
122
123 #ifdef WORDS_BIGENDIAN
124                 static const char * codeset = "UCS-4BE";
125 #else
126                 static const char * codeset = "UCS-4LE";
127 #endif
128                 if (!bind_textdomain_codeset(PACKAGE, codeset)) {
129                         lyxerr[Debug::DEBUG]
130                                 << BOOST_CURRENT_FUNCTION << '\n'
131                                 << "Error code: " << errno << '\n'
132                                 << "Codeset   : " << codeset << '\n'
133                                 << endl;
134                 }
135
136                 textdomain(PACKAGE);
137
138                 char const * tmp = m.c_str();
139                 char const * msg = gettext(tmp);
140                 docstring translated;
141                 if (!msg) {
142                         lyxerr << "Undefined result from gettext" << endl;
143                         translated = from_ascii(tmp);
144                 } else if (msg == tmp) {
145                         //lyxerr << "Same as entered returned" << endl;
146                         translated = from_ascii(tmp);
147                 } else {
148                         lyxerr[Debug::DEBUG] << "We got a translation" << endl;
149                         char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
150                         translated = ucs4;
151                 }
152                 setlocale(LC_ALL, old.c_str());
153                 return translated;
154         }
155 private:
156         ///
157         string lang_;
158 };
159 #endif
160
161 #else // ENABLE_NLS
162 // This is the dummy variant.
163 class Messages::Pimpl {
164 public:
165         Pimpl(string const &) {}
166
167         ~Pimpl() {}
168
169         docstring const get(string const & m) const
170         {
171                 return from_ascii(m);
172         }
173 };
174 #endif
175
176
177 Messages::Messages()
178         : pimpl_(new Pimpl(""))
179 {}
180
181
182 Messages::Messages(string const & l)
183         : pimpl_(new Pimpl(l))
184 {}
185
186
187 // We need this for the sake of scoped_ptr
188 Messages::~Messages()
189 {}
190
191
192 docstring const Messages::get(string const & msg) const
193 {
194         return pimpl_->get(msg);
195 }
196
197
198 } // namespace lyx