Update lyx examples to latest file format (for 1.5.0 release)
[lyx.git] / src / Messages.cpp
blobcbead2110636f7877040124c425bdfa664735e5d
1 /* \file Messages.cpp
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.
8 */
10 #include <config.h>
12 #include "Messages.h"
14 #include "debug.h"
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>
25 #include <cerrno>
27 namespace {
28 boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
31 #ifdef ENABLE_NLS
33 #ifdef HAVE_LOCALE_H
34 # include <locale.h>
35 #endif
37 # if HAVE_GETTEXT
38 # include <libintl.h> // use the header already in the system *EK*
39 # else
40 # include "../intl/libintl.h"
41 # endif
43 using std::endl;
44 using std::make_pair;
45 using std::map;
46 using std::string;
48 namespace lyx {
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);
63 #endif
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
76 if (m.empty())
77 return docstring();
79 // Look for the translated string in the cache.
80 TranslationCache::iterator it = cache_.find(m);
81 if (it != cache_.end())
82 return it->second;
83 // The string was not found, use gettext to generate it:
85 // In this order, see support/filetools.cpp:
86 string lang = getEnv("LC_ALL");
87 if (lang.empty()) {
88 lang = getEnv("LC_MESSAGES");
89 if (lang.empty()) {
90 lang = getEnv("LANG");
91 if (lang.empty())
92 lang = "C";
95 #ifdef HAVE_LC_MESSAGES
96 char const * lc_msgs = setlocale(LC_MESSAGES, lang_.c_str());
97 #endif
98 // setlocale fails (returns NULL) if the corresponding locale
99 // is not installed.
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) {
105 warned_ = true;
106 lyxerr << "Locale " << lang_ << " could not be set" << endl;
108 #endif
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());
114 errno = 0;
115 string const locale_dir = package().locale_dir().toFilesystemEncoding();
116 char const * c = bindtextdomain(PACKAGE, locale_dir.c_str());
117 int e = errno;
118 if (e) {
119 LYXERR(Debug::DEBUG)
120 << BOOST_CURRENT_FUNCTION << '\n'
121 << "Error code: " << errno << '\n'
122 << "Lang, mess: " << lang_ << " " << m << '\n'
123 << "Directory : " << package().locale_dir().absFilename() << '\n'
124 << "Rtn value : " << c << endl;
127 if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
128 LYXERR(Debug::DEBUG)
129 << BOOST_CURRENT_FUNCTION << '\n'
130 << "Error code: " << errno << '\n'
131 << "Codeset : " << ucs4_codeset << '\n'
132 << endl;
135 textdomain(PACKAGE);
136 char const * tmp = m.c_str();
137 char const * msg = gettext(tmp);
138 docstring translated;
139 if (!msg || msg == tmp) {
140 if (!msg)
141 lyxerr << "Undefined result from gettext" << endl;
142 //else
143 // lyxerr << "Same as entered returned" << endl;
144 // Some english words have different translations,
145 // depending on context. In these cases the original
146 // string is augmented by context information (e.g.
147 // "To:[[as in 'From page x to page y']]" and
148 // "To:[[as in 'From format x to format y']]".
149 // This means that we need to filter out everything
150 // in double square brackets at the end of the
151 // string, otherwise the user sees bogus messages.
152 // If we are unable to honour the request we just
153 // return what we got in.
154 boost::smatch sub;
155 if (regex_match(m, sub, reg))
156 translated = from_ascii(sub.str(1));
157 else
158 translated = from_ascii(tmp);
159 } else {
160 LYXERR(Debug::DEBUG) << "We got a translation" << endl;
161 char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
162 translated = ucs4;
164 #ifdef HAVE_LC_MESSAGES
165 setlocale(LC_MESSAGES, lang.c_str());
166 #endif
167 setlocale(LC_CTYPE, oldCTYPE.c_str());
169 std::pair<TranslationCache::iterator, bool> result =
170 cache_.insert(std::make_pair(m, translated));
172 BOOST_ASSERT(result.second);
174 return result.first->second;
177 } // namespace lyx
179 #else // ENABLE_NLS
180 // This is the dummy variant.
182 using std::endl;
183 using std::make_pair;
184 using std::map;
185 using std::string;
187 namespace lyx {
189 Messages::Messages(string const & l) {}
192 docstring const Messages::get(string const & m) const
194 // See comment above
195 boost::smatch sub;
196 if (regex_match(m, sub, reg))
197 return from_ascii(sub.str(1));
198 else
199 return from_ascii(m);
202 } // namespace lyx
204 #endif
206 #if 0
208 -#include <locale>
210 namespace lyx {
212 // This version of the Pimpl utilizes the message capability of
213 // libstdc++ that is distributed with GNU G++.
214 class Messages::Pimpl {
215 public:
216 typedef std::messages<char>::catalog catalog;
218 Pimpl(string const & l)
219 : lang_(l),
220 loc_gl(lang_.c_str()),
221 mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
223 //lyxerr << "Messages: language(" << l
224 // << ") in dir(" << dir << ")" << endl;
226 string const locale_dir = package().locale_dir().toFilesystemEncoding();
227 cat_gl = mssg_gl.open(PACKAGE, loc_gl, locale_dir.c_str());
231 ~Pimpl()
233 mssg_gl.close(cat_gl);
236 docstring const get(string const & msg) const
238 return mssg_gl.get(cat_gl, 0, 0, msg);
240 private:
242 string lang_;
244 std::locale loc_gl;
246 std::messages<char> const & mssg_gl;
248 catalog cat_gl;
251 } // namespace lyx
253 #endif