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/package.h"
17 #include <boost/regex.hpp>
19 using lyx::support::package;
31 // This version of the Pimpl utilizes the message capability of
32 // libstdc++ that is distributed with GNU G++.
33 class Messages::Pimpl {
35 typedef std::messages<char>::catalog catalog;
37 Pimpl(string const & l)
39 loc_gl(lang_.c_str()),
40 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
42 //lyxerr << "Messages: language(" << l
43 // << ") in dir(" << dir << ")" << std::endl;
45 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
51 mssg_gl.close(cat_gl);
54 string const get(string const & msg) const
56 return mssg_gl.get(cat_gl, 0, 0, msg);
64 std::messages<char> const & mssg_gl;
75 # include <libintl.h> // use the header already in the system *EK*
77 # include "../intl/libintl.h"
80 // This is a more traditional variant.
81 class Messages::Pimpl {
83 Pimpl(string const & l)
87 lang_ = setlocale(LC_MESSAGES, NULL);
88 lyxerr << "Messages: language(" << lang_
89 // << ") in dir(" << dir
96 string const get(string const & m) const
101 string oldMSG = setlocale(LC_MESSAGES, NULL);
102 bool works = setlocale(LC_MESSAGES, lang_.c_str());
103 // CTYPE controls what getmessage thinks what encoding the po file uses
104 string oldCTYPE = setlocale(LC_CTYPE, NULL);
105 setlocale(LC_CTYPE, lang_.c_str());
106 bindtextdomain(PACKAGE, package().locale_dir().c_str());
108 const char* msg = gettext(m.c_str());
109 string translated(works ? msg : m);
110 // Some english words have different translations, depending
111 // on context. In these cases the original string is
112 // augmented by context information (e.g.
113 // "To:[[as in 'From page x to page y']]" and
114 // "To:[[as in 'From format x to format y']]".
115 // This means that we need to filter out everything in
116 // double square brackets at the end of the string,
117 // otherwise the user sees bogus messages.
118 // If we are unable to honour the request we just
119 // return what we got in.
120 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
122 if (regex_match(translated, sub, reg))
123 translated = sub.str(1);
124 setlocale(LC_MESSAGES, oldMSG.c_str());
125 setlocale(LC_CTYPE, oldCTYPE.c_str());
135 // This is the dummy variant.
136 class Messages::Pimpl {
138 Pimpl(string const &) {}
142 string const get(string const & m) const
151 : pimpl_(new Pimpl(""))
155 Messages::Messages(string const & l)
156 : pimpl_(new Pimpl(l))
160 // We need this for the sake of scoped_ptr
161 Messages::~Messages()
165 string const Messages::get(string const & msg) const
167 return pimpl_->get(msg);