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 << "Messages: language(" << lang_ << ")" << std::endl;
99 string const get(string const & m) const
104 //string oldMSG = setlocale(LC_MESSAGES, NULL);
105 // In this order, see support/filetools.C:
106 string lang = getEnv("LC_ALL");
108 lang = getEnv("LC_MESSAGES");
110 lang = getEnv("LANG");
116 char const * works = setlocale(LC_MESSAGES, lang_.c_str());
117 // CTYPE controls what getmessage thinks what encoding the po file uses
118 string oldCTYPE = setlocale(LC_CTYPE, NULL);
119 setlocale(LC_CTYPE, lang_.c_str());
121 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
124 lyxerr << "Error code: " << errno << std::endl;
125 lyxerr << "Lang, mess: " << lang_ << " " << m << std::endl;
126 lyxerr << "Directory: " << package().locale_dir() << std::endl;
127 lyxerr << "Rtn value: " << c << std::endl;
130 const char* msg = gettext(m.c_str());
131 string translated(works ? msg : m);
132 // Some english words have different translations, depending
133 // on context. In these cases the original string is
134 // augmented by context information (e.g.
135 // "To:[[as in 'From page x to page y']]" and
136 // "To:[[as in 'From format x to format y']]".
137 // This means that we need to filter out everything in
138 // double square brackets at the end of the string,
139 // otherwise the user sees bogus messages.
140 // If we are unable to honour the request we just
141 // return what we got in.
142 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
144 if (regex_match(translated, sub, reg))
145 translated = sub.str(1);
146 setlocale(LC_MESSAGES, lang.c_str());
147 setlocale(LC_CTYPE, oldCTYPE.c_str());
157 // This is the dummy variant.
158 class Messages::Pimpl {
160 Pimpl(string const &) {}
164 string const get(string const & m) const
173 : pimpl_(new Pimpl(""))
177 Messages::Messages(string const & l)
178 : pimpl_(new Pimpl(l))
182 // We need this for the sake of scoped_ptr
183 Messages::~Messages()
187 string const Messages::get(string const & msg) const
189 return pimpl_->get(msg);