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/types.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 #ifdef WORDS_BIGENDIAN
56 char const * codeset = "UCS-4BE";
58 char const * codeset = "UCS-4LE";
61 // This version use the traditional gettext.
62 Messages::Messages(string const & l)
65 if ( lang_.empty() ) {
66 char const * lc_msgs = 0;
67 #ifdef HAVE_LC_MESSAGES
68 lc_msgs = setlocale(LC_MESSAGES, NULL);
70 lang_ = lc_msgs ? lc_msgs : "";
72 // strip off any encoding suffix, i.e., assume 8-bit po files
73 string::size_type i = lang_.find(".");
74 lang_ = lang_.substr(0, i);
75 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
76 << ": language(" << lang_ << ")" << endl;
80 docstring const Messages::get(string const & m) const
85 // Look for the translated string in the cache.
86 TranslationCache::iterator it = cache_.find(m);
87 if (it != cache_.end())
89 // The string was not found, use gettext to generate it:
91 // In this order, see support/filetools.C:
92 string lang = getEnv("LC_ALL");
94 lang = getEnv("LC_MESSAGES");
96 lang = getEnv("LANG");
101 #ifdef HAVE_LC_MESSAGES
102 char const * lc_msgs = setlocale(LC_MESSAGES, lang_.c_str());
104 // setlocale fails (returns NULL) if the corresponding locale
106 // On windows (mingw and cygwin) it always returns NULL.
107 // Since this method gets called for every translatable
108 // buffer string like e.g. "Figure:" we warn only once.
109 #if !defined(_WIN32) && !defined(__CYGWIN__)
111 if (!warned && !lc_msgs) {
113 lyxerr << "Locale " << lang_ << " could not be set" << endl;
116 // CTYPE controls what getmessage thinks what encoding the po file uses
117 char const * lc_ctype = setlocale(LC_CTYPE, NULL);
118 string oldCTYPE = lc_ctype ? lc_ctype : "";
120 setlocale(LC_CTYPE, lang_.c_str());
122 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
126 << BOOST_CURRENT_FUNCTION << '\n'
127 << "Error code: " << errno << '\n'
128 << "Lang, mess: " << lang_ << " " << m << '\n'
129 << "Directory : " << package().locale_dir() << '\n'
130 << "Rtn value : " << c << endl;
133 if (!bind_textdomain_codeset(PACKAGE, codeset)) {
135 << BOOST_CURRENT_FUNCTION << '\n'
136 << "Error code: " << errno << '\n'
137 << "Codeset : " << codeset << '\n'
142 char const * tmp = m.c_str();
143 char const * msg = gettext(tmp);
144 docstring translated;
145 if (!msg || msg == tmp) {
147 lyxerr << "Undefined result from gettext" << endl;
149 // lyxerr << "Same as entered returned" << endl;
150 // Some english words have different translations,
151 // depending on context. In these cases the original
152 // string is augmented by context information (e.g.
153 // "To:[[as in 'From page x to page y']]" and
154 // "To:[[as in 'From format x to format y']]".
155 // This means that we need to filter out everything
156 // in double square brackets at the end of the
157 // string, otherwise the user sees bogus messages.
158 // If we are unable to honour the request we just
159 // return what we got in.
161 if (regex_match(m, sub, reg))
162 translated = from_ascii(sub.str(1));
164 translated = from_ascii(tmp);
166 lyxerr[Debug::DEBUG] << "We got a translation" << endl;
167 char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
170 #ifdef HAVE_LC_MESSAGES
171 setlocale(LC_MESSAGES, lang.c_str());
173 setlocale(LC_CTYPE, oldCTYPE.c_str());
175 std::pair<TranslationCache::iterator, bool> result =
176 cache_.insert(std::make_pair(m, translated));
178 BOOST_ASSERT(result.second);
180 return result.first->second;
186 // This is the dummy variant.
189 using std::make_pair;
195 Messages::Messages(string const & l) {}
198 docstring const Messages::get(string const & m) const
202 if (regex_match(m, sub, reg))
203 return from_ascii(sub.str(1));
205 return from_ascii(m);
218 // This version of the Pimpl utilizes the message capability of
219 // libstdc++ that is distributed with GNU G++.
220 class Messages::Pimpl {
222 typedef std::messages<char>::catalog catalog;
224 Pimpl(string const & l)
226 loc_gl(lang_.c_str()),
227 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
229 //lyxerr << "Messages: language(" << l
230 // << ") in dir(" << dir << ")" << endl;
232 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
238 mssg_gl.close(cat_gl);
241 docstring const get(string const & msg) const
243 return mssg_gl.get(cat_gl, 0, 0, msg);
251 std::messages<char> const & mssg_gl;