news download themes documentation links










I18n.cc

00001 // I18n.hh for Fluxbox Window Manager
00002 // Copyright (c) 2001 - 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net)
00003 //
00004 // I18n.cc for Blackbox - an X11 Window manager
00005 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
00006 //
00007 // Permission is hereby granted, free of charge, to any person obtaining a
00008 // copy of this software and associated documentation files (the "Software"),
00009 // to deal in the Software without restriction, including without limitation
00010 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 // and/or sell copies of the Software, and to permit persons to whom the
00012 // Software is furnished to do so, subject to the following conditions:
00013 //
00014 // The above copyright notice and this permission notice shall be included in
00015 // all copies or substantial portions of the Software.
00016 //
00017 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00020 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 // DEALINGS IN THE SOFTWARE.
00024 
00025 // $Id: I18n.cc,v 1.5 2004/01/06 13:42:47 grubert Exp $
00026 
00027 //usr GNU extensions
00028 #ifndef  _GNU_SOURCE
00029 #define  _GNU_SOURCE
00030 #endif // _GNU_SOURCE
00031 
00032 #include "I18n.hh"
00033 
00034 #include <X11/Xlocale.h>
00035 
00036 #include <cstdlib>
00037 #include <cstring>
00038 #include <cstdio>
00039 
00040 #include <iostream>
00041 
00042 #include "defaults.hh"
00043 
00044 using std::cerr;
00045 using std::endl;
00046 using std::string;
00047 
00048 void NLSInit(const char *catalog) {
00049     I18n *i18n = I18n::instance();
00050     i18n->openCatalog(catalog);
00051 }
00052 
00053 
00054 I18n::I18n():m_multibyte(false), m_catalog_fd((nl_catd)(-1)) {
00055 #ifdef  HAVE_SETLOCALE
00056     //make sure we don't get 0 to m_locale string
00057     char *temp = setlocale(LC_ALL, "");
00058     m_locale = ( temp ?  temp : ""); 
00059     if (m_locale.size() == 0) {
00060         cerr<<"Warning: Failed to set locale, reverting to \"C\""<<endl;
00061 #endif // HAVE_SETLOCALE
00062 
00063         m_locale = "C";
00064 
00065 #ifdef  HAVE_SETLOCALE
00066 
00067     } else {        
00068         // MB_CUR_MAX returns the size of a char in the current locale
00069         if (MB_CUR_MAX > 1)
00070             m_multibyte = true;
00071         
00072         // truncate any encoding off the end of the locale
00073                 
00074         // remove everything after @
00075         string::size_type index = m_locale.find('@');
00076         if (index != string::npos)
00077             m_locale.erase(index); //erase all characters starting at index                 
00078         // remove everything after .        
00079         index = m_locale.find('.');
00080         if (index != string::npos) 
00081             m_locale.erase(index); //erase all characters starting at index 
00082         // remove everything before =       
00083         index = m_locale.find('=');
00084         if (index != string::npos) 
00085             m_locale.erase(0,index+1); //erase all characters starting up to index 
00086     }
00087 #endif // HAVE_SETLOCALE
00088 }
00089 
00090 
00091 I18n::~I18n() {
00092 
00093 #if defined(NLS) && defined(HAVE_CATCLOSE)
00094     if (m_catalog_fd != (nl_catd)-1)
00095         catclose(m_catalog_fd);
00096 #endif // HAVE_CATCLOSE
00097 }
00098 
00099 I18n *I18n::instance() {
00100     static I18n singleton; //singleton object
00101     return &singleton;
00102 }
00103 
00104 void I18n::openCatalog(const char *catalog) {
00105 #if defined(NLS) && defined(HAVE_CATOPEN)
00106     
00107     string catalog_filename = LOCALEPATH;
00108     catalog_filename += '/';
00109     catalog_filename += m_locale;
00110     catalog_filename += '/';
00111     catalog_filename += catalog;
00112 
00113 #ifdef MCLoadBySet
00114     m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet);
00115 #else // !MCLoadBySet
00116     m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE);
00117 #endif // MCLoadBySet
00118 
00119     if (m_catalog_fd == (nl_catd)-1) {
00120         cerr<<"Warning: Failed to open file("<<catalog_filename<<")"<<endl;
00121         cerr<<"for translation, using default messages."<<endl;
00122     }
00123     
00124 #else // !HAVE_CATOPEN
00125     
00126     m_catalog_fd = (nl_catd)-1;
00127 #endif // HAVE_CATOPEN
00128 }
00129 
00130 
00131 const char *I18n::getMessage(int set_number, int message_number, 
00132                              const char *default_message) const {
00133 
00134 #if defined(NLS) && defined(HAVE_CATGETS)
00135     if (m_catalog_fd != (nl_catd)-1)
00136         return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message);
00137     else
00138 #endif // NLS && HAVE_CATGETS
00139         return default_message;
00140 }

Fluxbox CVS-Jan-2003




      



Got comments about the page? Send them to webmaster.
If you have general Fluxbox related questions ask them on our irc channel or mailing lists.

Show Source








Designed by aLEczapKA