]> git.lyx.org Git - lyx.git/blob - src/messages.C
Fix bug 886 and others not reported related with the document paper size.
[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/package.h"
16
17 #include <boost/regex.hpp>
18
19 using lyx::support::package;
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, package().locale_dir().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                 if ( lang_.empty() )
87                         lang_ = setlocale(LC_MESSAGES, NULL);
88                 lyxerr << "Messages: language(" << lang_
89                 //       << ") in dir(" << dir 
90                        << ")" << std::endl;
91                 
92         }
93
94         ~Pimpl() {}
95
96         string const get(string const & m) const
97         {
98                 if (m.empty())
99                         return m;
100
101                 string oldMSG = setlocale(LC_MESSAGES, NULL);
102                 bool works = setlocale(LC_MESSAGES, lang_.c_str());
103                 // CTYPE controls what getmessage thinks what encoding the po file uses
104                 string oldCTYPE = setlocale(LC_CTYPE, NULL);
105                 setlocale(LC_CTYPE, lang_.c_str());
106                 bindtextdomain(PACKAGE, package().locale_dir().c_str());
107                 textdomain(PACKAGE);
108                 const char* msg = gettext(m.c_str());
109                 string translated(works ? msg : m);
110                 // Some english words have different translations, depending
111                 // on context. In these cases the original string is
112                 // augmented by context information (e.g.
113                 // "To:[[as in 'From page x to page y']]" and
114                 // "To:[[as in 'From format x to format y']]".
115                 // This means that we need to filter out everything in
116                 // double square brackets at the end of the string,
117                 // otherwise the user sees bogus messages.
118                 // If we are unable to honour the request we just
119                 // return what we got in.
120                 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
121                 boost::smatch sub;
122                 if (regex_match(translated, sub, reg))
123                         translated = sub.str(1);
124                 setlocale(LC_MESSAGES, oldMSG.c_str());
125                 setlocale(LC_CTYPE, oldCTYPE.c_str());
126                 return translated;
127         }
128 private:
129         ///
130         string lang_;
131 };
132 #endif
133
134 #else // ENABLE_NLS
135 // This is the dummy variant.
136 class Messages::Pimpl {
137 public:
138         Pimpl(string const &) {}
139
140         ~Pimpl() {}
141
142         string const get(string const & m) const
143         {
144                 return m;
145         }
146 };
147 #endif
148
149
150 Messages::Messages()
151         : pimpl_(new Pimpl(""))
152 {}
153
154
155 Messages::Messages(string const & l)
156         : pimpl_(new Pimpl(l))
157 {}
158
159
160 // We need this for the sake of scoped_ptr
161 Messages::~Messages()
162 {}
163
164
165 string const Messages::get(string const & msg) const
166 {
167         return pimpl_->get(msg);
168 }