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