]> git.lyx.org Git - lyx.git/blob - src/messages.C
- Link against qt-mt333.lib which is what the current qt3 cvs produces
[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/package.h"
15
16 #include <boost/regex.hpp>
17
18 using lyx::support::package;
19
20 using std::string;
21
22
23 #ifdef ENABLE_NLS
24
25
26 #if 0
27
28 -#include <locale>
29
30 // This version of the Pimpl utilizes the message capability of
31 // libstdc++ that is distributed with GNU G++.
32 class Messages::Pimpl {
33 public:
34         typedef std::messages<char>::catalog catalog;
35
36         Pimpl(string const & l)
37                 : lang_(l),
38                   loc_gl(lang_.c_str()),
39                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
40         {
41                 //lyxerr << "Messages: language(" << l
42                 //       << ") in dir(" << dir << ")" << std::endl;
43
44                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
45
46         }
47
48         ~Pimpl()
49         {
50                 mssg_gl.close(cat_gl);
51         }
52
53         string const get(string const & msg) const
54         {
55                 return mssg_gl.get(cat_gl, 0, 0, msg);
56         }
57 private:
58         ///
59         string lang_;
60         ///
61         std::locale loc_gl;
62         ///
63         std::messages<char> const & mssg_gl;
64         ///
65         catalog cat_gl;
66 };
67 #else
68
69 #ifdef HAVE_LOCALE_H
70 #  include <locale.h>
71 #endif
72
73 #  if HAVE_GETTEXT
74 #    include <libintl.h>      // use the header already in the system *EK*
75 #  else
76 #    include "../intl/libintl.h"
77 #  endif
78
79 // This is a more traditional variant.
80 class Messages::Pimpl {
81 public:
82         Pimpl(string const & l)
83                 : lang_(l)
84         {
85                 //lyxerr << "Messages: language(" << l
86                 //       << ") in dir(" << dir << ")" << std::endl;
87
88         }
89
90         ~Pimpl() {}
91
92         string const get(string const & m) const
93         {
94                 if (m.empty())
95                         return m;
96
97                 char * old = strdup(setlocale(LC_ALL, 0));
98                 char * n = setlocale(LC_ALL, lang_.c_str());
99                 bindtextdomain(PACKAGE, package().locale_dir().c_str());
100                 textdomain(PACKAGE);
101                 const char* msg = gettext(m.c_str());
102                 // Some english words have different translations, depending
103                 // on context. In these cases the original string is
104                 // augmented by context information (e.g.
105                 // "To:[[as in 'From page x to page y']]" and
106                 // "To:[[as in 'From format x to format y']]".
107                 // This means that we need to filter out everything in
108                 // double square brackets at the end of the string,
109                 // otherwise the user sees bogus messages.
110                 // If we are unable to honour the request we just
111                 // return what we got in.
112                 string translated(n ? msg : m);
113                 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
114                 boost::smatch sub;
115                 if (regex_match(translated, sub, reg))
116                         translated = sub.str(1);
117                 setlocale(LC_ALL, old);
118                 free(old);
119                 return translated;
120         }
121 private:
122         ///
123         string lang_;
124 };
125 #endif
126
127 #else // ENABLE_NLS
128 // This is the dummy variant.
129 class Messages::Pimpl {
130 public:
131         Pimpl(string const &) {}
132
133         ~Pimpl() {}
134
135         string const get(string const & m) const
136         {
137                 return m;
138         }
139 };
140 #endif
141
142
143 Messages::Messages()
144         : pimpl_(new Pimpl(""))
145 {}
146
147
148 Messages::Messages(string const & l)
149         : pimpl_(new Pimpl(l))
150 {}
151
152
153 // We need this for the sake of scoped_ptr
154 Messages::~Messages()
155 {}
156
157
158 string const Messages::get(string const & msg) const
159 {
160         return pimpl_->get(msg);
161 }