]> git.lyx.org Git - features.git/commitdiff
More messages work. Should now also
authorLars Gullik Bjønnes <larsbj@gullik.org>
Thu, 24 Apr 2003 22:09:46 +0000 (22:09 +0000)
committerLars Gullik Bjønnes <larsbj@gullik.org>
Thu, 24 Apr 2003 22:09:46 +0000 (22:09 +0000)
compile on some older compilers.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6848 a592a061-630c-0410-9148-cb99ea01b6c8

src/ChangeLog
src/gettext.C
src/gettext.h
src/messages.C
src/messages.h

index 6e015bef1064ae863b82aadbfda5c891bb0d40bc..9a401bef268d12706cff0a2850dbf3d8b5c1283c 100644 (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>
 
index 1712961c45d324b6f501fd6f92d0133ac0781c16..2ad36ede3bc6b60653de37e8cb948793ec4bcc3b 100644 (file)
 
 #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
index 002d2bc563e818a58e51eb2394e452fd58fa5548..75e64d05db0a5f2cd0baaf260e771c8829c76613 100644 (file)
 
 #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
 
index 9966178e5a967eb7f33d23c29e918e83f8fe47ec..08019d27681a32064fa71239cf7004321e5345d1 100644 (file)
 #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);
 }
index 459b109c7453e2baae913db1e6c13af82c0952d1..7522abc5cfea429acbfc0a97f989b2fac653f648 100644 (file)
 
 #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