00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00069 if (MB_CUR_MAX > 1)
00070 m_multibyte = true;
00071
00072
00073
00074
00075 string::size_type index = m_locale.find('@');
00076 if (index != string::npos)
00077 m_locale.erase(index);
00078
00079 index = m_locale.find('.');
00080 if (index != string::npos)
00081 m_locale.erase(index);
00082
00083 index = m_locale.find('=');
00084 if (index != string::npos)
00085 m_locale.erase(0,index+1);
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;
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 }