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 void i18n_get_string(const char *source, string &target, const vector<string> &data) {
117 string src = string (source);
118 i18n_get_string (src, target, data);
121 string i18n_get_string(const char *source, const vector<string> &data) {
123 string src = string (source);
124 i18n_get_string (src, target, data);
128 string i18n_get_string(const string &source, const vector<string> &data) {
130 i18n_get_string (source, target, data);
134 void i18n_get_string(const string &source, string &target, const vector<string> &data)
136 string::size_type p=0, slen=0;
140 string text = source;
144 while ((p=target.find('$',p))!=string::npos)
148 if (target.size() < p)
151 os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-";
156 // find string to insert (=ins)
157 if (target.at(p+1)=='$')
164 slen=target.find_first_not_of("0123456789",p+1);
166 if (slen==string::npos)
172 os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-";
178 istringstream is(target.substr(p+1,slen-1));
186 os << "i18n syntax error: error reading number at pos ->" << p << "<- in string ->" << text << "<-";
191 if (dnr+1 > data.size())
194 os << "i18n parameter error: missing variable ->$" << dnr << "<- in string ->" << text << "<-";
202 target.replace(p,slen,*ins);
207 // convert locale to language (de_DE -> de)
208 string i18n_locale2language(string locale)
211 string::size_type pos = locale.find("_");
212 if (pos == string::npos)
215 return(locale.substr(0, pos));