1
// I18n.hh for Fluxbox Window Manager
2
// Copyright (c) 2001 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
4
// I18n.cc for Blackbox - an X11 Window manager
5
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
7
// Permission is hereby granted, free of charge, to any person obtaining a
8
// copy of this software and associated documentation files (the "Software"),
9
// to deal in the Software without restriction, including without limitation
10
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
// and/or sell copies of the Software, and to permit persons to whom the
12
// Software is furnished to do so, subject to the following conditions:
14
// The above copyright notice and this permission notice shall be included in
15
// all copies or substantial portions of the Software.
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
// DEALINGS IN THE SOFTWARE.
25
// $Id: I18n.cc 4026 2005-05-18 08:32:17Z fluxgen $
28
* A good reference for the older non-gettext style I18n
29
* functions is the "Locale tutorial"
30
* Written by Patrick D'Cruze (pdcruze@orac.iinet.com.au)
31
* A copy of which is available (at the time of writing) here:
32
* http://www.kulichki.com/moshkow/CYRILLIC/locale-tutorial-0_8.txt
42
#include <X11/Xlocale.h>
68
void NLSInit(const char *catalog) {
69
I18n *i18n = I18n::instance();
70
i18n->openCatalog(catalog);
74
I18n::I18n():m_multibyte(false), m_catalog_fd((nl_catd)(-1)) {
76
//make sure we don't get 0 to m_locale string
77
char *temp = setlocale(LC_MESSAGES, "");
78
m_locale = ( temp ? temp : "");
79
if (m_locale.empty()) {
80
cerr<<"Warning: Failed to set locale, reverting to \"C\""<<endl;
81
#endif // HAVE_SETLOCALE
89
setlocale(LC_TIME, "");
90
// MB_CUR_MAX returns the size of a char in the current locale
94
// truncate any encoding off the end of the locale
96
// remove everything after @
97
string::size_type index = m_locale.find('@');
98
if (index != string::npos)
99
m_locale.erase(index); //erase all characters starting at index
100
// remove everything after .
101
index = m_locale.find('.');
102
if (index != string::npos)
103
m_locale.erase(index); //erase all characters starting at index
104
// remove everything before =
105
index = m_locale.find('=');
106
if (index != string::npos)
107
m_locale.erase(0,index+1); //erase all characters starting up to index
109
#endif // HAVE_SETLOCALE
115
#if defined(NLS) && defined(HAVE_CATCLOSE)
116
if (m_catalog_fd != (nl_catd)-1)
117
catclose(m_catalog_fd);
118
#endif // HAVE_CATCLOSE
121
I18n *I18n::instance() {
122
static I18n singleton; //singleton object
126
void I18n::openCatalog(const char *catalog) {
127
#if defined(NLS) && defined(HAVE_CATOPEN)
129
string catalog_filename = LOCALEPATH;
130
catalog_filename += '/';
131
catalog_filename += m_locale;
132
catalog_filename += '/';
133
catalog_filename += catalog;
136
m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet);
137
#else // !MCLoadBySet
138
m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE);
139
#endif // MCLoadBySet
141
if (m_catalog_fd == (nl_catd)-1) {
142
cerr<<"Warning: Failed to open file("<<catalog_filename<<")"<<endl;
143
cerr<<"for translation, using default messages."<<endl;
146
#else // !HAVE_CATOPEN
148
m_catalog_fd = (nl_catd)-1;
149
#endif // HAVE_CATOPEN
153
const char *I18n::getMessage(int set_number, int message_number,
154
const char *default_message) const {
156
#if defined(NLS) && defined(HAVE_CATGETS)
157
if (m_catalog_fd != (nl_catd)-1)
158
return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message);
160
#endif // NLS && HAVE_CATGETS
161
return default_message;
164
}; // end namespace FbTk