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>
25 using lyx::support::package;
26 using lyx::support::getEnv;
27 using lyx::support::setEnv;
31 using lyx::from_ascii;
43 // This version of the Pimpl utilizes the message capability of
44 // libstdc++ that is distributed with GNU G++.
45 class Messages::Pimpl {
47 typedef std::messages<char>::catalog catalog;
49 Pimpl(string const & l)
51 loc_gl(lang_.c_str()),
52 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
54 //lyxerr << "Messages: language(" << l
55 // << ") in dir(" << dir << ")" << endl;
57 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
63 mssg_gl.close(cat_gl);
66 docstring const get(string const & msg) const
68 return mssg_gl.get(cat_gl, 0, 0, msg);
76 std::messages<char> const & mssg_gl;
87 # include <libintl.h> // use the header already in the system *EK*
89 # include "../intl/libintl.h"
92 // This is a more traditional variant.
93 class Messages::Pimpl {
95 Pimpl(string const & l)
98 if ( lang_.empty() ) {
99 char const * lc_msgs = 0;
100 #ifdef HAVE_LC_MESSAGES
101 lc_msgs = setlocale(LC_MESSAGES, NULL);
103 lang_ = lc_msgs ? lc_msgs : "";
105 // strip off any encoding suffix, i.e., assume 8-bit po files
106 string::size_type i = lang_.find(".");
107 lang_ = lang_.substr(0, i);
108 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
109 << ": language(" << lang_ << ")" << endl;
114 docstring const get(string const & m) const
117 return lyx::from_ascii(m);
119 // In this order, see support/filetools.C:
120 string lang = getEnv("LC_ALL");
122 lang = getEnv("LC_MESSAGES");
124 lang = getEnv("LANG");
129 #ifdef HAVE_LC_MESSAGES
130 char const * lc_msgs = setlocale(LC_MESSAGES, lang_.c_str());
132 // setlocale fails (returns NULL) if the corresponding locale
134 // On windows (mingw and cygwin) it always returns NULL.
135 // Since this method gets called for every translatable
136 // buffer string like e.g. "Figure:" we warn only once.
137 #if !defined(_WIN32) && !defined(__CYGWIN__)
138 static bool warned = false;
139 if (!warned && !lc_msgs) {
141 lyxerr << "Locale " << lang_ << " could not be set" << endl;
144 // CTYPE controls what getmessage thinks what encoding the po file uses
145 char const * lc_ctype = setlocale(LC_CTYPE, NULL);
146 string oldCTYPE = lc_ctype ? lc_ctype : "";
148 setlocale(LC_CTYPE, lang_.c_str());
150 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
154 << BOOST_CURRENT_FUNCTION << '\n'
155 << "Error code: " << errno << '\n'
156 << "Lang, mess: " << lang_ << " " << m << '\n'
157 << "Directory : " << package().locale_dir() << '\n'
158 << "Rtn value : " << c << endl;
160 #ifdef WORDS_BIGENDIAN
161 static const char * codeset = "UCS-4BE";
163 static const char * codeset = "UCS-4LE";
165 if (!bind_textdomain_codeset(PACKAGE, codeset)) {
167 << BOOST_CURRENT_FUNCTION << '\n'
168 << "Error code: " << errno << '\n'
169 << "Codeset : " << codeset << '\n'
175 const char* msg = gettext(m.c_str());
176 string translated(msg ? msg : m);
177 // Some english words have different translations, depending
178 // on context. In these cases the original string is
179 // augmented by context information (e.g.
180 // "To:[[as in 'From page x to page y']]" and
181 // "To:[[as in 'From format x to format y']]".
182 // This means that we need to filter out everything in
183 // double square brackets at the end of the string,
184 // otherwise the user sees bogus messages.
185 // If we are unable to honour the request we just
186 // return what we got in.
187 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
189 if (regex_match(translated, sub, reg))
190 translated = sub.str(1);
192 char const * tmp = m.c_str();
193 char const * msg = gettext(tmp);
194 docstring translated;
196 lyxerr << "Undefined result from gettext" << endl;
197 translated = from_ascii(tmp);
198 } else if (msg == tmp) {
199 lyxerr << "Same as entered returned" << endl;
200 translated = from_ascii(tmp);
202 lyxerr << "We got a translation" << endl;
203 lyx::char_type const * ucs4 = reinterpret_cast<lyx::char_type const *>(msg);
207 #ifdef HAVE_LC_MESSAGES
208 setlocale(LC_MESSAGES, lang.c_str());
210 setlocale(LC_CTYPE, oldCTYPE.c_str());
220 // This is the dummy variant.
221 class Messages::Pimpl {
223 Pimpl(string const &) {}
227 string const get(string const & m) const
236 : pimpl_(new Pimpl(""))
240 Messages::Messages(string const & l)
241 : pimpl_(new Pimpl(l))
245 // We need this for the sake of scoped_ptr
246 Messages::~Messages()
250 docstring const Messages::get(string const & msg) const
252 return pimpl_->get(msg);