Consider the case where there is not any layout name.
[lyx.git] / src / messages.C
blobd523af76262f059b4da67a05c4cb241d8e0dc51d
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  */
10 #include <config.h>
12 #include "messages.h"
13 #include "support/filetools.h"
14 #include "support/package.h"
16 #include <boost/regex.hpp>
18 using lyx::support::package;
20 using std::string;
23 #ifdef ENABLE_NLS
26 #if 0
28 -#include <locale>
30 // This version of the Pimpl utilizes the message capability of
31 // libstdc++ that is distributed with GNU G++.
32 class Messages::Pimpl {
33 public:
34         typedef std::messages<char>::catalog catalog;
36         Pimpl(string const & l)
37                 : lang_(l),
38                   loc_gl(lang_.c_str()),
39                   mssg_gl(std::use_facet<std::messages<char> >(loc_gl))
40         {
41                 //lyxerr << "Messages: language(" << l
42                 //       << ") in dir(" << dir << ")" << std::endl;
44                 cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str());
46         }
48         ~Pimpl()
49         {
50                 mssg_gl.close(cat_gl);
51         }
53         string const get(string const & msg) const
54         {
55                 return mssg_gl.get(cat_gl, 0, 0, msg);
56         }
57 private:
58         ///
59         string lang_;
60         ///
61         std::locale loc_gl;
62         ///
63         std::messages<char> const & mssg_gl;
64         ///
65         catalog cat_gl;
67 #else
69 #ifdef HAVE_LOCALE_H
70 #  include <locale.h>
71 #endif
73 #  if HAVE_GETTEXT
74 #    include <libintl.h>      // use the header already in the system *EK*
75 #  else
76 #    include "../intl/libintl.h"
77 #  endif
79 // This is a more traditional variant.
80 class Messages::Pimpl {
81 public:
82         Pimpl(string const & l)
83                 : lang_(l)
84         {
85                 //lyxerr << "Messages: language(" << l
86                 //       << ") in dir(" << dir << ")" << std::endl;
88         }
90         ~Pimpl() {}
92         string const get(string const & m) const
93         {
94                 if (m.empty())
95                         return m;
97                 char * old = strdup(setlocale(LC_ALL, 0));
98                 char * n = setlocale(LC_ALL, lang_.c_str());
99                 bindtextdomain(PACKAGE, package().locale_dir().c_str());
100                 textdomain(PACKAGE);
101                 const char* msg = gettext(m.c_str());
102                 // Some english words have different translations, depending
103                 // on context. In these cases the original string is
104                 // augmented by context information (e.g.
105                 // "To:[[as in 'From page x to page y']]" and
106                 // "To:[[as in 'From format x to format y']]".
107                 // This means that we need to filter out everything in
108                 // double square brackets at the end of the string,
109                 // otherwise the user sees bogus messages.
110                 // If we are unable to honour the request we just
111                 // return what we got in.
112                 string translated(n ? msg : m);
113                 static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
114                 boost::smatch sub;
115                 if (regex_match(translated, sub, reg))
116                         translated = sub.str(1);
117                 setlocale(LC_ALL, old);
118                 free(old);
119                 return translated;
120         }
121 private:
122         ///
123         string lang_;
125 #endif
127 #else // ENABLE_NLS
128 // This is the dummy variant.
129 class Messages::Pimpl {
130 public:
131         Pimpl(string const &) {}
133         ~Pimpl() {}
135         string const get(string const & m) const
136         {
137                 return m;
138         }
140 #endif
143 Messages::Messages()
144         : pimpl_(new Pimpl(""))
148 Messages::Messages(string const & l)
149         : pimpl_(new Pimpl(l))
153 // We need this for the sake of scoped_ptr
154 Messages::~Messages()
158 string const Messages::get(string const & msg) const
160         return pimpl_->get(msg);