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