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/regex.hpp>
20 using lyx::support::package;
21 using lyx::support::getEnv;
22 using lyx::support::setEnv;
34 // This version of the Pimpl utilizes the message capability of
35 // libstdc++ that is distributed with GNU G++.
36 class Messages::Pimpl {
38 typedef std::messages<char>::catalog catalog;
40 Pimpl(string const & l)
42 loc_gl(lang_.c_str()),
43 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
45 //lyxerr << "Messages: language(" << l
46 // << ") in dir(" << dir << ")" << std::endl;
48 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
54 mssg_gl.close(cat_gl);
57 string const get(string const & msg) const
59 return mssg_gl.get(cat_gl, 0, 0, msg);
67 std::messages<char> const & mssg_gl;
78 # include <libintl.h> // use the header already in the system *EK*
80 # include "../intl/libintl.h"
83 // This is a more traditional variant.
84 class Messages::Pimpl {
86 Pimpl(string const & l)
90 lang_ = setlocale(LC_MESSAGES, NULL);
91 // strip off any encoding suffix, i.e., assume 8-bit po files
92 string::size_type i = lang_.find(".");
93 lang_ = lang_.substr(0, i);
94 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
95 << ": language(" << lang_ << ")" << std::endl;
100 string const get(string const & m) const
105 //string oldMSG = setlocale(LC_MESSAGES, NULL);
106 // In this order, see support/filetools.C:
107 string lang = getEnv("LC_ALL");
109 lang = getEnv("LC_MESSAGES");
111 lang = getEnv("LANG");
117 char const * works = setlocale(LC_MESSAGES, lang_.c_str());
118 // CTYPE controls what getmessage thinks what encoding the po file uses
119 string oldCTYPE = setlocale(LC_CTYPE, NULL);
120 setlocale(LC_CTYPE, lang_.c_str());
122 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
126 << BOOST_CURRENT_FUNCTION << '\n'
127 << "Error code: " << errno << '\n'
128 << "Lang, mess: " << lang_ << " " << m << '\n'
129 << "Directory : " << package().locale_dir() << '\n'
130 << "Rtn value : " << c << std::endl;
133 const char* msg = gettext(m.c_str());
134 string translated(works ? msg : m);
135 // Some english words have different translations, depending
136 // on context. In these cases the original string is
137 // augmented by context information (e.g.
138 // "To:[[as in 'From page x to page y']]" and
139 // "To:[[as in 'From format x to format y']]".
140 // This means that we need to filter out everything in
141 // double square brackets at the end of the string,
142 // otherwise the user sees bogus messages.
143 // If we are unable to honour the request we just
144 // return what we got in.
145 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
147 if (regex_match(translated, sub, reg))
148 translated = sub.str(1);
149 setlocale(LC_MESSAGES, lang.c_str());
150 setlocale(LC_CTYPE, oldCTYPE.c_str());
160 // This is the dummy variant.
161 class Messages::Pimpl {
163 Pimpl(string const &) {}
167 string const get(string const & m) const
176 : pimpl_(new Pimpl(""))
180 Messages::Messages(string const & l)
181 : pimpl_(new Pimpl(l))
185 // We need this for the sake of scoped_ptr
186 Messages::~Messages()
190 string const Messages::get(string const & msg) const
192 return pimpl_->get(msg);