]> git.lyx.org Git - lyx.git/blob - src/messages.C
fix two crashes related to dEPM. Some crashes remain
[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 "debug.h"
13 #include "messages.h"
14 #include "support/filetools.h"
15 #include "support/environment.h"
16 #include "support/package.h"
17
18 #include <boost/current_function.hpp>
19 #include <boost/regex.hpp>
20
21 using lyx::support::package;
22 using lyx::support::getEnv;
23 using lyx::support::setEnv;
24
25 using std::string;
26
27
28 #ifdef ENABLE_NLS
29
30
31 #if 0
32
33 -#include <locale>
34
35 // This version of the Pimpl utilizes the message capability of
36 // libstdc++ that is distributed with GNU G++.
37 class Messages::Pimpl {
38 public:
39         typedef std::messages<char>::catalog catalog;
40
41         Pimpl(string const & l)
42                 : lang_(l),
43                   loc_gl(lang_.c_str()),
44                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
45         {
46                 //lyxerr << "Messages: language(" << l
47                 //       << ") in dir(" << dir << ")" << std::endl;
48
49                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
50
51         }
52
53         ~Pimpl()
54         {
55                 mssg_gl.close(cat_gl);
56         }
57
58         string const get(string const & msg) const
59         {
60                 return mssg_gl.get(cat_gl, 0, 0, msg);
61         }
62 private:
63         ///
64         string lang_;
65         ///
66         std::locale loc_gl;
67         ///
68         std::messages<char> const & mssg_gl;
69         ///
70         catalog cat_gl;
71 };
72 #else
73
74 #ifdef HAVE_LOCALE_H
75 #  include <locale.h>
76 #endif
77
78 #  if HAVE_GETTEXT
79 #    include <libintl.h>      // use the header already in the system *EK*
80 #  else
81 #    include "../intl/libintl.h"
82 #  endif
83
84 // This is a more traditional variant.
85 class Messages::Pimpl {
86 public:
87         Pimpl(string const & l)
88                 : lang_(l)
89         {
90                 if ( lang_.empty() )
91                         lang_ = setlocale(LC_MESSAGES, NULL);
92                 // strip off any encoding suffix, i.e., assume 8-bit po files
93                 string::size_type i = lang_.find(".");
94                 lang_ = lang_.substr(0, i);
95                 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
96                                      << ": language(" << lang_ << ")" << std::endl;
97         }
98
99         ~Pimpl() {}
100
101         string const get(string const & m) const
102         {
103                 if (m.empty())
104                         return m;
105
106                 //string oldMSG = setlocale(LC_MESSAGES, NULL);
107                 // In this order, see support/filetools.C:
108                 string lang = getEnv("LC_ALL");
109                 if (lang.empty()) {
110                         lang = getEnv("LC_MESSAGES");
111                         if (lang.empty()) {
112                                 lang = getEnv("LANG");
113                                 if (lang.empty())
114                                         lang = "C";
115                         }
116                 }
117                 
118                 char const * works = setlocale(LC_MESSAGES, lang_.c_str());
119                 if (!works)
120                         lyxerr << "Locale " << lang_ << " could not be set" << std::endl;
121                 // CTYPE controls what getmessage thinks what encoding the po file uses
122                 string oldCTYPE = setlocale(LC_CTYPE, NULL);
123                 setlocale(LC_CTYPE, lang_.c_str());
124                 errno = 0;
125                 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
126                 int e = errno;
127                 if (e) {
128                         lyxerr[Debug::DEBUG]
129                                 << BOOST_CURRENT_FUNCTION << '\n'
130                                 << "Error code: " << errno << '\n'
131                                 << "Lang, mess: " << lang_ << " " << m << '\n'
132                                 << "Directory : " << package().locale_dir() << '\n'
133                                 << "Rtn value : " << c << std::endl;
134                 }
135                 textdomain(PACKAGE);
136                 const char* msg = gettext(m.c_str());
137                 string translated(works ? msg : m);
138                 // Some english words have different translations, depending
139                 // on context. In these cases the original string is
140                 // augmented by context information (e.g.
141                 // "To:[[as in 'From page x to page y']]" and
142                 // "To:[[as in 'From format x to format y']]".
143                 // This means that we need to filter out everything in
144                 // double square brackets at the end of the string,
145                 // otherwise the user sees bogus messages.
146                 // If we are unable to honour the request we just
147                 // return what we got in.
148                 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
149                 boost::smatch sub;
150                 if (regex_match(translated, sub, reg))
151                         translated = sub.str(1);
152                 setlocale(LC_MESSAGES, lang.c_str());
153                 setlocale(LC_CTYPE, oldCTYPE.c_str());
154                 return translated;
155         }
156 private:
157         ///
158         string lang_;
159 };
160 #endif
161
162 #else // ENABLE_NLS
163 // This is the dummy variant.
164 class Messages::Pimpl {
165 public:
166         Pimpl(string const &) {}
167
168         ~Pimpl() {}
169
170         string const get(string const & m) const
171         {
172                 return m;
173         }
174 };
175 #endif
176
177
178 Messages::Messages()
179         : pimpl_(new Pimpl(""))
180 {}
181
182
183 Messages::Messages(string const & l)
184         : pimpl_(new Pimpl(l))
185 {}
186
187
188 // We need this for the sake of scoped_ptr
189 Messages::~Messages()
190 {}
191
192
193 string const Messages::get(string const & msg) const
194 {
195         return pimpl_->get(msg);
196 }