]> git.lyx.org Git - lyx.git/blob - src/client/Messages.cpp
reduce line noise
[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 <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, BOOST_CURRENT_FUNCTION << '\n'
120                                 << "Error code: " << errno << '\n'
121                                 << "Lang, mess: " << lang_ << " " << m << '\n'
122                                 << "Directory : " << package().locale_dir().absFilename() << '\n'
123                                 << "Rtn value : " << c);
124                 }
125
126                 if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
127                         LYXERR(Debug::DEBUG, BOOST_CURRENT_FUNCTION << '\n'
128                                 << "Error code: " << errno << '\n'
129                                 << "Codeset   : " << ucs4_codeset << '\n');
130                 }
131
132                 textdomain(PACKAGE);
133
134                 char const * tmp = m.c_str();
135                 char const * msg = gettext(tmp);
136                 docstring translated;
137                 if (!msg) {
138                         lyxerr << "Undefined result from gettext" << endl;
139                         translated = from_ascii(tmp);
140                 } else if (msg == tmp) {
141                         //lyxerr << "Same as entered returned" << endl;
142                         translated = from_ascii(tmp);
143                 } else {
144                         LYXERR(Debug::DEBUG, "We got a translation");
145                         char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
146                         translated = ucs4;
147                 }
148                 setlocale(LC_ALL, old.c_str());
149                 return translated;
150         }
151 private:
152         ///
153         string lang_;
154 };
155 #endif
156
157 #else // ENABLE_NLS
158 // This is the dummy variant.
159 class Messages::Pimpl {
160 public:
161         Pimpl(string const &) {}
162
163         ~Pimpl() {}
164
165         docstring const get(string const & m) const
166         {
167                 return from_ascii(m);
168         }
169 };
170 #endif
171
172
173 Messages::Messages()
174         : pimpl_(new Pimpl(""))
175 {}
176
177
178 Messages::Messages(string const & l)
179         : pimpl_(new Pimpl(l))
180 {}
181
182
183 // We need this for the sake of scoped_ptr
184 Messages::~Messages()
185 {}
186
187
188 docstring const Messages::get(string const & msg) const
189 {
190         return pimpl_->get(msg);
191 }
192
193
194 } // namespace lyx