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>
21 using lyx::support::package;
22 using lyx::support::getEnv;
23 using lyx::support::setEnv;
35 // This version of the Pimpl utilizes the message capability of
36 // libstdc++ that is distributed with GNU G++.
37 class Messages::Pimpl {
39 typedef std::messages<char>::catalog catalog;
41 Pimpl(string const & l)
43 loc_gl(lang_.c_str()),
44 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
46 //lyxerr << "Messages: language(" << l
47 // << ") in dir(" << dir << ")" << std::endl;
49 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
55 mssg_gl.close(cat_gl);
58 string const get(string const & msg) const
60 return mssg_gl.get(cat_gl, 0, 0, msg);
68 std::messages<char> const & mssg_gl;
79 # include <libintl.h> // use the header already in the system *EK*
81 # include "../intl/libintl.h"
84 // This is a more traditional variant.
85 class Messages::Pimpl {
87 Pimpl(string const & l)
91 lang_ = setlocale(LC_MESSAGES, NULL);
92 // strip off any encoding suffix, i.e., assume 8-bit po files
93 string::size_type i = lang_.find(".");
94 lang_ = lang_.substr(0, i);
95 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
96 << ": language(" << lang_ << ")" << std::endl;
101 string const get(string const & m) const
106 //string oldMSG = setlocale(LC_MESSAGES, NULL);
107 // In this order, see support/filetools.C:
108 string lang = getEnv("LC_ALL");
110 lang = getEnv("LC_MESSAGES");
112 lang = getEnv("LANG");
118 char const * works = setlocale(LC_MESSAGES, lang_.c_str());
120 lyxerr << "Locale " << lang_ << " could not be set" << std::endl;
121 // CTYPE controls what getmessage thinks what encoding the po file uses
122 string oldCTYPE = setlocale(LC_CTYPE, NULL);
123 setlocale(LC_CTYPE, lang_.c_str());
125 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
129 << BOOST_CURRENT_FUNCTION << '\n'
130 << "Error code: " << errno << '\n'
131 << "Lang, mess: " << lang_ << " " << m << '\n'
132 << "Directory : " << package().locale_dir() << '\n'
133 << "Rtn value : " << c << std::endl;
136 const char* msg = gettext(m.c_str());
137 string translated(works ? msg : m);
138 // Some english words have different translations, depending
139 // on context. In these cases the original string is
140 // augmented by context information (e.g.
141 // "To:[[as in 'From page x to page y']]" and
142 // "To:[[as in 'From format x to format y']]".
143 // This means that we need to filter out everything in
144 // double square brackets at the end of the string,
145 // otherwise the user sees bogus messages.
146 // If we are unable to honour the request we just
147 // return what we got in.
148 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
150 if (regex_match(translated, sub, reg))
151 translated = sub.str(1);
152 setlocale(LC_MESSAGES, lang.c_str());
153 setlocale(LC_CTYPE, oldCTYPE.c_str());
163 // This is the dummy variant.
164 class Messages::Pimpl {
166 Pimpl(string const &) {}
170 string const get(string const & m) const
179 : pimpl_(new Pimpl(""))
183 Messages::Messages(string const & l)
184 : pimpl_(new Pimpl(l))
188 // We need this for the sake of scoped_ptr
189 Messages::~Messages()
193 string const Messages::get(string const & msg) const
195 return pimpl_->get(msg);