diff options
author | rathnor <rathnor> | 2004-06-07 11:46:05 (GMT) |
---|---|---|
committer | rathnor <rathnor> | 2004-06-07 11:46:05 (GMT) |
commit | fff4456dee29e675d7f2ed3490db39bcb7e10e53 (patch) | |
tree | 2d2dbf386551773cbdc8231b2a93b493187bd733 /src/FbTk/I18n.cc | |
parent | 073065ac56b388db1169108d44f37d32f1d19c67 (diff) | |
download | fluxbox_pavel-fff4456dee29e675d7f2ed3490db39bcb7e10e53.zip fluxbox_pavel-fff4456dee29e675d7f2ed3490db39bcb7e10e53.tar.bz2 |
update NLS string handling...
Diffstat (limited to 'src/FbTk/I18n.cc')
-rw-r--r-- | src/FbTk/I18n.cc | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/src/FbTk/I18n.cc b/src/FbTk/I18n.cc new file mode 100644 index 0000000..caffc6b --- /dev/null +++ b/src/FbTk/I18n.cc | |||
@@ -0,0 +1,153 @@ | |||
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.1 2004/06/07 11:46:05 rathnor Exp $ | ||
26 | |||
27 | /* Note: | ||
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 | ||
33 | */ | ||
34 | |||
35 | //usr GNU extensions | ||
36 | #ifndef _GNU_SOURCE | ||
37 | #define _GNU_SOURCE | ||
38 | #endif // _GNU_SOURCE | ||
39 | |||
40 | #include "I18n.hh" | ||
41 | |||
42 | #include <X11/Xlocale.h> | ||
43 | |||
44 | #include <cstdlib> | ||
45 | #include <cstring> | ||
46 | #include <cstdio> | ||
47 | |||
48 | #include <iostream> | ||
49 | |||
50 | // TODO: FIXME: the LOCALEPATH from this file should be ./configure-ed into config.h | ||
51 | #include "../defaults.hh" | ||
52 | |||
53 | using std::cerr; | ||
54 | using std::endl; | ||
55 | using std::string; | ||
56 | |||
57 | namespace FbTk { | ||
58 | |||
59 | void NLSInit(const char *catalog) { | ||
60 | I18n *i18n = I18n::instance(); | ||
61 | i18n->openCatalog(catalog); | ||
62 | } | ||
63 | |||
64 | |||
65 | I18n::I18n():m_multibyte(false), m_catalog_fd((nl_catd)(-1)) { | ||
66 | #ifdef HAVE_SETLOCALE | ||
67 | //make sure we don't get 0 to m_locale string | ||
68 | char *temp = setlocale(LC_ALL, ""); | ||
69 | m_locale = ( temp ? temp : ""); | ||
70 | if (m_locale.empty()) { | ||
71 | cerr<<"Warning: Failed to set locale, reverting to \"C\""<<endl; | ||
72 | #endif // HAVE_SETLOCALE | ||
73 | |||
74 | m_locale = "C"; | ||
75 | |||
76 | #ifdef HAVE_SETLOCALE | ||
77 | |||
78 | } else { | ||
79 | // MB_CUR_MAX returns the size of a char in the current locale | ||
80 | if (MB_CUR_MAX > 1) | ||
81 | m_multibyte = true; | ||
82 | |||
83 | // truncate any encoding off the end of the locale | ||
84 | |||
85 | // remove everything after @ | ||
86 | string::size_type index = m_locale.find('@'); | ||
87 | if (index != string::npos) | ||
88 | m_locale.erase(index); //erase all characters starting at index | ||
89 | // remove everything after . | ||
90 | index = m_locale.find('.'); | ||
91 | if (index != string::npos) | ||
92 | m_locale.erase(index); //erase all characters starting at index | ||
93 | // remove everything before = | ||
94 | index = m_locale.find('='); | ||
95 | if (index != string::npos) | ||
96 | m_locale.erase(0,index+1); //erase all characters starting up to index | ||
97 | } | ||
98 | #endif // HAVE_SETLOCALE | ||
99 | } | ||
100 | |||
101 | |||
102 | I18n::~I18n() { | ||
103 | |||
104 | #if defined(NLS) && defined(HAVE_CATCLOSE) | ||
105 | if (m_catalog_fd != (nl_catd)-1) | ||
106 | catclose(m_catalog_fd); | ||
107 | #endif // HAVE_CATCLOSE | ||
108 | } | ||
109 | |||
110 | I18n *I18n::instance() { | ||
111 | static I18n singleton; //singleton object | ||
112 | return &singleton; | ||
113 | } | ||
114 | |||
115 | void I18n::openCatalog(const char *catalog) { | ||
116 | #if defined(NLS) && defined(HAVE_CATOPEN) | ||
117 | |||
118 | string catalog_filename = LOCALEPATH; | ||
119 | catalog_filename += '/'; | ||
120 | catalog_filename += m_locale; | ||
121 | catalog_filename += '/'; | ||
122 | catalog_filename += catalog; | ||
123 | |||
124 | #ifdef MCLoadBySet | ||
125 | m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet); | ||
126 | #else // !MCLoadBySet | ||
127 | m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE); | ||
128 | #endif // MCLoadBySet | ||
129 | |||
130 | if (m_catalog_fd == (nl_catd)-1) { | ||
131 | cerr<<"Warning: Failed to open file("<<catalog_filename<<")"<<endl; | ||
132 | cerr<<"for translation, using default messages."<<endl; | ||
133 | } | ||
134 | |||
135 | #else // !HAVE_CATOPEN | ||
136 | |||
137 | m_catalog_fd = (nl_catd)-1; | ||
138 | #endif // HAVE_CATOPEN | ||
139 | } | ||
140 | |||
141 | |||
142 | const char *I18n::getMessage(int set_number, int message_number, | ||
143 | const char *default_message) const { | ||
144 | |||
145 | #if defined(NLS) && defined(HAVE_CATGETS) | ||
146 | if (m_catalog_fd != (nl_catd)-1) | ||
147 | return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message); | ||
148 | else | ||
149 | #endif // NLS && HAVE_CATGETS | ||
150 | return default_message; | ||
151 | } | ||
152 | |||
153 | }; // end namespace FbTk | ||