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