aboutsummaryrefslogtreecommitdiff
path: root/src/I18n.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/I18n.cc')
-rw-r--r--src/I18n.cc136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/I18n.cc b/src/I18n.cc
new file mode 100644
index 0000000..c28e478
--- /dev/null
+++ b/src/I18n.cc
@@ -0,0 +1,136 @@
1// I18n.hh for Fluxbox Window Manager
2// Copyright (c) 2001 - 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net)
3//
4// I18n.cc for Blackbox - an X11 Window manager
5// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
6//
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:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
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.
24
25// $Id: I18n.cc,v 1.4 2003/12/08 17:29:24 fluxgen Exp $
26
27//usr GNU extensions
28#ifndef _GNU_SOURCE
29#define _GNU_SOURCE
30#endif // _GNU_SOURCE
31
32#include "I18n.hh"
33
34#include <X11/Xlocale.h>
35
36#include <cstdlib>
37#include <cstring>
38#include <cstdio>
39
40#include <iostream>
41
42#include "defaults.hh"
43
44using std::cerr;
45using std::endl;
46using std::string;
47
48void NLSInit(const char *catalog) {
49 I18n *i18n = I18n::instance();
50 i18n->openCatalog(catalog);
51}
52
53
54I18n::I18n():m_multibyte(false), m_catalog_fd((nl_catd)(-1)) {
55#ifdef HAVE_SETLOCALE
56 //make sure we don't get 0 to m_locale string
57 char *temp = setlocale(LC_ALL, "");
58 m_locale = ( temp ? temp : "");
59 if (m_locale.size() == 0) {
60 cerr<<"Warning: Failed to set locale, reverting to \"C\""<<endl;
61#endif // HAVE_SETLOCALE
62
63 m_locale = "C";
64
65#ifdef HAVE_SETLOCALE
66
67 } else {
68 // MB_CUR_MAX returns the size of a char in the current locale
69 if (MB_CUR_MAX > 1)
70 m_multibyte = true;
71
72 // truncate any encoding off the end of the locale
73
74 // remove everything after @
75 string::size_type index = m_locale.find('@');
76 if (index != string::npos)
77 m_locale.erase(index); //erase all characters starting at index
78 // remove everything after .
79 index = m_locale.find('.');
80 if (index != string::npos)
81 m_locale.erase(index); //erase all characters starting at index
82 }
83#endif // HAVE_SETLOCALE
84}
85
86
87I18n::~I18n() {
88
89#if defined(NLS) && defined(HAVE_CATCLOSE)
90 if (m_catalog_fd != (nl_catd)-1)
91 catclose(m_catalog_fd);
92#endif // HAVE_CATCLOSE
93}
94
95I18n *I18n::instance() {
96 static I18n singleton; //singleton object
97 return &singleton;
98}
99
100void I18n::openCatalog(const char *catalog) {
101#if defined(NLS) && defined(HAVE_CATOPEN)
102
103 string catalog_filename = LOCALEPATH;
104 catalog_filename += '/';
105 catalog_filename += m_locale;
106 catalog_filename += '/';
107 catalog_filename += catalog;
108
109#ifdef MCLoadBySet
110 m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet);
111#else // !MCLoadBySet
112 m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE);
113#endif // MCLoadBySet
114
115 if (m_catalog_fd == (nl_catd)-1) {
116 cerr<<"Warning: Failed to open file("<<catalog_filename<<")"<<endl;
117 cerr<<"for translation, using default messages."<<endl;
118 }
119
120#else // !HAVE_CATOPEN
121
122 m_catalog_fd = (nl_catd)-1;
123#endif // HAVE_CATOPEN
124}
125
126
127const char *I18n::getMessage(int set_number, int message_number,
128 const char *default_message) const {
129
130#if defined(NLS) && defined(HAVE_CATGETS)
131 if (m_catalog_fd != (nl_catd)-1)
132 return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message);
133 else
134#endif // NLS && HAVE_CATGETS
135 return default_message;
136}