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