More messages work. Should now also

compile on some older compilers.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6848 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Lars Gullik Bjønnes 2003-04-24 22:09:46 +00:00
parent 265327763d
commit 4fbee58346
5 changed files with 150 additions and 77 deletions

View File

@ -1,3 +1,9 @@
2003-04-24 Lars Gullik Bjønnes <larsbj@gullik.net>
* messages.[hC]: pimplify Messages, and three different pimpls to be
used in different circumstances.
* gettext.[Ch]: change for use with new message code.
2003-04-24 André Pönitz <poenitz@gmx.net>

View File

@ -10,49 +10,53 @@
#include <config.h>
#include "messages.h"
#include "LString.h"
#include "support/LAssert.h"
#include <boost/scoped_ptr.hpp>
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
#include "LString.h"
namespace {
#include <boost/scoped_array.hpp>
boost::scoped_ptr<Messages> lyx_messages;
#ifdef ENABLE_NLS
} // anon namespace
# if HAVE_GETTEXT
# include <libintl.h> // use the header already in the system *EK*
# else
# include "../intl/libintl.h"
# endif
char const * _(char const * str)
{
// I'd rather have an Assert on str, we should not allow
// null pointers here. Lgb
// Assert(str);
if (str && str[0])
return gettext(str);
else
return "";
lyx::Assert(str && str[0]);
if (!lyx_messages.get())
return str;
return lyx_messages->get(str).c_str();
}
string const _(string const & str)
{
if (!str.empty()) {
int const s = str.length();
boost::scoped_array<char> tmp(new char[s + 1]);
str.copy(tmp.get(), s);
tmp[s] = '\0';
string const ret(gettext(tmp.get()));
return ret;
} else {
return string();
}
lyx::Assert(!str.empty());
if (!lyx_messages.get())
return str;
return lyx_messages->get(str);
}
void gettext_init(string const & localedir)
{
lyx_messages.reset(new Messages("", localedir));
}
#ifdef ENABLE_NLS
void locale_init()
{
# ifdef HAVE_LC_MESSAGES
@ -62,14 +66,6 @@ void locale_init()
setlocale(LC_NUMERIC, "C");
}
void gettext_init(string const & localedir)
{
bindtextdomain(PACKAGE, localedir.c_str());
textdomain(PACKAGE);
}
#else // ENABLE_NLS
void locale_init()
@ -77,8 +73,4 @@ void locale_init()
setlocale(LC_NUMERIC, "C");
}
void gettext_init(string const &)
{
}
#endif

View File

@ -32,21 +32,19 @@
#include "LString.h"
#ifdef ENABLE_NLS
//#ifdef ENABLE_NLS
///
char const * _(char const *);
///
string const _(string const &);
#else // ENABLE_NLS
//#else // ENABLE_NLS
///
# define _(str) (str)
///
# define S_(str) (str)
//# define _(str) (str)
#endif
//#endif
# define N_(str) (str) // for detecting static strings

View File

@ -12,27 +12,122 @@
#include "messages.h"
#include "debug.h"
using std::endl;
#ifdef ENABLE_NLS
#if 0
#include <locale>
// This version of the Pimpl utilizes the message capability of
// libstdc++ that is distributed with GNU G++
class Messages::Pimpl {
public:
typedef std::messages<char>::catalog catalog;
Pimpl(string const & l, string const & dir)
: lang_(l), localedir_(dir),
loc_gl(lang_.c_str()),
mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
{
//lyxerr << "Messages: language(" << l
// << ") in dir(" << dir << ")" << std::endl;
cat_gl = mssg_gl.open(PACKAGE, loc_gl, localedir_.c_str());
}
~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_;
///
string localedir_;
///
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
// This is a more traditional variant.
class Messages::Pimpl {
public:
Pimpl(string const & l, string const & dir)
: lang_(l), localedir_(dir)
{
//lyxerr << "Messages: language(" << l
// << ") in dir(" << dir << ")" << std::endl;
bindtextdomain(PACKAGE, localedir_.c_str());
textdomain(PACKAGE);
}
~Pimpl() {}
string const get(string const & m) const
{
char * old = strdup(setlocale(LC_ALL, 0));
setlocale(LC_ALL, lang_.c_str());
const char* msg = gettext(m.c_str());
setlocale(LC_ALL, old);
free(old);
return string(msg);
}
private:
///
string lang_;
///
string localedir_;
};
#endif
#else // ENABLE_NLS
// This is the dummy variant.
class Messages::Pimpl {
public:
Pimpl(string const &, string const &) {}
~Pimpl() {}
string const get(string const & m) const
{
return m;
}
};
#endif
Messages::Messages(string const & l, string const & dir)
: lang_(l), localedir_(dir),
loc_gl(lang_.c_str()),
mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
{
lyxerr << "Messages: language(" << l << ") in dir(" << dir << ")" << endl;
cat_gl = mssg_gl.open("lyx", loc_gl, localedir_.c_str());
}
: pimpl_(new Pimpl(l, dir))
{}
// We need this for the sake of scopted_ptr
Messages::~Messages()
{
mssg_gl.close(cat_gl);
}
{}
string const Messages::get(string const & msg) const
{
return mssg_gl.get(cat_gl, 0, 0, msg);
return pimpl_->get(msg);
}

View File

@ -13,38 +13,20 @@
#include "LString.h"
#include <locale>
#include <boost/scoped_ptr.hpp>
///
class Messages {
public:
///
typedef std::messages<char>::catalog catalog;
///
Messages(string const & l, string const & dir);
///
~Messages();
///
string const get(string const & msg) const;
///
string const & lang() const {
return lang_;
}
///
string const & localedir() const {
return localedir_;
}
private:
///
string lang_;
///
string localedir_;
///
std::locale loc_gl;
///
std::messages<char> const & mssg_gl;
///
catalog cat_gl;
class Pimpl;
boost::scoped_ptr<Pimpl> pimpl_;
};
#endif