]> git.lyx.org Git - features.git/blob - src/client/Messages.cpp
'using namespace std' instead of 'using std::xxx'
[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 "support/debug.h"
14 #include "support/filetools.h"
15 #include "support/Package.h"
16 #include "support/unicode.h"
17
18 #include <cerrno>
19 #include <ostream>
20
21 using namespace std;
22
23 namespace lyx {
24
25 using support::package;
26
27
28 #ifdef ENABLE_NLS
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                 string const locale_dir = package().locale_dir().toFilesystemEncoding();
49                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, 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                 string const locale_dir = package().locale_dir().toFilesystemEncoding();
113                 char const * c = bindtextdomain(PACKAGE, locale_dir.c_str());
114                 int e = errno;
115                 if (e) {
116                         LYXERR(Debug::DEBUG, "Messages::get()" << '\n'
117                                 << "Error code: " << errno << '\n'
118                                 << "Lang, mess: " << lang_ << " " << m << '\n'
119                                 << "Directory : " << package().locale_dir().absFilename() << '\n'
120                                 << "Rtn value : " << c);
121                 }
122
123                 if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
124                         LYXERR(Debug::DEBUG, "Messages::get()" << '\n'
125                                 << "Error code: " << errno << '\n'
126                                 << "Codeset   : " << ucs4_codeset << '\n');
127                 }
128
129                 textdomain(PACKAGE);
130
131                 char const * tmp = m.c_str();
132                 char const * msg = gettext(tmp);
133                 docstring translated;
134                 if (!msg) {
135                         lyxerr << "Undefined result from gettext" << endl;
136                         translated = from_ascii(tmp);
137                 } else if (msg == tmp) {
138                         //lyxerr << "Same as entered returned" << endl;
139                         translated = from_ascii(tmp);
140                 } else {
141                         LYXERR(Debug::DEBUG, "We got a translation");
142                         char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
143                         translated = ucs4;
144                 }
145                 setlocale(LC_ALL, old.c_str());
146                 return translated;
147         }
148 private:
149         ///
150         string lang_;
151 };
152 #endif
153
154 #else // ENABLE_NLS
155 // This is the dummy variant.
156 class Messages::Pimpl {
157 public:
158         Pimpl(string const &) {}
159
160         ~Pimpl() {}
161
162         docstring const get(string const & m) const
163         {
164                 return from_ascii(m);
165         }
166 };
167 #endif
168
169
170 Messages::Messages()
171         : pimpl_(new Pimpl(""))
172 {}
173
174
175 Messages::Messages(string const & l)
176         : pimpl_(new Pimpl(l))
177 {}
178
179
180 // We need this for the sake of scoped_ptr
181 Messages::~Messages()
182 {}
183
184
185 docstring const Messages::get(string const & msg) const
186 {
187         return pimpl_->get(msg);
188 }
189
190
191 } // namespace lyx