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.
16 #include "support/docstring.h"
17 #include "support/environment.h"
18 #include "support/filetools.h"
19 #include "support/Package.h"
20 #include "support/unicode.h"
22 #include <boost/current_function.hpp>
23 #include <boost/regex.hpp>
34 # include <libintl.h> // use the header already in the system *EK*
36 # include "../intl/libintl.h"
46 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
48 using support::package;
49 using support::getEnv;
50 using support::setEnv;
53 // This version use the traditional gettext.
54 Messages::Messages(string const & l)
55 : lang_(l), warned_(false)
57 // strip off any encoding suffix, i.e., assume 8-bit po files
58 string::size_type i = lang_.find(".");
59 lang_ = lang_.substr(0, i);
60 LYXERR(Debug::DEBUG, BOOST_CURRENT_FUNCTION
61 << ": language(" << lang_ << ")");
68 string const locale_dir = package().locale_dir().toFilesystemEncoding();
69 char const * c = bindtextdomain(PACKAGE, locale_dir.c_str());
72 LYXERR(Debug::DEBUG, BOOST_CURRENT_FUNCTION << '\n'
73 << "Error code: " << errno << '\n'
74 << "Directory : " << package().locale_dir().absFilename() << '\n'
75 << "Rtn value : " << c);
78 if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
79 LYXERR(Debug::DEBUG, BOOST_CURRENT_FUNCTION << '\n'
80 << "Error code: " << errno << '\n'
81 << "Codeset : " << ucs4_codeset << '\n');
88 docstring const Messages::get(string const & m) const
93 // Look for the translated string in the cache.
94 TranslationCache::iterator it = cache_.find(m);
95 if (it != cache_.end())
98 // The string was not found, use gettext to generate it
100 string const oldLANGUAGE = getEnv("LANGUAGE");
101 string const oldLC_ALL = getEnv("LC_ALL");
102 if (!lang_.empty()) {
103 // This GNU extension overrides any language locale
105 setEnv("LANGUAGE", lang_);
106 // However, setting LANGUAGE does nothing when the
107 // locale is "C". Therefore we set the locale to
108 // something that is believed to exist on most
109 // systems. The idea is that one should be able to
110 // load German documents even without having de_DE
112 setEnv("LC_ALL", "en_US");
113 #ifdef HAVE_LC_MESSAGES
114 setlocale(LC_MESSAGES, "");
118 char const * tmp = m.c_str();
119 char const * msg = gettext(tmp);
120 docstring translated;
121 if (!msg || msg == tmp) {
123 lyxerr << "Undefined result from gettext" << endl;
125 // lyxerr << "Same as entered returned" << endl;
126 // Some english words have different translations,
127 // depending on context. In these cases the original
128 // string is augmented by context information (e.g.
129 // "To:[[as in 'From page x to page y']]" and
130 // "To:[[as in 'From format x to format y']]".
131 // This means that we need to filter out everything
132 // in double square brackets at the end of the
133 // string, otherwise the user sees bogus messages.
134 // If we are unable to honour the request we just
135 // return what we got in.
137 if (regex_match(m, sub, reg))
138 translated = from_ascii(sub.str(1));
140 translated = from_ascii(tmp);
142 LYXERR(Debug::DEBUG, "We got a translation");
143 // m is actually not a char const * but ucs4 data
144 translated = reinterpret_cast<char_type const *>(msg);
147 if (!lang_.empty()) {
148 // Reset everything as it was.
149 setEnv("LANGUAGE", oldLANGUAGE);
150 setEnv("LC_ALL", oldLC_ALL);
151 #ifdef HAVE_LC_MESSAGES
152 setlocale(LC_MESSAGES, "");
156 std::pair<TranslationCache::iterator, bool> result =
157 cache_.insert(std::make_pair(m, translated));
159 BOOST_ASSERT(result.second);
161 return result.first->second;
167 // This is the dummy variant.
170 using std::make_pair;
176 Messages::Messages(string const & l) {}
178 void Messages::init()
183 docstring const Messages::get(string const & m) const
187 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
188 if (regex_match(m, sub, reg))
189 return from_ascii(sub.str(1));
191 return from_ascii(m);
204 // This version of the Pimpl utilizes the message capability of
205 // libstdc++ that is distributed with GNU G++.
206 class Messages::Pimpl {
208 typedef std::messages<char>::catalog catalog;
210 Pimpl(string const & l)
212 loc_gl(lang_.c_str()),
213 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
215 //lyxerr << "Messages: language(" << l
216 // << ") in dir(" << dir << ")" << endl;
218 string const locale_dir = package().locale_dir().toFilesystemEncoding();
219 cat_gl = mssg_gl.open(PACKAGE, loc_gl, locale_dir.c_str());
225 mssg_gl.close(cat_gl);
228 docstring const get(string const & msg) const
230 return mssg_gl.get(cat_gl, 0, 0, msg);
238 std::messages<char> const & mssg_gl;