2003-04-23 20:44:53 +00:00
|
|
|
|
/* \file messages.C
|
2003-04-23 18:47:21 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-04-23 18:47:21 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2005-07-16 15:55:36 +00:00
|
|
|
|
#include "debug.h"
|
2003-04-23 18:47:21 +00:00
|
|
|
|
#include "messages.h"
|
2003-04-27 22:11:45 +00:00
|
|
|
|
#include "support/filetools.h"
|
2005-07-17 11:58:54 +00:00
|
|
|
|
#include "support/environment.h"
|
2005-01-10 19:17:43 +00:00
|
|
|
|
#include "support/package.h"
|
2003-04-23 18:47:21 +00:00
|
|
|
|
|
2005-09-26 09:20:15 +00:00
|
|
|
|
#include <boost/current_function.hpp>
|
2004-10-19 09:11:02 +00:00
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
|
|
2005-01-10 19:17:43 +00:00
|
|
|
|
using lyx::support::package;
|
2005-07-17 11:58:54 +00:00
|
|
|
|
using lyx::support::getEnv;
|
|
|
|
|
using lyx::support::setEnv;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
|
|
|
|
|
2003-04-23 18:47:21 +00:00
|
|
|
|
|
2003-04-24 22:09:46 +00:00
|
|
|
|
#ifdef ENABLE_NLS
|
2003-04-23 18:47:21 +00:00
|
|
|
|
|
2003-04-27 22:11:45 +00:00
|
|
|
|
|
2003-04-24 22:09:46 +00:00
|
|
|
|
#if 0
|
2003-04-23 18:47:21 +00:00
|
|
|
|
|
2003-09-05 16:31:30 +00:00
|
|
|
|
-#include <locale>
|
2003-04-24 22:09:46 +00:00
|
|
|
|
|
|
|
|
|
// This version of the Pimpl utilizes the message capability of
|
2003-04-28 01:52:18 +00:00
|
|
|
|
// libstdc++ that is distributed with GNU G++.
|
2003-04-24 22:09:46 +00:00
|
|
|
|
class Messages::Pimpl {
|
|
|
|
|
public:
|
|
|
|
|
typedef std::messages<char>::catalog catalog;
|
|
|
|
|
|
2003-04-27 22:11:45 +00:00
|
|
|
|
Pimpl(string const & l)
|
|
|
|
|
: lang_(l),
|
2003-04-24 22:09:46 +00:00
|
|
|
|
loc_gl(lang_.c_str()),
|
|
|
|
|
mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
|
|
|
|
|
{
|
|
|
|
|
//lyxerr << "Messages: language(" << l
|
|
|
|
|
// << ") in dir(" << dir << ")" << std::endl;
|
|
|
|
|
|
2005-01-10 19:17:43 +00:00
|
|
|
|
cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
|
2003-04-24 22:09:46 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~Pimpl()
|
|
|
|
|
{
|
|
|
|
|
mssg_gl.close(cat_gl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string const get(string const & msg) const
|
|
|
|
|
{
|
|
|
|
|
return mssg_gl.get(cat_gl, 0, 0, msg);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
///
|
|
|
|
|
string lang_;
|
|
|
|
|
///
|
|
|
|
|
std::locale loc_gl;
|
|
|
|
|
///
|
|
|
|
|
std::messages<char> const & mssg_gl;
|
|
|
|
|
///
|
|
|
|
|
catalog cat_gl;
|
|
|
|
|
};
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LOCALE_H
|
|
|
|
|
# include <locale.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
# if HAVE_GETTEXT
|
|
|
|
|
# include <libintl.h> // use the header already in the system *EK*
|
|
|
|
|
# else
|
|
|
|
|
# include "../intl/libintl.h"
|
|
|
|
|
# endif
|
2003-04-23 18:47:21 +00:00
|
|
|
|
|
2003-04-24 22:09:46 +00:00
|
|
|
|
// This is a more traditional variant.
|
|
|
|
|
class Messages::Pimpl {
|
|
|
|
|
public:
|
2003-04-27 22:11:45 +00:00
|
|
|
|
Pimpl(string const & l)
|
|
|
|
|
: lang_(l)
|
2003-04-24 22:09:46 +00:00
|
|
|
|
{
|
2005-07-16 15:55:36 +00:00
|
|
|
|
if ( lang_.empty() )
|
|
|
|
|
lang_ = setlocale(LC_MESSAGES, NULL);
|
2005-07-17 11:58:54 +00:00
|
|
|
|
// strip off any encoding suffix, i.e., assume 8-bit po files
|
|
|
|
|
string::size_type i = lang_.find(".");
|
|
|
|
|
lang_ = lang_.substr(0, i);
|
2005-07-18 15:12:15 +00:00
|
|
|
|
lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
|
|
|
|
|
<< ": language(" << lang_ << ")" << std::endl;
|
2003-04-24 22:09:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~Pimpl() {}
|
|
|
|
|
|
|
|
|
|
string const get(string const & m) const
|
|
|
|
|
{
|
2003-06-10 14:39:45 +00:00
|
|
|
|
if (m.empty())
|
|
|
|
|
return m;
|
|
|
|
|
|
2005-07-17 11:58:54 +00:00
|
|
|
|
//string oldMSG = setlocale(LC_MESSAGES, NULL);
|
|
|
|
|
// In this order, see support/filetools.C:
|
|
|
|
|
string lang = getEnv("LC_ALL");
|
|
|
|
|
if (lang.empty()) {
|
|
|
|
|
lang = getEnv("LC_MESSAGES");
|
|
|
|
|
if (lang.empty()) {
|
|
|
|
|
lang = getEnv("LANG");
|
|
|
|
|
if (lang.empty())
|
|
|
|
|
lang = "C";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char const * works = setlocale(LC_MESSAGES, lang_.c_str());
|
2005-08-18 01:46:44 +00:00
|
|
|
|
if (!works)
|
|
|
|
|
lyxerr << "Locale " << lang_ << " could not be set" << std::endl;
|
2005-07-16 15:55:36 +00:00
|
|
|
|
// CTYPE controls what getmessage thinks what encoding the po file uses
|
|
|
|
|
string oldCTYPE = setlocale(LC_CTYPE, NULL);
|
|
|
|
|
setlocale(LC_CTYPE, lang_.c_str());
|
2005-07-17 11:58:54 +00:00
|
|
|
|
errno = 0;
|
|
|
|
|
char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
|
|
|
|
|
int e = errno;
|
|
|
|
|
if (e) {
|
2005-07-18 15:12:15 +00:00
|
|
|
|
lyxerr[Debug::DEBUG]
|
|
|
|
|
<< BOOST_CURRENT_FUNCTION << '\n'
|
|
|
|
|
<< "Error code: " << errno << '\n'
|
|
|
|
|
<< "Lang, mess: " << lang_ << " " << m << '\n'
|
|
|
|
|
<< "Directory : " << package().locale_dir() << '\n'
|
|
|
|
|
<< "Rtn value : " << c << std::endl;
|
2005-07-17 11:58:54 +00:00
|
|
|
|
}
|
2004-06-08 14:06:11 +00:00
|
|
|
|
textdomain(PACKAGE);
|
2003-04-24 22:09:46 +00:00
|
|
|
|
const char* msg = gettext(m.c_str());
|
2005-07-16 15:55:36 +00:00
|
|
|
|
string translated(works ? msg : m);
|
2004-10-19 09:11:02 +00:00
|
|
|
|
// Some english words have different translations, depending
|
|
|
|
|
// on context. In these cases the original string is
|
|
|
|
|
// augmented by context information (e.g.
|
|
|
|
|
// "To:[[as in 'From page x to page y']]" and
|
|
|
|
|
// "To:[[as in 'From format x to format y']]".
|
|
|
|
|
// This means that we need to filter out everything in
|
|
|
|
|
// double square brackets at the end of the string,
|
|
|
|
|
// otherwise the user sees bogus messages.
|
2003-04-25 01:54:04 +00:00
|
|
|
|
// If we are unable to honour the request we just
|
|
|
|
|
// return what we got in.
|
2004-10-19 09:11:02 +00:00
|
|
|
|
static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
|
|
|
|
|
boost::smatch sub;
|
|
|
|
|
if (regex_match(translated, sub, reg))
|
|
|
|
|
translated = sub.str(1);
|
2005-07-17 11:58:54 +00:00
|
|
|
|
setlocale(LC_MESSAGES, lang.c_str());
|
2005-07-16 15:55:36 +00:00
|
|
|
|
setlocale(LC_CTYPE, oldCTYPE.c_str());
|
2004-10-19 09:11:02 +00:00
|
|
|
|
return translated;
|
2003-04-24 22:09:46 +00:00
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
///
|
|
|
|
|
string lang_;
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#else // ENABLE_NLS
|
|
|
|
|
// This is the dummy variant.
|
|
|
|
|
class Messages::Pimpl {
|
|
|
|
|
public:
|
2003-04-27 22:11:45 +00:00
|
|
|
|
Pimpl(string const &) {}
|
2003-04-24 22:09:46 +00:00
|
|
|
|
|
|
|
|
|
~Pimpl() {}
|
|
|
|
|
|
|
|
|
|
string const get(string const & m) const
|
|
|
|
|
{
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-04-27 22:11:45 +00:00
|
|
|
|
|
|
|
|
|
Messages::Messages()
|
|
|
|
|
: pimpl_(new Pimpl(""))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Messages::Messages(string const & l)
|
|
|
|
|
: pimpl_(new Pimpl(l))
|
2003-04-24 22:09:46 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
2003-04-24 23:46:40 +00:00
|
|
|
|
// We need this for the sake of scoped_ptr
|
2003-04-23 18:47:21 +00:00
|
|
|
|
Messages::~Messages()
|
2003-04-24 22:09:46 +00:00
|
|
|
|
{}
|
2003-04-23 18:47:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const Messages::get(string const & msg) const
|
|
|
|
|
{
|
2003-04-24 22:09:46 +00:00
|
|
|
|
return pimpl_->get(msg);
|
2003-04-23 18:47:21 +00:00
|
|
|
|
}
|