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.cpp:
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 string
const locale_dir
= package().locale_dir().toFilesystemEncoding();
116 char const * c
= bindtextdomain(PACKAGE
, locale_dir
.c_str());
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
)) {
129 << BOOST_CURRENT_FUNCTION
<< '\n'
130 << "Error code: " << errno
<< '\n'
131 << "Codeset : " << ucs4_codeset
<< '\n'
136 char const * tmp
= m
.c_str();
137 char const * msg
= gettext(tmp
);
138 docstring translated
;
139 if (!msg
|| msg
== tmp
) {
141 lyxerr
<< "Undefined result from gettext" << endl
;
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.
155 if (regex_match(m
, sub
, reg
))
156 translated
= from_ascii(sub
.str(1));
158 translated
= from_ascii(tmp
);
160 LYXERR(Debug::DEBUG
) << "We got a translation" << endl
;
161 char_type
const * ucs4
= reinterpret_cast<char_type
const *>(msg
);
164 #ifdef HAVE_LC_MESSAGES
165 setlocale(LC_MESSAGES
, lang
.c_str());
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
;
180 // This is the dummy variant.
183 using std::make_pair
;
189 Messages::Messages(string
const & l
) {}
192 docstring
const Messages::get(string
const & m
) const
196 if (regex_match(m
, sub
, reg
))
197 return from_ascii(sub
.str(1));
199 return from_ascii(m
);
212 // This version of the Pimpl utilizes the message capability of
213 // libstdc++ that is distributed with GNU G++.
214 class Messages::Pimpl
{
216 typedef std::messages
<char>::catalog catalog
;
218 Pimpl(string
const & 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());
233 mssg_gl
.close(cat_gl
);
236 docstring
const get(string
const & msg
) const
238 return mssg_gl
.get(cat_gl
, 0, 0, msg
);
246 std::messages
<char> const & mssg_gl
;