+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>
#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;
-void locale_init()
-{
-# ifdef HAVE_LC_MESSAGES
- setlocale(LC_MESSAGES, "");
-# endif
- setlocale(LC_CTYPE, "");
- setlocale(LC_NUMERIC, "C");
+ return lyx_messages->get(str);
}
void gettext_init(string const & localedir)
{
- bindtextdomain(PACKAGE, localedir.c_str());
- textdomain(PACKAGE);
+ lyx_messages.reset(new Messages("", localedir));
}
-#else // ENABLE_NLS
+#ifdef ENABLE_NLS
void locale_init()
{
+# ifdef HAVE_LC_MESSAGES
+ setlocale(LC_MESSAGES, "");
+# endif
+ setlocale(LC_CTYPE, "");
setlocale(LC_NUMERIC, "C");
}
+#else // ENABLE_NLS
-void gettext_init(string const &)
+void locale_init()
{
+ setlocale(LC_NUMERIC, "C");
}
+
#endif
#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
#include "messages.h"
#include "debug.h"
-using std::endl;
+#ifdef ENABLE_NLS
-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;
+#if 0
- cat_gl = mssg_gl.open("lyx", loc_gl, localedir_.c_str());
-}
+#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)
+ : 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);
}
#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