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)
63 : lang_(l), warned_(false)
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__)
110 if (!warned_ && !lc_msgs) {
112 lyxerr << "Locale " << lang_ << " could not be set" << endl;
115 // CTYPE controls what getmessage thinks what encoding the po file uses
116 char const * lc_ctype = setlocale(LC_CTYPE, NULL);
117 string oldCTYPE = lc_ctype ? lc_ctype : "";
119 setlocale(LC_CTYPE, lang_.c_str());
121 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
125 << BOOST_CURRENT_FUNCTION << '\n'
126 << "Error code: " << errno << '\n'
127 << "Lang, mess: " << lang_ << " " << m << '\n'
128 << "Directory : " << package().locale_dir() << '\n'
129 << "Rtn value : " << c << endl;
132 if (!bind_textdomain_codeset(PACKAGE, codeset)) {
134 << BOOST_CURRENT_FUNCTION << '\n'
135 << "Error code: " << errno << '\n'
136 << "Codeset : " << codeset << '\n'
141 char const * tmp = m.c_str();
142 char const * msg = gettext(tmp);
143 docstring translated;
144 if (!msg || msg == tmp) {
146 lyxerr << "Undefined result from gettext" << endl;
148 // lyxerr << "Same as entered returned" << endl;
149 // Some english words have different translations,
150 // depending on context. In these cases the original
151 // string is augmented by context information (e.g.
152 // "To:[[as in 'From page x to page y']]" and
153 // "To:[[as in 'From format x to format y']]".
154 // This means that we need to filter out everything
155 // in double square brackets at the end of the
156 // string, otherwise the user sees bogus messages.
157 // If we are unable to honour the request we just
158 // return what we got in.
160 if (regex_match(m, sub, reg))
161 translated = from_ascii(sub.str(1));
163 translated = from_ascii(tmp);
165 lyxerr[Debug::DEBUG] << "We got a translation" << endl;
166 char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
169 #ifdef HAVE_LC_MESSAGES
170 setlocale(LC_MESSAGES, lang.c_str());
172 setlocale(LC_CTYPE, oldCTYPE.c_str());
174 std::pair<TranslationCache::iterator, bool> result =
175 cache_.insert(std::make_pair(m, translated));
177 BOOST_ASSERT(result.second);
179 return result.first->second;
185 // This is the dummy variant.
188 using std::make_pair;
194 Messages::Messages(string const & l) {}
197 docstring const Messages::get(string const & m) const
201 if (regex_match(m, sub, reg))
202 return from_ascii(sub.str(1));
204 return from_ascii(m);
217 // This version of the Pimpl utilizes the message capability of
218 // libstdc++ that is distributed with GNU G++.
219 class Messages::Pimpl {
221 typedef std::messages<char>::catalog catalog;
223 Pimpl(string const & l)
225 loc_gl(lang_.c_str()),
226 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
228 //lyxerr << "Messages: language(" << l
229 // << ") in dir(" << dir << ")" << endl;
231 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
237 mssg_gl.close(cat_gl);
240 docstring const get(string const & msg) const
242 return mssg_gl.get(cat_gl, 0, 0, msg);
250 std::messages<char> const & mssg_gl;