]> git.lyx.org Git - lyx.git/blob - src/client/Messages.cpp
cosmetics
[lyx.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 <cerrno>
19
20
21 namespace lyx {
22
23 using support::package;
24
25 using std::endl;
26 using std::string;
27
28
29 #ifdef ENABLE_NLS
30
31
32 #if 0
33
34 #include <locale>
35
36 // This version of the Pimpl utilizes the message capability of
37 // libstdc++ that is distributed with GNU G++.
38 class Messages::Pimpl {
39 public:
40         typedef std::messages<char>::catalog catalog;
41
42         Pimpl(string const & l)
43                 : lang_(l),
44                   loc_gl(lang_.c_str()),
45                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
46         {
47                 //lyxerr << "Messages: language(" << l
48                 //       << ") in dir(" << dir << ")" << std::endl;
49
50                 string const locale_dir = package().locale_dir().toFilesystemEncoding();
51                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, 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                 //lyxerr << "Messages: language(" << l
93                 //       << ") in dir(" << dir << ")" << std::endl;
94
95         }
96
97         ~Pimpl() {}
98
99         docstring const get(string const & m) const
100         {
101                 if (m.empty())
102                         return from_ascii(m);
103
104                 char * o = setlocale(LC_ALL, 0);
105                 string old;
106                 if (o)
107                         old = o;
108                 char * n = setlocale(LC_ALL, lang_.c_str());
109                 if (!n)
110                         // If we are unable to honour the request we just
111                         // return what we got in.
112                         return from_ascii(m);
113                 errno = 0;
114                 string const locale_dir = package().locale_dir().toFilesystemEncoding();
115                 char const * c = bindtextdomain(PACKAGE, locale_dir.c_str());
116                 int e = errno;
117                 if (e) {
118                         LYXERR(Debug::DEBUG, "Messages::get()" << '\n'
119                                 << "Error code: " << errno << '\n'
120                                 << "Lang, mess: " << lang_ << " " << m << '\n'
121                                 << "Directory : " << package().locale_dir().absFilename() << '\n'
122                                 << "Rtn value : " << c);
123                 }
124
125                 if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
126                         LYXERR(Debug::DEBUG, "Messages::get()" << '\n'
127                                 << "Error code: " << errno << '\n'
128                                 << "Codeset   : " << ucs4_codeset << '\n');
129                 }
130
131                 textdomain(PACKAGE);
132
133                 char const * tmp = m.c_str();
134                 char const * msg = gettext(tmp);
135                 docstring translated;
136                 if (!msg) {
137                         lyxerr << "Undefined result from gettext" << endl;
138                         translated = from_ascii(tmp);
139                 } else if (msg == tmp) {
140                         //lyxerr << "Same as entered returned" << endl;
141                         translated = from_ascii(tmp);
142                 } else {
143                         LYXERR(Debug::DEBUG, "We got a translation");
144                         char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
145                         translated = ucs4;
146                 }
147                 setlocale(LC_ALL, old.c_str());
148                 return translated;
149         }
150 private:
151         ///
152         string lang_;
153 };
154 #endif
155
156 #else // ENABLE_NLS
157 // This is the dummy variant.
158 class Messages::Pimpl {
159 public:
160         Pimpl(string const &) {}
161
162         ~Pimpl() {}
163
164         docstring const get(string const & m) const
165         {
166                 return from_ascii(m);
167         }
168 };
169 #endif
170
171
172 Messages::Messages()
173         : pimpl_(new Pimpl(""))
174 {}
175
176
177 Messages::Messages(string const & l)
178         : pimpl_(new Pimpl(l))
179 {}
180
181
182 // We need this for the sake of scoped_ptr
183 Messages::~Messages()
184 {}
185
186
187 docstring const Messages::get(string const & msg) const
188 {
189         return pimpl_->get(msg);
190 }
191
192
193 } // namespace lyx