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