]> git.lyx.org Git - lyx.git/blob - src/messages.C
466d9e2240ca8376e19b3866886adcd6a2eff460
[lyx.git] / src / messages.C
1 /* \file messages.C
2  * This file is part of LyX, the document processor.
3  * Licence details can be found in the file COPYING.
4  *
5  * \author Lars Gullik Bjønnes
6  *
7  * Full author contact details are available in file CREDITS
8  */
9
10 #include <config.h>
11
12 #include "messages.h"
13 #include "debug.h"
14
15
16 #ifdef ENABLE_NLS
17
18 #if 0
19
20 #include <locale>
21
22 // This version of the Pimpl utilizes the message capability of
23 // libstdc++ that is distributed with GNU G++
24 class Messages::Pimpl {
25 public:
26         typedef std::messages<char>::catalog catalog;
27
28         Pimpl(string const & l, string const & dir)
29                 : lang_(l), localedir_(dir),
30                   loc_gl(lang_.c_str()),
31                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
32         {
33                 //lyxerr << "Messages: language(" << l
34                 //       << ") in dir(" << dir << ")" << std::endl;
35
36                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, localedir_.c_str());
37
38         }
39
40         ~Pimpl()
41         {
42                 mssg_gl.close(cat_gl);
43         }
44
45         string const get(string const & msg) const
46         {
47                 return mssg_gl.get(cat_gl, 0, 0, msg);
48         }
49 private:
50         ///
51         string lang_;
52         ///
53         string localedir_;
54         ///
55         std::locale loc_gl;
56         ///
57         std::messages<char> const & mssg_gl;
58         ///
59         catalog cat_gl;
60 };
61 #else
62
63 #ifdef HAVE_LOCALE_H
64 #  include <locale.h>
65 #endif
66
67 #  if HAVE_GETTEXT
68 #    include <libintl.h>      // use the header already in the system *EK*
69 #  else
70 #    include "../intl/libintl.h"
71 #  endif
72
73 // This is a more traditional variant.
74 class Messages::Pimpl {
75 public:
76         Pimpl(string const & l, string const & dir)
77                 : lang_(l), localedir_(dir)
78         {
79                 //lyxerr << "Messages: language(" << l
80                 //       << ") in dir(" << dir << ")" << std::endl;
81
82               bindtextdomain(PACKAGE, localedir_.c_str());
83               textdomain(PACKAGE);
84         }
85
86         ~Pimpl() {}
87
88         string const get(string const & m) const
89         {
90                 char * old = strdup(setlocale(LC_ALL, 0));
91                 char * n = setlocale(LC_ALL, lang_.c_str());
92                 const char* msg = gettext(m.c_str());
93                 setlocale(LC_ALL, old);
94                 free(old);
95                 // If we are unable to honour the request we just
96                 // return what we got in.
97                 return (!n ? m : string(msg));
98         }
99 private:
100         ///
101         string lang_;
102         ///
103         string localedir_;
104 };
105 #endif
106
107 #else // ENABLE_NLS
108 // This is the dummy variant.
109 class Messages::Pimpl {
110 public:
111         Pimpl(string const &, string const &) {}
112
113         ~Pimpl() {}
114
115         string const get(string const & m) const
116         {
117                 return m;
118         }
119 };
120 #endif
121
122 Messages::Messages(string const & l, string const & dir)
123         : pimpl_(new Pimpl(l, dir))
124 {}
125
126
127 // We need this for the sake of scoped_ptr
128 Messages::~Messages()
129 {}
130
131
132 string const Messages::get(string const & msg) const
133 {
134         return pimpl_->get(msg);
135 }