]> git.lyx.org Git - lyx.git/blob - src/messages.C
7c9db6142bd1c0bdf1c84f01198935838664e532
[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[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
95                                      << ": language(" << lang_ << ")" << std::endl;
96         }
97
98         ~Pimpl() {}
99
100         string const get(string const & m) const
101         {
102                 if (m.empty())
103                         return m;
104
105                 //string oldMSG = setlocale(LC_MESSAGES, NULL);
106                 // In this order, see support/filetools.C:
107                 string lang = getEnv("LC_ALL");
108                 if (lang.empty()) {
109                         lang = getEnv("LC_MESSAGES");
110                         if (lang.empty()) {
111                                 lang = getEnv("LANG");
112                                 if (lang.empty())
113                                         lang = "C";
114                         }
115                 }
116                 
117                 char const * works = setlocale(LC_MESSAGES, lang_.c_str());
118                 // CTYPE controls what getmessage thinks what encoding the po file uses
119                 string oldCTYPE = setlocale(LC_CTYPE, NULL);
120                 setlocale(LC_CTYPE, lang_.c_str());
121                 errno = 0;
122                 char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str());
123                 int e = errno;
124                 if (e) {
125                         lyxerr[Debug::DEBUG]
126                                 << BOOST_CURRENT_FUNCTION << '\n'
127                                 << "Error code: " << errno << '\n'
128                                 << "Lang, mess: " << lang_ << " " << m << '\n'
129                                 << "Directory : " << package().locale_dir() << '\n'
130                                 << "Rtn value : " << c << std::endl;
131                 }
132                 textdomain(PACKAGE);
133                 const char* msg = gettext(m.c_str());
134                 string translated(works ? msg : m);
135                 // Some english words have different translations, depending
136                 // on context. In these cases the original string is
137                 // augmented by context information (e.g.
138                 // "To:[[as in 'From page x to page y']]" and
139                 // "To:[[as in 'From format x to format y']]".
140                 // This means that we need to filter out everything in
141                 // double square brackets at the end of the string,
142                 // otherwise the user sees bogus messages.
143                 // If we are unable to honour the request we just
144                 // return what we got in.
145                 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
146                 boost::smatch sub;
147                 if (regex_match(translated, sub, reg))
148                         translated = sub.str(1);
149                 setlocale(LC_MESSAGES, lang.c_str());
150                 setlocale(LC_CTYPE, oldCTYPE.c_str());
151                 return translated;
152         }
153 private:
154         ///
155         string lang_;
156 };
157 #endif
158
159 #else // ENABLE_NLS
160 // This is the dummy variant.
161 class Messages::Pimpl {
162 public:
163         Pimpl(string const &) {}
164
165         ~Pimpl() {}
166
167         string const get(string const & m) const
168         {
169                 return m;
170         }
171 };
172 #endif
173
174
175 Messages::Messages()
176         : pimpl_(new Pimpl(""))
177 {}
178
179
180 Messages::Messages(string const & l)
181         : pimpl_(new Pimpl(l))
182 {}
183
184
185 // We need this for the sake of scoped_ptr
186 Messages::~Messages()
187 {}
188
189
190 string const Messages::get(string const & msg) const
191 {
192         return pimpl_->get(msg);
193 }