/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ /*************************************************************************** i18n.cpp - description ------------------- begin : Wed Apr 25 2001 copyright : (C) 2001-2004 by Intra2net AG ***************************************************************************/ #include #include #include #include #include #include #include #include #include "i18n.h" #define DEFAULT_LANG "en_EN" using namespace std; void i18n_init (const string& domain, const string& path) { i18n_set_language (i18n_get_default_language()); bindtextdomain (domain.c_str(), path.c_str()); textdomain (domain.c_str()); } void i18n_set_language (const string &lang) { string target; if (lang.empty()) target=DEFAULT_LANG; else { if (lang=="NULL") target=""; else target=lang; } if (target.empty()) setlocale(LC_ALL,"C"); else setlocale(LC_ALL,target.c_str()); // Flush gcc based gettext caching. Code is from the -GNU- // gettext manual in the "being a gettext grok" chapter. { extern int _nl_msg_cat_cntr; ++_nl_msg_cat_cntr; } return; } // empty string returned: no language set string i18n_get_current_language(void) { string lng; char* env=setlocale(LC_ALL,NULL); if (env != NULL) { if (strcmp(env,"C")==0) lng="NULL"; else lng=env; } else lng="NULL"; return lng; } // empty string returned: no language set string i18n_get_default_language(void) { string lang; ifstream in("/usr/intranator/etc/locale"); if (in) { getline (in, lang); if (lang.empty()) lang=DEFAULT_LANG; in.close(); } return lang; } string i18n_get_string(const char *source, const vector &data) { string::size_type p=0, slen=0; string dollar="$"; // Convert char* to std::string string text(source); while ((p=text.find('$',p))!=string::npos) { const string *ins; if (text.size() < p) { ostringstream os; os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-"; return os.str(); } // find string to insert (=ins) if (text.at(p+1)=='$') { ins=$ slen=2; } else { slen=text.find_first_not_of("0123456789",p+1); if (slen==string::npos) slen=text.size(); if (slen==p+1) { ostringstream os; os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-"; return os.str(); } slen-=p; istringstream is(text.substr(p+1,slen-1)); unsigned int dnr; is >> dnr; if (is.fail()) { ostringstream os; os << "i18n syntax error: error reading number at pos ->" << p << "<- in string ->" << text << "<-"; return os.str(); } if (dnr+1 > data.size()) { ostringstream os; os << "i18n parameter error: missing variable ->$" << dnr << "<- in string ->" << text << "<-"; return os.str(); } ins=&(data.at(dnr)); } text.replace(p,slen,*ins); p=p+ins->size(); } return text; } string i18n_get_string(const std::string &source, const vector &data) { // Convert std::string to const char* return i18n_get_string (source.c_str(), data); } string i18n_get_string (const string &source, const string &arg0) { vector data; data.push_back(arg0); return i18n_get_string (source.c_str(), data); } string i18n_get_string (const string &source, const string &arg0, const string &arg1) { vector data; data.push_back(arg0); data.push_back(arg1); return i18n_get_string (source.c_str(), data); } string i18n_get_string (const string &source, const string &arg0, const string &arg1, const string &arg2) { vector data; data.push_back(arg0); data.push_back(arg1); data.push_back(arg2); return i18n_get_string (source.c_str(), data); } // convert locale to language (de_DE -> de) string i18n_locale2language(const string &locale) { // search for "_" string::size_type pos = locale.find("_"); if (pos == string::npos) return locale; return(locale.substr(0, pos)); }