2 * This file is part of LyX, the document processor.
3 * Licence details can be found in the file COPYING.
5 * \author Lars Gullik Bjønnes
7 * Full author contact details are available in file CREDITS.
14 #include "support/filetools.h"
15 #include "support/environment.h"
16 #include "support/package.h"
17 #include "support/docstring.h"
18 #include "support/types.h"
20 #include <boost/current_function.hpp>
21 #include <boost/regex.hpp>
28 using support::package;
29 using support::getEnv;
30 using support::setEnv;
42 // This version of the Pimpl utilizes the message capability of
43 // libstdc++ that is distributed with GNU G++.
44 class Messages::Pimpl {
46 typedef std::messages<char>::catalog catalog;
48 Pimpl(string const & l)
50 loc_gl(lang_.c_str()),
51 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
53 //lyxerr << "Messages: language(" << l
54 // << ") in dir(" << dir << ")" << endl;
56 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
62 mssg_gl.close(cat_gl);
65 docstring const get(string const & msg) const
67 return mssg_gl.get(cat_gl, 0, 0, msg);
75 std::messages<char> const & mssg_gl;
86 # include <libintl.h> // use the header already in the system *EK*
88 # include "../intl/libintl.h"
91 // This is a more traditional variant.
92 class Messages::Pimpl {
94 Pimpl(string const & l)
97 if ( lang_.empty() ) {
98 char const * lc_msgs = 0;
99 #ifdef HAVE_LC_MESSAGES
100 lc_msgs = setlocale(LC_MESSAGES, NULL);
102 lang_ = lc_msgs ? lc_msgs : "";
104 // strip off any encoding suffix, i.e., assume 8-bit po files
105 string::size_type i = lang_.find(".");
106 lang_ = lang_.substr(0, i);
107 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
108 << ": language(" << lang_ << ")" << endl;
113 docstring const get(string const & m) const
116 return from_ascii(m);
118 // In this order, see support/filetools.C:
119 string lang = getEnv("LC_ALL");
121 lang = getEnv("LC_MESSAGES");
123 lang = getEnv("LANG");
128 #ifdef HAVE_LC_MESSAGES
129 char const * lc_msgs = setlocale(LC_MESSAGES, lang_.c_str());
131 // setlocale fails (returns NULL) if the corresponding locale
133 // On windows (mingw and cygwin) it always returns NULL.
134 // Since this method gets called for every translatable
135 // buffer string like e.g. "Figure:" we warn only once.
136 #if !defined(_WIN32) && !defined(__CYGWIN__)
137 static bool warned = false;
138 if (!warned && !lc_msgs) {
140 lyxerr << "Locale " << lang_ << " could not be set" << endl;
143 // CTYPE controls what getmessage thinks what encoding the po file uses
144 char const * lc_ctype = setlocale(LC_CTYPE, NULL);
145 string oldCTYPE = lc_ctype ? lc_ctype : "";
147 setlocale(LC_CTYPE, lang_.c_str());
149 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
153 << BOOST_CURRENT_FUNCTION << '\n'
154 << "Error code: " << errno << '\n'
155 << "Lang, mess: " << lang_ << " " << m << '\n'
156 << "Directory : " << package().locale_dir() << '\n'
157 << "Rtn value : " << c << endl;
159 #ifdef WORDS_BIGENDIAN
160 static const char * codeset = "UCS-4BE";
162 static const char * codeset = "UCS-4LE";
164 if (!bind_textdomain_codeset(PACKAGE, codeset)) {
166 << BOOST_CURRENT_FUNCTION << '\n'
167 << "Error code: " << errno << '\n'
168 << "Codeset : " << codeset << '\n'
174 const char* msg = gettext(m.c_str());
175 string translated(msg ? msg : m);
176 // Some english words have different translations, depending
177 // on context. In these cases the original string is
178 // augmented by context information (e.g.
179 // "To:[[as in 'From page x to page y']]" and
180 // "To:[[as in 'From format x to format y']]".
181 // This means that we need to filter out everything in
182 // double square brackets at the end of the string,
183 // otherwise the user sees bogus messages.
184 // If we are unable to honour the request we just
185 // return what we got in.
186 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
188 if (regex_match(translated, sub, reg))
189 translated = sub.str(1);
191 char const * tmp = m.c_str();
192 char const * msg = gettext(tmp);
193 docstring translated;
195 lyxerr << "Undefined result from gettext" << endl;
196 translated = from_ascii(tmp);
197 } else if (msg == tmp) {
198 //lyxerr << "Same as entered returned" << endl;
199 translated = from_ascii(tmp);
201 lyxerr[Debug::DEBUG] << "We got a translation" << endl;
202 char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
206 #ifdef HAVE_LC_MESSAGES
207 setlocale(LC_MESSAGES, lang.c_str());
209 setlocale(LC_CTYPE, oldCTYPE.c_str());
219 // This is the dummy variant.
220 class Messages::Pimpl {
222 Pimpl(string const &) {}
226 docstring const get(string const & m) const
228 return from_ascii(m);
235 : pimpl_(new Pimpl(""))
239 Messages::Messages(string const & l)
240 : pimpl_(new Pimpl(l))
244 // We need this for the sake of scoped_ptr
245 Messages::~Messages()
249 docstring const Messages::get(string const & msg) const
251 return pimpl_->get(msg);