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