// 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 #ifdef HAVE_CSTDLIB #include #else #include #endif #ifdef HAVE_CSTRING #include #else #include #endif #ifdef HAVE_CSTDIO #include #else #include #endif #include using std::cerr; using std::endl; using std::string; namespace FbTk { void NLSInit(const char *catalog) { FbStringUtil::init(); I18n *i18n = I18n::instance(); i18n->openCatalog(catalog); } I18n::I18n():m_multibyte(false), m_utf8_translate(false), m_catalog_fd((nl_catd)(-1)) { #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\""< 1) m_multibyte = true; // truncate any encoding off the end of the locale // remove everything after @ string::size_type index = m_locale.find('@'); if (index != string::npos) m_locale.erase(index); //erase all characters starting at index // remove everything before = index = m_locale.find('='); if (index != string::npos) m_locale.erase(0,index+1); //erase all characters starting up to index } #endif // defined(HAVE_SETLOCALE) && defined(NLS) } I18n::~I18n() { #if defined(NLS) && defined(HAVE_CATCLOSE) if (m_catalog_fd != (nl_catd)-1) catclose(m_catalog_fd); #endif // HAVE_CATCLOSE } I18n *I18n::instance() { static I18n singleton; //singleton object return &singleton; } void I18n::openCatalog(const char *catalog) { #if defined(NLS) && defined(HAVE_CATOPEN) string catalog_filename = LOCALEPATH; catalog_filename += '/'; catalog_filename += m_locale; catalog_filename += '/'; catalog_filename += catalog; if (!FileUtil::isRegularFile(catalog_filename.c_str()) && m_locale != "C" && FbStringUtil::haveUTF8()) { // try the UTF-8 catalog, this also picks up situations where // the codeset somehow isn't specified // remove everything after @ string::size_type index = m_locale.find('.'); // erase all characters starting at index if (index != string::npos) m_locale.erase(index); m_locale.append(".UTF-8"); m_utf8_translate = true; catalog_filename = LOCALEPATH; catalog_filename += '/'; catalog_filename += m_locale; catalog_filename += '/'; catalog_filename += catalog; } #ifdef MCLoadBySet m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet); #else // !MCLoadBySet m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE); #endif // MCLoadBySet if (m_catalog_fd == (nl_catd)-1) { cerr<<"Warning: Failed to open file("<