2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
20 /***************************************************************************
21 i18n.cpp - description
23 begin : Wed Apr 25 2001
24 copyright : (C) 2001-2004 by Intra2net AG
25 ***************************************************************************/
38 #define DEFAULT_LANG "en_EN"
42 void i18n_init (const string& domain, const string& path)
44 i18n_set_language (i18n_get_default_language());
46 bindtextdomain (domain.c_str(), path.c_str());
47 textdomain (domain.c_str());
50 void i18n_set_language (const string &lang)
65 setlocale(LC_ALL,"C");
67 setlocale(LC_ALL,target.c_str());
69 // Flush gcc based gettext caching. Code is from the -GNU-
70 // gettext manual in the "being a gettext grok" chapter.
72 extern int _nl_msg_cat_cntr;
79 // empty string returned: no language set
80 string i18n_get_current_language(void)
83 char* env=setlocale(LC_ALL,NULL);
87 if (strcmp(env,"C")==0)
98 // empty string returned: no language set
99 string i18n_get_default_language(void)
103 ifstream in("/usr/intranator/etc/locale");
116 string i18n_get_string(const char *source, const vector<string> &data)
118 string::size_type p=0, slen=0;
121 // Convert char* to std::string
124 while ((p=text.find('$',p))!=string::npos)
131 os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-";
135 // find string to insert (=ins)
136 if (text.at(p+1)=='$')
143 slen=text.find_first_not_of("0123456789",p+1);
145 if (slen==string::npos)
151 os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-";
156 istringstream is(text.substr(p+1,slen-1));
164 os << "i18n syntax error: error reading number at pos ->" << p << "<- in string ->" << text << "<-";
168 if (dnr+1 > data.size())
171 os << "i18n parameter error: missing variable ->$" << dnr << "<- in string ->" << text << "<-";
178 text.replace(p,slen,*ins);
185 string i18n_get_string(const std::string &source, const vector<string> &data)
187 // Convert std::string to const char*
188 return i18n_get_string (source.c_str(), data);
191 string i18n_get_string (const string &source, const string &arg0)
194 data.push_back(arg0);
195 return i18n_get_string (source.c_str(), data);
198 string i18n_get_string (const string &source, const string &arg0, const string &arg1)
201 data.push_back(arg0);
202 data.push_back(arg1);
203 return i18n_get_string (source.c_str(), data);
206 string i18n_get_string (const string &source, const string &arg0, const string &arg1, const string &arg2)
209 data.push_back(arg0);
210 data.push_back(arg1);
211 data.push_back(arg2);
212 return i18n_get_string (source.c_str(), data);
215 // convert locale to language (de_DE -> de)
216 string i18n_locale2language(const string &locale)
219 string::size_type pos = locale.find("_");
220 if (pos == string::npos)
223 return(locale.substr(0, pos));