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)
93 lang_ = setlocale(LC_MESSAGES, NULL);
94 // strip off any encoding suffix, i.e., assume 8-bit po files
95 string::size_type i = lang_.find(".");
96 lang_ = lang_.substr(0, i);
97 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
98 << ": language(" << lang_ << ")" << std::endl;
103 string const get(string const & m) const
108 //string oldMSG = setlocale(LC_MESSAGES, NULL);
109 // In this order, see support/filetools.C:
110 string lang = getEnv("LC_ALL");
112 lang = getEnv("LC_MESSAGES");
114 lang = getEnv("LANG");
120 char const * works = setlocale(LC_MESSAGES, lang_.c_str());
122 lyxerr << "Locale " << lang_ << " could not be set" << std::endl;
123 // CTYPE controls what getmessage thinks what encoding the po file uses
124 string oldCTYPE = setlocale(LC_CTYPE, NULL);
125 setlocale(LC_CTYPE, lang_.c_str());
127 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
131 << BOOST_CURRENT_FUNCTION << '\n'
132 << "Error code: " << errno << '\n'
133 << "Lang, mess: " << lang_ << " " << m << '\n'
134 << "Directory : " << package().locale_dir() << '\n'
135 << "Rtn value : " << c << std::endl;
138 const char* msg = gettext(m.c_str());
139 string translated(works ? msg : m);
140 // Some english words have different translations, depending
141 // on context. In these cases the original string is
142 // augmented by context information (e.g.
143 // "To:[[as in 'From page x to page y']]" and
144 // "To:[[as in 'From format x to format y']]".
145 // This means that we need to filter out everything in
146 // double square brackets at the end of the string,
147 // otherwise the user sees bogus messages.
148 // If we are unable to honour the request we just
149 // return what we got in.
150 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
152 if (regex_match(translated, sub, reg))
153 translated = sub.str(1);
154 setlocale(LC_MESSAGES, lang.c_str());
155 setlocale(LC_CTYPE, oldCTYPE.c_str());
165 // This is the dummy variant.
166 class Messages::Pimpl {
168 Pimpl(string const &) {}
172 string const get(string const & m) const
181 : pimpl_(new Pimpl(""))
185 Messages::Messages(string const & l)
186 : pimpl_(new Pimpl(l))
190 // We need this for the sake of scoped_ptr
191 Messages::~Messages()
195 string const Messages::get(string const & msg) const
197 return pimpl_->get(msg);