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 if ( lang_.empty() ) {
60 char const * lc_msgs = 0;
61 #ifdef HAVE_LC_MESSAGES
62 lc_msgs = setlocale(LC_MESSAGES, NULL);
64 lang_ = lc_msgs ? lc_msgs : "";
66 // strip off any encoding suffix, i.e., assume 8-bit po files
67 string::size_type i = lang_.find(".");
68 lang_ = lang_.substr(0, i);
69 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
70 << ": language(" << lang_ << ")" << endl;
74 docstring const Messages::get(string const & m) const
79 // Look for the translated string in the cache.
80 TranslationCache::iterator it = cache_.find(m);
81 if (it != cache_.end())
83 // The string was not found, use gettext to generate it:
85 // In this order, see support/filetools.C:
86 string lang = getEnv("LC_ALL");
88 lang = getEnv("LC_MESSAGES");
90 lang = getEnv("LANG");
95 #ifdef HAVE_LC_MESSAGES
96 char const * lc_msgs = setlocale(LC_MESSAGES, lang_.c_str());
98 // setlocale fails (returns NULL) if the corresponding locale
100 // On windows (mingw and cygwin) it always returns NULL.
101 // Since this method gets called for every translatable
102 // buffer string like e.g. "Figure:" we warn only once.
103 #if !defined(_WIN32) && !defined(__CYGWIN__)
104 if (!warned_ && !lc_msgs) {
106 lyxerr << "Locale " << lang_ << " could not be set" << endl;
109 // CTYPE controls what getmessage thinks what encoding the po file uses
110 char const * lc_ctype = setlocale(LC_CTYPE, NULL);
111 string oldCTYPE = lc_ctype ? lc_ctype : "";
113 setlocale(LC_CTYPE, lang_.c_str());
115 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
119 << BOOST_CURRENT_FUNCTION << '\n'
120 << "Error code: " << errno << '\n'
121 << "Lang, mess: " << lang_ << " " << m << '\n'
122 << "Directory : " << package().locale_dir() << '\n'
123 << "Rtn value : " << c << endl;
126 if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
128 << BOOST_CURRENT_FUNCTION << '\n'
129 << "Error code: " << errno << '\n'
130 << "Codeset : " << ucs4_codeset << '\n'
135 char const * tmp = m.c_str();
136 char const * msg = gettext(tmp);
137 docstring translated;
138 if (!msg || msg == tmp) {
140 lyxerr << "Undefined result from gettext" << endl;
142 // lyxerr << "Same as entered returned" << endl;
143 // Some english words have different translations,
144 // depending on context. In these cases the original
145 // string is augmented by context information (e.g.
146 // "To:[[as in 'From page x to page y']]" and
147 // "To:[[as in 'From format x to format y']]".
148 // This means that we need to filter out everything
149 // in double square brackets at the end of the
150 // string, otherwise the user sees bogus messages.
151 // If we are unable to honour the request we just
152 // return what we got in.
154 if (regex_match(m, sub, reg))
155 translated = from_ascii(sub.str(1));
157 translated = from_ascii(tmp);
159 lyxerr[Debug::DEBUG] << "We got a translation" << endl;
160 char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
163 #ifdef HAVE_LC_MESSAGES
164 setlocale(LC_MESSAGES, lang.c_str());
166 setlocale(LC_CTYPE, oldCTYPE.c_str());
168 std::pair<TranslationCache::iterator, bool> result =
169 cache_.insert(std::make_pair(m, translated));
171 BOOST_ASSERT(result.second);
173 return result.first->second;
179 // This is the dummy variant.
182 using std::make_pair;
188 Messages::Messages(string const & l) {}
191 docstring const Messages::get(string const & m) const
195 if (regex_match(m, sub, reg))
196 return from_ascii(sub.str(1));
198 return from_ascii(m);
211 // This version of the Pimpl utilizes the message capability of
212 // libstdc++ that is distributed with GNU G++.
213 class Messages::Pimpl {
215 typedef std::messages<char>::catalog catalog;
217 Pimpl(string const & l)
219 loc_gl(lang_.c_str()),
220 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
222 //lyxerr << "Messages: language(" << l
223 // << ") in dir(" << dir << ")" << endl;
225 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
231 mssg_gl.close(cat_gl);
234 docstring const get(string const & msg) const
236 return mssg_gl.get(cat_gl, 0, 0, msg);
244 std::messages<char> const & mssg_gl;