]> git.lyx.org Git - lyx.git/blob - src/messages.C
Point fix, earlier forgotten
[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 #include "support/filetools.h"
15 #include "support/path_defines.h"
16
17 using namespace lyx::support;
18
19
20 #ifdef ENABLE_NLS
21
22 namespace {
23
24 string const & getLocaleDir()
25 {
26         static string locale_dir;
27
28         if (locale_dir.empty()) {
29                 locale_dir = GetEnvPath("LYX_LOCALEDIR");
30                 if (locale_dir.empty())
31                         locale_dir = lyx_localedir();
32         }
33         return locale_dir;
34 }
35
36 } // anon namespace
37
38 #if 0
39
40 #include <locale>
41
42 // This version of the Pimpl utilizes the message capability of
43 // libstdc++ that is distributed with GNU G++.
44 class Messages::Pimpl {
45 public:
46         typedef std::messages<char>::catalog catalog;
47
48         Pimpl(string const & l)
49                 : lang_(l),
50                   loc_gl(lang_.c_str()),
51                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
52         {
53                 //lyxerr << "Messages: language(" << l
54                 //       << ") in dir(" << dir << ")" << std::endl;
55
56                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, getLocaleDir().c_str());
57
58         }
59
60         ~Pimpl()
61         {
62                 mssg_gl.close(cat_gl);
63         }
64
65         string const get(string const & msg) const
66         {
67                 return mssg_gl.get(cat_gl, 0, 0, msg);
68         }
69 private:
70         ///
71         string lang_;
72         ///
73         std::locale loc_gl;
74         ///
75         std::messages<char> const & mssg_gl;
76         ///
77         catalog cat_gl;
78 };
79 #else
80
81 #ifdef HAVE_LOCALE_H
82 #  include <locale.h>
83 #endif
84
85 #  if HAVE_GETTEXT
86 #    include <libintl.h>      // use the header already in the system *EK*
87 #  else
88 #    include "../intl/libintl.h"
89 #  endif
90
91 // This is a more traditional variant.
92 class Messages::Pimpl {
93 public:
94         Pimpl(string const & l)
95                 : lang_(l)
96         {
97                 //lyxerr << "Messages: language(" << l
98                 //       << ") in dir(" << dir << ")" << std::endl;
99
100               bindtextdomain(PACKAGE, getLocaleDir().c_str());
101               textdomain(PACKAGE);
102         }
103
104         ~Pimpl() {}
105
106         string const get(string const & m) const
107         {
108                 if (m.empty())
109                         return m;
110
111                 char * old = strdup(setlocale(LC_ALL, 0));
112                 char * n = setlocale(LC_ALL, lang_.c_str());
113                 const char* msg = gettext(m.c_str());
114                 setlocale(LC_ALL, old);
115                 free(old);
116                 // If we are unable to honour the request we just
117                 // return what we got in.
118                 return (!n ? m : string(msg));
119         }
120 private:
121         ///
122         string lang_;
123 };
124 #endif
125
126 #else // ENABLE_NLS
127 // This is the dummy variant.
128 class Messages::Pimpl {
129 public:
130         Pimpl(string const &) {}
131
132         ~Pimpl() {}
133
134         string const get(string const & m) const
135         {
136                 return m;
137         }
138 };
139 #endif
140
141
142 Messages::Messages()
143         : pimpl_(new Pimpl(""))
144 {}
145
146
147 Messages::Messages(string const & l)
148         : pimpl_(new Pimpl(l))
149 {}
150
151
152 // We need this for the sake of scoped_ptr
153 Messages::~Messages()
154 {}
155
156
157 string const Messages::get(string const & msg) const
158 {
159         return pimpl_->get(msg);
160 }