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"
18 #include <boost/current_function.hpp>
19 #include <boost/regex.hpp>
23 using lyx::support::package;
24 using lyx::support::getEnv;
25 using lyx::support::setEnv;
37 // This version of the Pimpl utilizes the message capability of
38 // libstdc++ that is distributed with GNU G++.
39 class Messages::Pimpl {
41 typedef std::messages<char>::catalog catalog;
43 Pimpl(string const & l)
45 loc_gl(lang_.c_str()),
46 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
48 //lyxerr << "Messages: language(" << l
49 // << ") in dir(" << dir << ")" << std::endl;
51 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
57 mssg_gl.close(cat_gl);
60 string const get(string const & msg) const
62 return mssg_gl.get(cat_gl, 0, 0, msg);
70 std::messages<char> const & mssg_gl;
81 # include <libintl.h> // use the header already in the system *EK*
83 # include "../intl/libintl.h"
86 // This is a more traditional variant.
87 class Messages::Pimpl {
89 Pimpl(string const & l)
92 if ( lang_.empty() ) {
93 char const * lc_msgs = setlocale(LC_MESSAGES, NULL);
94 lang_ = lc_msgs ? lc_msgs : "";
96 // strip off any encoding suffix, i.e., assume 8-bit po files
97 string::size_type i = lang_.find(".");
98 lang_ = lang_.substr(0, i);
99 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
100 << ": language(" << lang_ << ")" << std::endl;
105 string const get(string const & m) const
110 // In this order, see support/filetools.C:
111 string lang = getEnv("LC_ALL");
113 lang = getEnv("LC_MESSAGES");
115 lang = getEnv("LANG");
121 char const * lc_msgs = setlocale(LC_MESSAGES, lang_.c_str());
122 // setlocale fails (returns NULL) if the corresponding locale
124 // On windows (mingw) it always returns NULL.
125 // Since this method gets called for every translatable
126 // buffer string like e.g. "Figure:" we warn only once.
128 static bool warned = false;
129 if (!warned && !lc_msgs) {
131 lyxerr << "Locale " << lang_ << " could not be set" << std::endl;
134 // CTYPE controls what getmessage thinks what encoding the po file uses
135 char const * lc_ctype = setlocale(LC_CTYPE, NULL);
136 string oldCTYPE = lc_ctype ? lc_ctype : "";
138 setlocale(LC_CTYPE, lang_.c_str());
140 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
144 << BOOST_CURRENT_FUNCTION << '\n'
145 << "Error code: " << errno << '\n'
146 << "Lang, mess: " << lang_ << " " << m << '\n'
147 << "Directory : " << package().locale_dir() << '\n'
148 << "Rtn value : " << c << std::endl;
151 const char* msg = gettext(m.c_str());
152 string translated(msg ? msg : m);
153 // Some english words have different translations, depending
154 // on context. In these cases the original string is
155 // augmented by context information (e.g.
156 // "To:[[as in 'From page x to page y']]" and
157 // "To:[[as in 'From format x to format y']]".
158 // This means that we need to filter out everything in
159 // double square brackets at the end of the string,
160 // otherwise the user sees bogus messages.
161 // If we are unable to honour the request we just
162 // return what we got in.
163 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
165 if (regex_match(translated, sub, reg))
166 translated = sub.str(1);
167 setlocale(LC_MESSAGES, lang.c_str());
168 setlocale(LC_CTYPE, oldCTYPE.c_str());
178 // This is the dummy variant.
179 class Messages::Pimpl {
181 Pimpl(string const &) {}
185 string const get(string const & m) const
194 : pimpl_(new Pimpl(""))
198 Messages::Messages(string const & l)
199 : pimpl_(new Pimpl(l))
203 // We need this for the sake of scoped_ptr
204 Messages::~Messages()
208 string const Messages::get(string const & msg) const
210 return pimpl_->get(msg);