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_ << ")" << endl;
68 string const locale_dir = package().locale_dir().toFilesystemEncoding();
69 char const * c = bindtextdomain(PACKAGE, locale_dir.c_str());
73 << BOOST_CURRENT_FUNCTION << '\n'
74 << "Error code: " << errno << '\n'
75 << "Directory : " << package().locale_dir().absFilename() << '\n'
76 << "Rtn value : " << c << endl;
79 if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
81 << BOOST_CURRENT_FUNCTION << '\n'
82 << "Error code: " << errno << '\n'
83 << "Codeset : " << ucs4_codeset << '\n'
91 docstring const Messages::get(string const & m) const
96 // Look for the translated string in the cache.
97 TranslationCache::iterator it = cache_.find(m);
98 if (it != cache_.end())
101 // The string was not found, use gettext to generate it
103 string const oldLANGUAGE = getEnv("LANGUAGE");
104 string const oldLC_ALL = getEnv("LC_ALL");
105 if (!lang_.empty()) {
106 // This GNU extension overrides any language locale
108 setEnv("LANGUAGE", lang_);
109 // However, setting LANGUAGE does nothing when the
110 // locale is "C". Therefore we set the locale to
111 // something that is believed to exist on most
112 // systems. The idea is that one should be able to
113 // load German documents even without having de_DE
115 setEnv("LC_ALL", "en_US");
116 #ifdef HAVE_LC_MESSAGES
117 setlocale(LC_MESSAGES, "");
121 char const * tmp = m.c_str();
122 char const * msg = gettext(tmp);
123 docstring translated;
124 if (!msg || msg == tmp) {
126 lyxerr << "Undefined result from gettext" << endl;
128 // lyxerr << "Same as entered returned" << endl;
129 // Some english words have different translations,
130 // depending on context. In these cases the original
131 // string is augmented by context information (e.g.
132 // "To:[[as in 'From page x to page y']]" and
133 // "To:[[as in 'From format x to format y']]".
134 // This means that we need to filter out everything
135 // in double square brackets at the end of the
136 // string, otherwise the user sees bogus messages.
137 // If we are unable to honour the request we just
138 // return what we got in.
140 if (regex_match(m, sub, reg))
141 translated = from_ascii(sub.str(1));
143 translated = from_ascii(tmp);
145 LYXERR(Debug::DEBUG) << "We got a translation" << endl;
146 // m is actually not a char const * but ucs4 data
147 translated = reinterpret_cast<char_type const *>(msg);
150 if (!lang_.empty()) {
151 // Reset everything as it was.
152 setEnv("LANGUAGE", oldLANGUAGE);
153 setEnv("LC_ALL", oldLC_ALL);
154 #ifdef HAVE_LC_MESSAGES
155 setlocale(LC_MESSAGES, "");
159 std::pair<TranslationCache::iterator, bool> result =
160 cache_.insert(std::make_pair(m, translated));
162 BOOST_ASSERT(result.second);
164 return result.first->second;
170 // This is the dummy variant.
173 using std::make_pair;
179 Messages::Messages(string const & l) {}
181 void Messages::init()
186 docstring const Messages::get(string const & m) const
190 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
191 if (regex_match(m, sub, reg))
192 return from_ascii(sub.str(1));
194 return from_ascii(m);
207 // This version of the Pimpl utilizes the message capability of
208 // libstdc++ that is distributed with GNU G++.
209 class Messages::Pimpl {
211 typedef std::messages<char>::catalog catalog;
213 Pimpl(string const & l)
215 loc_gl(lang_.c_str()),
216 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
218 //lyxerr << "Messages: language(" << l
219 // << ") in dir(" << dir << ")" << endl;
221 string const locale_dir = package().locale_dir().toFilesystemEncoding();
222 cat_gl = mssg_gl.open(PACKAGE, loc_gl, locale_dir.c_str());
228 mssg_gl.close(cat_gl);
231 docstring const get(string const & msg) const
233 return mssg_gl.get(cat_gl, 0, 0, msg);
241 std::messages<char> const & mssg_gl;