Merge branch 'daemon-ext'
[libi2ncommon] / src / i18n.cpp
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
5ff2acd9
TJ
20/***************************************************************************
21 i18n.cpp - description
22 -------------------
23 begin : Wed Apr 25 2001
24 copyright : (C) 2001-2004 by Intra2net AG
5ff2acd9
TJ
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
40using namespace std;
41
42void 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
50void 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
80string 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
99string 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
4e888860
TJ
116string i18n_get_string(const char *source, const vector<string> &data)
117{
118 string::size_type p=0, slen=0;
119 string dollar="$";
5ff2acd9 120
4e888860
TJ
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=&dollar;
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 }
5ff2acd9 181
4e888860 182 return text;
5ff2acd9
TJ
183}
184
4e888860 185string i18n_get_string(const std::string &source, const vector<string> &data)
5ff2acd9 186{
4e888860
TJ
187 // Convert std::string to const char*
188 return i18n_get_string (source.c_str(), data);
5ff2acd9
TJ
189}
190
e11a40a2
CH
191string 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
198string 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
206string 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
5ff2acd9 215// convert locale to language (de_DE -> de)
9e2ff02c 216string i18n_locale2language(const string &locale)
5ff2acd9
TJ
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}