fluxbox/src/FbTk/I18n.cc

165 lines
4.7 KiB
C++
Raw Normal View History

// I18n.hh for Fluxbox Window Manager
2005-01-24 18:02:34 +00:00
// Copyright (c) 2001 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
//
// I18n.cc for Blackbox - an X11 Window manager
2001-12-11 20:47:02 +00:00
// 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.
2004-11-19 11:37:27 +00:00
// $Id$
2004-06-07 11:46:05 +00:00
/* 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
*/
//usr GNU extensions
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
2001-12-11 20:47:02 +00:00
#endif // _GNU_SOURCE
#include "I18n.hh"
2001-12-11 20:47:02 +00:00
#include <X11/Xlocale.h>
2004-08-31 15:26:40 +00:00
#ifdef HAVE_CSTDLIB
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef HAVE_CSTRING
#include <cstring>
#else
#include <string.h>
#endif
#ifdef HAVE_CSTDIO
#include <cstdio>
#else
#include <stdio.h>
#endif
2002-12-02 22:50:57 +00:00
#include <iostream>
2003-06-30 20:37:57 +00:00
using std::cerr;
using std::endl;
using std::string;
2001-12-11 20:47:02 +00:00
2004-06-07 11:46:05 +00:00
namespace FbTk {
2001-12-11 20:47:02 +00:00
void NLSInit(const char *catalog) {
2002-12-01 13:42:15 +00:00
I18n *i18n = I18n::instance();
i18n->openCatalog(catalog);
2001-12-11 20:47:02 +00:00
}
2002-04-04 11:32:16 +00:00
I18n::I18n():m_multibyte(false), m_catalog_fd((nl_catd)(-1)) {
#ifdef HAVE_SETLOCALE
2002-12-01 13:42:15 +00:00
//make sure we don't get 0 to m_locale string
char *temp = setlocale(LC_MESSAGES, "");
2002-12-01 13:42:15 +00:00
m_locale = ( temp ? temp : "");
2004-01-21 14:14:40 +00:00
if (m_locale.empty()) {
2002-12-01 13:42:15 +00:00
cerr<<"Warning: Failed to set locale, reverting to \"C\""<<endl;
2001-12-11 20:47:02 +00:00
#endif // HAVE_SETLOCALE
2002-12-01 13:42:15 +00:00
m_locale = "C";
#ifdef HAVE_SETLOCALE
2002-12-01 13:42:15 +00:00
} else {
setlocale(LC_TIME, "");
2002-12-01 13:42:15 +00:00
// MB_CUR_MAX returns the size of a char in the current locale
if (MB_CUR_MAX > 1)
m_multibyte = true;
2002-12-01 13:42:15 +00:00
// truncate any encoding off the end of the locale
// remove everything after @
2002-12-01 13:42:15 +00:00
string::size_type index = m_locale.find('@');
if (index != string::npos)
m_locale.erase(index); //erase all characters starting at index
// remove everything after .
2002-12-01 13:42:15 +00:00
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
2002-12-01 13:42:15 +00:00
}
2001-12-11 20:47:02 +00:00
#endif // HAVE_SETLOCALE
}
I18n::~I18n() {
2001-12-11 20:47:02 +00:00
#if defined(NLS) && defined(HAVE_CATCLOSE)
2002-12-01 13:42:15 +00:00
if (m_catalog_fd != (nl_catd)-1)
catclose(m_catalog_fd);
2001-12-11 20:47:02 +00:00
#endif // HAVE_CATCLOSE
}
I18n *I18n::instance() {
2002-12-01 13:42:15 +00:00
static I18n singleton; //singleton object
return &singleton;
2001-12-11 20:47:02 +00:00
}
void I18n::openCatalog(const char *catalog) {
#if defined(NLS) && defined(HAVE_CATOPEN)
2002-12-01 13:42:15 +00:00
string catalog_filename = LOCALEPATH;
catalog_filename += '/';
catalog_filename += m_locale;
catalog_filename += '/';
catalog_filename += catalog;
#ifdef MCLoadBySet
2002-12-01 13:42:15 +00:00
m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet);
#else // !MCLoadBySet
2002-12-01 13:42:15 +00:00
m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE);
#endif // MCLoadBySet
2003-05-16 00:17:16 +00:00
if (m_catalog_fd == (nl_catd)-1) {
cerr<<"Warning: Failed to open file("<<catalog_filename<<")"<<endl;
cerr<<"for translation, using default messages."<<endl;
}
2001-12-11 20:47:02 +00:00
#else // !HAVE_CATOPEN
2002-12-01 13:42:15 +00:00
m_catalog_fd = (nl_catd)-1;
2001-12-11 20:47:02 +00:00
#endif // HAVE_CATOPEN
}
const char *I18n::getMessage(int set_number, int message_number,
2003-12-08 17:29:24 +00:00
const char *default_message) const {
#if defined(NLS) && defined(HAVE_CATGETS)
2002-12-01 13:42:15 +00:00
if (m_catalog_fd != (nl_catd)-1)
return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message);
else
2002-08-13 23:54:41 +00:00
#endif // NLS && HAVE_CATGETS
2002-12-01 13:42:15 +00:00
return default_message;
2001-12-11 20:47:02 +00:00
}
2004-06-07 11:46:05 +00:00
}; // end namespace FbTk