src/FbTk/I18n.cc (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
// I18n.hh for Fluxbox Window Manager // Copyright (c) 2001 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org) // // I18n.cc for Blackbox - an X11 Window manager // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net) // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. /* Note: * A good reference for the older non-gettext style I18n * functions is the "Locale tutorial" * Written by Patrick D'Cruze (pdcruze@orac.iinet.com.au) * A copy of which is available (at the time of writing) here: * http://www.kulichki.com/moshkow/CYRILLIC/locale-tutorial-0_8.txt */ #include "I18n.hh" #include "FileUtil.hh" #include <X11/Xlocale.h> #include <cstdlib> #include <cstring> #include <cstdarg> #include <iostream> #ifdef HAVE_LOCALE_H #include <locale.h> #endif // HAVE_LOCALE_H #ifdef HAVE_NL_TYPES_H // this is needed for linux libc5 systems extern "C" { #include <nl_types.h> } #elif defined(__CYGWIN__) || defined(__EMX__) || defined(__APPLE__) extern "C" { typedef int nl_catd; char *catgets(nl_catd cat, int set_number, int message_number, char *message); nl_catd catopen(char *name, int flag); void catclose(nl_catd cat); } #endif // HAVE_NL_TYPES_H using std::cerr; using std::endl; using std::string; namespace { #if defined(NLS) const char UTF8_SUFFIX[] = "-UTF-8.cat"; const size_t UTF8_SUFFIX_LEN = sizeof(UTF8_SUFFIX)-1; // without \0 const char DEFAULT_CATFILE[] = "fluxbox.cat"; const char ENV_CATFILE[] = "FLUXBOX_CATFILE"; const char ENV_CATDIR[] = "FLUXBOX_CATDIR"; const char* getCatalogDir() { const char* cat_dir = getenv(ENV_CATDIR); if (cat_dir) { return cat_dir; } return LOCALEPATH; } #endif const nl_catd INVALID_CATALOG = (nl_catd)(-1); nl_catd s_catalog_fd = INVALID_CATALOG; std::string join_str(size_t n, ...) { std::string s; va_list args; va_start(args, n); for (; n > 0; n--) { s.append(va_arg(args, const char*)); } return s; } } namespace FbTk { // initialize the i18n-system be opening the catalog-file // named by 'catalog'. per default we expect 'catalog' to // be 0/NULL, the code picks a sane default then: // // - environment variable FLUXBOX_CATFILE is set? use it // - DEFAULT_CATFILE ("fluxbox.cat") // - the utf8 encoded translation for the current locale // // handling things this was allows us to test catalog files // without putting them into the install path // $PREFIX/share/fluxbox/nls/XYZ/ void I18n::init(const char* catalog) { static bool init = false; if (init) { return; } #if defined(NLS) && defined(HAVE_CATOPEN) if (!catalog) { const char* c = getenv(ENV_CATFILE); if (!c) { c = DEFAULT_CATFILE; } catalog = c; } FbStringUtil::init(); int flag; I18n& i18n = I18n::instance(); const string dir = getCatalogDir(); const string locale = i18n.m_locale; string clean_locale = locale; size_t i; // clean the locale, we have to append something later on i = clean_locale.find('.'); if (i != string::npos) clean_locale.erase(i); #ifdef MCLoadBySet flag = MCLoadBySet; #else flag = NL_CAT_LOCALE; #endif struct { std::string catalog; std::string locale; bool utf8; } _catalog[] = { // first try pure 'catalog'. catopen() will use NLSPATH if it's // set and replaces '%N' by 'catalog'. eg: with catalog="fluxbox.cat" // "/usr/share/fluxbox/nls/C/%N" becomes "/usr/share/fluxbox/nls/C/fluxbox.cat" { string(catalog), locale, false }, // try full-path to 'catalog' { join_str(5, dir.c_str(), "/", locale.c_str(), "/", catalog), locale, false }, // try the UTF-8 catalog, this also picks up situations where // the codeset somehow isn't specified { join_str(5, dir.c_str(), "/", clean_locale.c_str(), ".UTF-8/", catalog), join_str(2, clean_locale.c_str(), ".UTF8"), true}, }; for (i = 0; i < sizeof(_catalog)/sizeof(_catalog[0]); i++) { if (_catalog[i].utf8 && locale == "C") { continue; } const char* fname = _catalog[i].catalog.c_str(); s_catalog_fd = catopen(fname, flag); if (s_catalog_fd == INVALID_CATALOG) { continue; } i18n.m_locale = _catalog[i].locale; if (FbStringUtil::haveUTF8()) { if (_catalog[i].utf8) { i18n.m_utf8_translate = true; } else { size_t n = _catalog[i].catalog.rfind(UTF8_SUFFIX); if (n != std::string::npos && (n + UTF8_SUFFIX_LEN) == _catalog[i].catalog.size()) { i18n.m_utf8_translate = true; } } } break; } if (s_catalog_fd == INVALID_CATALOG) { cerr<<"Warning: Failed to open file("<< catalog <<")"<<endl <<"for translation, using default messages."<<endl; } #endif // HAVE_CATOPEN } I18n::I18n() : m_multibyte(false), m_utf8_translate(false) { #if defined(HAVE_SETLOCALE) && defined(NLS) //make sure we don't get 0 to m_locale string char *temp = setlocale(LC_MESSAGES, ""); m_locale = ( temp ? temp : ""); if (m_locale.empty()) { cerr<<"Warning: Failed to set locale, reverting to \"C\""<<endl; #endif // defined(HAVE_SETLOCALE) && defined(NLS) m_locale = "C"; #if defined(HAVE_SETLOCALE) && defined(NLS) } else { setlocale(LC_TIME, ""); // MB_CUR_MAX returns the size of a char in the current locale if (MB_CUR_MAX > 1) m_multibyte = true; // truncate any encoding off the end of the locale string::size_type index = m_locale.find('@'); if (index != string::npos) m_locale.erase(index); index = m_locale.find('='); if (index != string::npos) m_locale.erase(0, index+1); } #endif // defined(HAVE_SETLOCALE) && defined(NLS) } I18n::~I18n() { #if defined(NLS) && defined(HAVE_CATCLOSE) if (s_catalog_fd != INVALID_CATALOG) catclose(s_catalog_fd); #endif // HAVE_CATCLOSE } I18n& I18n::instance() { static I18n singleton; //singleton object return singleton; } // Translate_FB means it'll become an FbString that goes to X for Fonts, // No translate means it stays in the local encoding, for printing to the // console. FbString I18n::getMessage(int set_number, int message_number, const char *default_message, bool translate_fb) const { FbString msg(default_message); #if defined(NLS) && defined(HAVE_CATGETS) if (s_catalog_fd != INVALID_CATALOG) { const char *ret = catgets(s_catalog_fd, set_number, message_number, default_message); if (ret == default_message || ret == NULL) { // can't translate, leave it in raw ascii (utf-8 compatible) } else if (!m_utf8_translate && translate_fb) // Local input, UTF-8 output msg = FbStringUtil::LocaleStrToFb(ret); else if (m_utf8_translate && !translate_fb) // UTF-8 input, local output msg = FbStringUtil::FbStrToLocale(ret); else // UTF-8 input, UTF-8 output OR // local input, local output msg = ret; } #endif // NLS && HAVE_CATGETS return msg; } } // end namespace FbTk |