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