| 1 | /* |
| 2 | The software in this package is distributed under the GNU General |
| 3 | Public License version 2 (with a special exception described below). |
| 4 | |
| 5 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 6 | in the file COPYING.GPL. |
| 7 | |
| 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. |
| 13 | |
| 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. |
| 16 | |
| 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. |
| 19 | */ |
| 20 | /*************************************************************************** |
| 21 | i18n.cpp - description |
| 22 | ------------------- |
| 23 | begin : Wed Apr 25 2001 |
| 24 | copyright : (C) 2001-2004 by Intra2net AG |
| 25 | ***************************************************************************/ |
| 26 | |
| 27 | #include <sstream> |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | #include <iostream> |
| 31 | #include <fstream> |
| 32 | |
| 33 | #include <time.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include "i18n.h" |
| 37 | |
| 38 | #define DEFAULT_LANG "en_EN" |
| 39 | |
| 40 | using namespace std; |
| 41 | |
| 42 | void i18n_init (const string& domain, const string& path) |
| 43 | { |
| 44 | i18n_set_language (i18n_get_default_language()); |
| 45 | |
| 46 | bindtextdomain (domain.c_str(), path.c_str()); |
| 47 | textdomain (domain.c_str()); |
| 48 | } |
| 49 | |
| 50 | void i18n_set_language (const string &lang) |
| 51 | { |
| 52 | string target; |
| 53 | |
| 54 | if (lang.empty()) |
| 55 | target=DEFAULT_LANG; |
| 56 | else |
| 57 | { |
| 58 | if (lang=="NULL") |
| 59 | target=""; |
| 60 | else |
| 61 | target=lang; |
| 62 | } |
| 63 | |
| 64 | if (target.empty()) |
| 65 | setlocale(LC_ALL,"C"); |
| 66 | else |
| 67 | setlocale(LC_ALL,target.c_str()); |
| 68 | |
| 69 | // Flush gcc based gettext caching. Code is from the -GNU- |
| 70 | // gettext manual in the "being a gettext grok" chapter. |
| 71 | { |
| 72 | extern int _nl_msg_cat_cntr; |
| 73 | ++_nl_msg_cat_cntr; |
| 74 | } |
| 75 | |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // empty string returned: no language set |
| 80 | string i18n_get_current_language(void) |
| 81 | { |
| 82 | string lng; |
| 83 | char* env=setlocale(LC_ALL,NULL); |
| 84 | |
| 85 | if (env != NULL) |
| 86 | { |
| 87 | if (strcmp(env,"C")==0) |
| 88 | lng="NULL"; |
| 89 | else |
| 90 | lng=env; |
| 91 | } |
| 92 | else |
| 93 | lng="NULL"; |
| 94 | |
| 95 | return lng; |
| 96 | } |
| 97 | |
| 98 | // empty string returned: no language set |
| 99 | string i18n_get_default_language(void) |
| 100 | { |
| 101 | string lang; |
| 102 | |
| 103 | ifstream in("/usr/intranator/etc/locale"); |
| 104 | if (in) |
| 105 | { |
| 106 | getline (in, lang); |
| 107 | |
| 108 | if (lang.empty()) |
| 109 | lang=DEFAULT_LANG; |
| 110 | in.close(); |
| 111 | } |
| 112 | |
| 113 | return lang; |
| 114 | } |
| 115 | |
| 116 | string i18n_get_string(const char *source, const vector<string> &data) |
| 117 | { |
| 118 | string::size_type p=0, slen=0; |
| 119 | string dollar="$"; |
| 120 | |
| 121 | // Convert char* to std::string |
| 122 | string text(source); |
| 123 | |
| 124 | while ((p=text.find('$',p))!=string::npos) |
| 125 | { |
| 126 | const string *ins; |
| 127 | |
| 128 | if (text.size() < p) |
| 129 | { |
| 130 | ostringstream os; |
| 131 | os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-"; |
| 132 | return os.str(); |
| 133 | } |
| 134 | |
| 135 | // find string to insert (=ins) |
| 136 | if (text.at(p+1)=='$') |
| 137 | { |
| 138 | ins=$ |
| 139 | slen=2; |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | slen=text.find_first_not_of("0123456789",p+1); |
| 144 | |
| 145 | if (slen==string::npos) |
| 146 | slen=text.size(); |
| 147 | |
| 148 | if (slen==p+1) |
| 149 | { |
| 150 | ostringstream os; |
| 151 | os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-"; |
| 152 | return os.str(); |
| 153 | } |
| 154 | |
| 155 | slen-=p; |
| 156 | istringstream is(text.substr(p+1,slen-1)); |
| 157 | unsigned int dnr; |
| 158 | |
| 159 | is >> dnr; |
| 160 | |
| 161 | if (is.fail()) |
| 162 | { |
| 163 | ostringstream os; |
| 164 | os << "i18n syntax error: error reading number at pos ->" << p << "<- in string ->" << text << "<-"; |
| 165 | return os.str(); |
| 166 | } |
| 167 | |
| 168 | if (dnr+1 > data.size()) |
| 169 | { |
| 170 | ostringstream os; |
| 171 | os << "i18n parameter error: missing variable ->$" << dnr << "<- in string ->" << text << "<-"; |
| 172 | return os.str(); |
| 173 | } |
| 174 | |
| 175 | ins=&(data.at(dnr)); |
| 176 | } |
| 177 | |
| 178 | text.replace(p,slen,*ins); |
| 179 | p=p+ins->size(); |
| 180 | } |
| 181 | |
| 182 | return text; |
| 183 | } |
| 184 | |
| 185 | string i18n_get_string(const std::string &source, const vector<string> &data) |
| 186 | { |
| 187 | // Convert std::string to const char* |
| 188 | return i18n_get_string (source.c_str(), data); |
| 189 | } |
| 190 | |
| 191 | string i18n_get_string (const string &source, const string &arg0) |
| 192 | { |
| 193 | vector<string> data; |
| 194 | data.push_back(arg0); |
| 195 | return i18n_get_string (source.c_str(), data); |
| 196 | } |
| 197 | |
| 198 | string i18n_get_string (const string &source, const string &arg0, const string &arg1) |
| 199 | { |
| 200 | vector<string> data; |
| 201 | data.push_back(arg0); |
| 202 | data.push_back(arg1); |
| 203 | return i18n_get_string (source.c_str(), data); |
| 204 | } |
| 205 | |
| 206 | string i18n_get_string (const string &source, const string &arg0, const string &arg1, const string &arg2) |
| 207 | { |
| 208 | vector<string> data; |
| 209 | data.push_back(arg0); |
| 210 | data.push_back(arg1); |
| 211 | data.push_back(arg2); |
| 212 | return i18n_get_string (source.c_str(), data); |
| 213 | } |
| 214 | |
| 215 | // convert locale to language (de_DE -> de) |
| 216 | string i18n_locale2language(const string &locale) |
| 217 | { |
| 218 | // search for "_" |
| 219 | string::size_type pos = locale.find("_"); |
| 220 | if (pos == string::npos) |
| 221 | return locale; |
| 222 | |
| 223 | return(locale.substr(0, pos)); |
| 224 | } |