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>
28 boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
38 # include <libintl.h> // use the header already in the system *EK*
40 # include "../intl/libintl.h"
50 using support::package;
51 using support::getEnv;
52 using support::setEnv;
55 // This version use the traditional gettext.
56 Messages::Messages(string const & l)
57 : lang_(l), warned_(false)
59 // strip off any encoding suffix, i.e., assume 8-bit po files
60 string::size_type i = lang_.find(".");
61 lang_ = lang_.substr(0, i);
62 LYXERR(Debug::DEBUG) << BOOST_CURRENT_FUNCTION
63 << ": language(" << lang_ << ")" << endl;
70 string const locale_dir = package().locale_dir().toFilesystemEncoding();
71 char const * c = bindtextdomain(PACKAGE, locale_dir.c_str());
75 << BOOST_CURRENT_FUNCTION << '\n'
76 << "Error code: " << errno << '\n'
77 << "Directory : " << package().locale_dir().absFilename() << '\n'
78 << "Rtn value : " << c << endl;
81 if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
83 << BOOST_CURRENT_FUNCTION << '\n'
84 << "Error code: " << errno << '\n'
85 << "Codeset : " << ucs4_codeset << '\n'
93 docstring const Messages::get(string const & m) const
98 // Look for the translated string in the cache.
99 TranslationCache::iterator it = cache_.find(m);
100 if (it != cache_.end())
103 // The string was not found, use gettext to generate it
105 string const oldLANGUAGE = getEnv("LANGUAGE");
106 string const oldLC_ALL = getEnv("LC_ALL");
107 if (!lang_.empty()) {
108 // This GNU extension overrides any language locale
110 setEnv("LANGUAGE", lang_);
111 // However, setting LANGUAGE does nothing when the
112 // locale is "C". Therefore we set the locale to
113 // something that is believed to exist on most
114 // systems. The idea is that one should be able to
115 // load German documents even without having de_DE
117 setEnv("LC_ALL", "en_US");
118 #ifdef HAVE_LC_MESSAGES
119 setlocale(LC_MESSAGES, "");
123 char const * tmp = m.c_str();
124 char const * msg = gettext(tmp);
125 docstring translated;
126 if (!msg || msg == tmp) {
128 lyxerr << "Undefined result from gettext" << endl;
130 // lyxerr << "Same as entered returned" << endl;
131 // Some english words have different translations,
132 // depending on context. In these cases the original
133 // string is augmented by context information (e.g.
134 // "To:[[as in 'From page x to page y']]" and
135 // "To:[[as in 'From format x to format y']]".
136 // This means that we need to filter out everything
137 // in double square brackets at the end of the
138 // string, otherwise the user sees bogus messages.
139 // If we are unable to honour the request we just
140 // return what we got in.
142 if (regex_match(m, sub, reg))
143 translated = from_ascii(sub.str(1));
145 translated = from_ascii(tmp);
147 LYXERR(Debug::DEBUG) << "We got a translation" << endl;
148 // m is actually not a char const * but ucs4 data
149 translated = reinterpret_cast<char_type const *>(msg);
152 if (!lang_.empty()) {
153 // Reset everything as it was.
154 setEnv("LANGUAGE", oldLANGUAGE);
155 setEnv("LC_ALL", oldLC_ALL);
156 #ifdef HAVE_LC_MESSAGES
157 setlocale(LC_MESSAGES, "");
161 std::pair<TranslationCache::iterator, bool> result =
162 cache_.insert(std::make_pair(m, translated));
164 BOOST_ASSERT(result.second);
166 return result.first->second;
172 // This is the dummy variant.
175 using std::make_pair;
181 Messages::Messages(string const & l) {}
183 void Messages::init()
188 docstring const Messages::get(string const & m) const
192 if (regex_match(m, sub, reg))
193 return from_ascii(sub.str(1));
195 return from_ascii(m);
208 // This version of the Pimpl utilizes the message capability of
209 // libstdc++ that is distributed with GNU G++.
210 class Messages::Pimpl {
212 typedef std::messages<char>::catalog catalog;
214 Pimpl(string const & l)
216 loc_gl(lang_.c_str()),
217 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
219 //lyxerr << "Messages: language(" << l
220 // << ") in dir(" << dir << ")" << endl;
222 string const locale_dir = package().locale_dir().toFilesystemEncoding();
223 cat_gl = mssg_gl.open(PACKAGE, loc_gl, locale_dir.c_str());
229 mssg_gl.close(cat_gl);
232 docstring const get(string const & msg) const
234 return mssg_gl.get(cat_gl, 0, 0, msg);
242 std::messages<char> const & mssg_gl;