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