diff options
Diffstat (limited to 'src/I18n.cc')
-rw-r--r-- | src/I18n.cc | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/I18n.cc b/src/I18n.cc new file mode 100644 index 0000000..1d3cf8b --- /dev/null +++ b/src/I18n.cc | |||
@@ -0,0 +1,137 @@ | |||
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 2003/06/12 14:24:26 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 | #ifdef HAVE_LOCALE_H | ||
41 | #include <locale.h> | ||
42 | #endif // HAVE_LOCALE_H | ||
43 | |||
44 | #include <iostream> | ||
45 | using std::cerr; | ||
46 | using std::endl; | ||
47 | using std::string; | ||
48 | |||
49 | void NLSInit(const char *catalog) { | ||
50 | I18n *i18n = I18n::instance(); | ||
51 | i18n->openCatalog(catalog); | ||
52 | } | ||
53 | |||
54 | |||
55 | I18n::I18n():m_multibyte(false), m_catalog_fd((nl_catd)(-1)) { | ||
56 | #ifdef HAVE_SETLOCALE | ||
57 | //make sure we don't get 0 to m_locale string | ||
58 | char *temp = setlocale(LC_ALL, ""); | ||
59 | m_locale = ( temp ? temp : ""); | ||
60 | if (m_locale.size() == 0) { | ||
61 | cerr<<"Warning: Failed to set locale, reverting to \"C\""<<endl; | ||
62 | #endif // HAVE_SETLOCALE | ||
63 | |||
64 | m_locale = "C"; | ||
65 | |||
66 | #ifdef HAVE_SETLOCALE | ||
67 | |||
68 | } else { | ||
69 | // MB_CUR_MAX returns the size of a char in the current locale | ||
70 | if (MB_CUR_MAX > 1) | ||
71 | m_multibyte = true; | ||
72 | |||
73 | // truncate any encoding off the end of the locale | ||
74 | |||
75 | // remove everything after @ | ||
76 | string::size_type index = m_locale.find('@'); | ||
77 | if (index != string::npos) | ||
78 | m_locale.erase(index); //erase all characters starting at index | ||
79 | // remove everything after . | ||
80 | index = m_locale.find('.'); | ||
81 | if (index != string::npos) | ||
82 | m_locale.erase(index); //erase all characters starting at index | ||
83 | } | ||
84 | #endif // HAVE_SETLOCALE | ||
85 | } | ||
86 | |||
87 | |||
88 | I18n::~I18n() { | ||
89 | |||
90 | #if defined(NLS) && defined(HAVE_CATCLOSE) | ||
91 | if (m_catalog_fd != (nl_catd)-1) | ||
92 | catclose(m_catalog_fd); | ||
93 | #endif // HAVE_CATCLOSE | ||
94 | } | ||
95 | |||
96 | I18n *I18n::instance() { | ||
97 | static I18n singleton; //singleton object | ||
98 | return &singleton; | ||
99 | } | ||
100 | |||
101 | void I18n::openCatalog(const char *catalog) { | ||
102 | #if defined(NLS) && defined(HAVE_CATOPEN) | ||
103 | |||
104 | string catalog_filename = LOCALEPATH; | ||
105 | catalog_filename += '/'; | ||
106 | catalog_filename += m_locale; | ||
107 | catalog_filename += '/'; | ||
108 | catalog_filename += catalog; | ||
109 | |||
110 | #ifdef MCLoadBySet | ||
111 | m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet); | ||
112 | #else // !MCLoadBySet | ||
113 | m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE); | ||
114 | #endif // MCLoadBySet | ||
115 | |||
116 | if (m_catalog_fd == (nl_catd)-1) { | ||
117 | cerr<<"Warning: Failed to open file("<<catalog_filename<<")"<<endl; | ||
118 | cerr<<"for translation, using default messages."<<endl; | ||
119 | } | ||
120 | |||
121 | #else // !HAVE_CATOPEN | ||
122 | |||
123 | m_catalog_fd = (nl_catd)-1; | ||
124 | #endif // HAVE_CATOPEN | ||
125 | } | ||
126 | |||
127 | |||
128 | const char *I18n::getMessage(int set_number, int message_number, | ||
129 | const char *default_message) { | ||
130 | |||
131 | #if defined(NLS) && defined(HAVE_CATGETS) | ||
132 | if (m_catalog_fd != (nl_catd)-1) | ||
133 | return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message); | ||
134 | else | ||
135 | #endif // NLS && HAVE_CATGETS | ||
136 | return default_message; | ||
137 | } | ||