* added Portoguese Brazillian translation done by Marco Goncalves
[citadel.git] / webcit / gettext.c
1 /*
2  * $Id
3  */
4 /**
5  * \defgroup LocaleHeaderParser Parse the browser http locale headers and set the NLS stuff.
6  * \ingroup WebcitHttpServer 
7  */
8 /*@{*/
9 #include "webcit.h"
10 #include "webserver.h"
11
12 #ifdef ENABLE_NLS
13
14 #define NUM_LANGS 10 /**< how many different locales do we know? */
15 #define SEARCH_LANG 20 /**< how many langs should we parse? */
16
17 /** actual supported locales */
18 char *AvailLang[NUM_LANGS] = {
19         "C",
20         "en_US",
21         "de_DE",
22         "it_IT",
23         "es_ES",
24         "en_GB",
25         "da_DK",
26         "fr_FR",
27         "nl_NL",
28         "pt_BR"
29 };
30
31 #ifdef HAVE_USELOCALE
32 locale_t wc_locales[NUM_LANGS]; /**< here we keep the parsed stuff */
33 #endif
34
35 /** Keep information about one locale */
36 typedef struct _lang_pref{
37         char lang[16];          /**< the language locale string */
38         char region[16];        /**< the region locale string */
39         long priority;          /**< which priority does it have */
40         int availability;       /**< do we know it? */
41         int selectedlang;       /**< is this the selected language? */
42 } LangStruct;
43
44 /* \brief parse browser locale header 
45  * seems as most browsers just do a one after coma value even if more than 10 locales are available. Sample strings:
46  * opera: 
47  * Accept-Language: sq;q=1.0,de;q=0.9,as;q=0.8,ar;q=0.7,bn;q=0.6,zh-cn;q=0.5,kn;q=0.4,ch;q=0.3,fo;q=0.2,gn;q=0.1,ce;q=0.1,ie;q=0.1 
48  * Firefox 
49  * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' 
50  * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 
51  * Accept-Language: de,en-us;q=0.9,it;q=0.9,de-de;q=0.8,en-ph;q=0.7,de-at;q=0.7,zh-cn;q=0.6,cy;q=0.5,ar-om;q=0.5,en-tt;q=0.4,xh;q=0.3,nl-be;q=0.3,cs;q=0.2,sv;q=0.1,tk;q=0.1 
52  * \param LocaleString the string from the browser http headers
53  */
54
55 void httplang_to_locale(char *LocaleString)
56 {
57         LangStruct wanted_locales[SEARCH_LANG];
58         LangStruct *ls;
59
60         int i = 0;
61         int j = 0;
62         /* size_t len = strlen(LocaleString); */
63         long prio;
64         int av;
65         int nBest;
66         int nParts;
67         char search[1024];
68         
69         safestrncpy(search, LocaleString, sizeof search);
70         nParts=num_tokens(search,',');
71         for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
72         {
73                         char buf[16];
74                         char sbuf[16];
75                         char lbuf[16];
76                         int blen;
77                         
78                         ls=&wanted_locales[i];
79
80                         extract_token(&buf[0],search, i,',',16);
81                         /** we are searching, if this list item has something like ;q=n*/
82                         if (num_tokens(&buf[0],'=')>1) {
83                                 int sbuflen, k;
84                                 extract_token(&sbuf[0],&buf[0], 1,'=',16);
85                                 sbuflen=strlen(&sbuf[0]);
86                                 for (k=0; k<sbuflen; k++) if (sbuf[k]=='.') sbuf[k]='0';
87                                 ls->priority=atol(&sbuf[0]);
88                         }
89                         else {
90                                 ls->priority=1000;
91                         }
92                         /** get the locale part */
93                         extract_token(&sbuf[0],&buf[0],0,';',16);
94                         /** get the lang part, which should be allways there */
95                         extract_token(&ls->lang[0],&sbuf[0],0,'-',16);
96                         /** get the area code if any. */
97                         if (num_tokens(&sbuf[0],'-')>1) {
98                                 extract_token(&ls->region[0],&sbuf[0],1,'-',16);
99                         }
100                         else { /** no ara code? use lang code */
101                                 blen=strlen(&ls->lang[0]);
102                                 memcpy(&ls->region[0], ls->lang,blen);
103                                 ls->region[blen]='\0';
104                         } /** area codes are uppercase */
105                         blen=strlen(&ls->region[0]);
106                         for (j=0; j<blen; j++)
107                                 {
108                                         int chars=toupper(ls->region[j]);
109                                         ls->region[j]=(char)chars;/** \todo ?! */
110                                 }
111                         sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
112                         
113                         /** check if we have this lang */
114                         ls->availability=1;
115                         ls->selectedlang=-1;
116                         for (j=0; j<NUM_LANGS; j++) {
117                                 int result;
118                                 /** match against the LANG part */
119                                 result=strcasecmp(&ls->lang[0], AvailLang[j]);
120                                 if ((result<0)&&(result<ls->availability)){
121                                         ls->availability=result;
122                                         ls->selectedlang=j;
123                                 }
124                                 /** match against lang and locale */
125                                 if (0==strcasecmp(&lbuf[0], AvailLang[j])){
126                                         ls->availability=0;
127                                         ls->selectedlang=j;
128                                         j=NUM_LANGS;
129                                 }
130                         }
131         }
132         
133         prio=0;
134         av=-1000;
135         nBest=-1;
136         for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++) {
137                 ls=&wanted_locales[i];
138                 if ((ls->availability<=0)&& 
139                    (av<ls->availability)&&
140                    (prio<ls->priority)&&
141                    (ls->selectedlang!=-1)) {
142                         nBest=ls->selectedlang;
143                         av=ls->availability;
144                         prio=ls->priority;
145                 }
146         }
147         if (nBest == -1) {
148                 /** fall back to C */
149                 nBest=0;
150         }
151         WC->selected_language=nBest;
152         lprintf(9, "language found: %s\n", AvailLang[WC->selected_language]);
153 }
154
155 /* TODO: we skip the language weighting so far. */
156 /* Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' */
157 /* Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 */
158 //void httplang_to_locale(char *LocaleString)
159 //{
160 //      char selected_locale[16];
161 //      int i, j;
162 //      char lang[64];
163 //      int num_accept = 0;
164 //
165 //      lprintf(9, "languageAccept: %s\n", LocaleString);
166 //
167 //      strcpy(selected_locale, "C");
168 //      num_accept = num_tokens(LocaleString, ',');
169 //
170 //      for (i=num_accept-1; i>=0; --i) {
171 //              extract_token(lang, LocaleString, i, ',', sizeof lang);
172 //
173 //              /* Strip out the weights; we don't use them.  Also convert
174 //               * hyphens to underscores.
175 //               */
176 //              for (j=0; j<strlen(lang); ++j) {
177 //                      if (lang[j] == '-') lang[j] = '_';
178 //                      if (lang[j] == ';') lang[j] = 0;
179 //              }
180 //
181 //              for (j=0; j<NUM_LANGS; ++j) {
182 //                      if (!strncasecmp(lang, AvailLang[j], strlen(lang))) {
183 //                              strcpy(selected_locale, AvailLang[j]);
184 //                      }
185 //              }
186 //      }
187 //
188 //      lprintf(9, "language found: %s\n", selected_locale);
189 //      set_selected_language(selected_locale);
190 //}
191
192
193 /**
194  * \brief show the language chooser on the login dialog
195  * depending on the browser locale change the sequence of the 
196  * language chooser.
197  */
198 void offer_languages(void) {
199         int i;
200
201         wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
202
203         for (i=0; i < NUM_LANGS; ++i) {
204 #ifndef HAVE_USELOCALE
205                 if (strcmp(AvailLang[i], getenv("LANG")) == 0)
206 #endif
207                 wprintf("<option %s value=%s>%s</option>\n",
208                         ((WC->selected_language == i) ? "selected" : ""),
209                         AvailLang[i],
210                         AvailLang[i]
211                 );
212         }
213
214         wprintf("</select>\n");
215 }
216
217 /**
218  * \brief Set the selected language for this session.
219  * \param lang the locale to set.
220  */
221 void set_selected_language(char *lang) {
222         int i;
223 #ifdef HAVE_USELOCALE
224         for (i=0; i<NUM_LANGS; ++i) {
225                 if (!strcasecmp(lang, AvailLang[i])) {
226                         WC->selected_language = i;
227                 }
228         }
229 #endif
230 }
231
232 /**
233  * \brief Activate the selected language for this session.
234  */
235 void go_selected_language(void) {
236 #ifdef HAVE_USELOCALE
237         if (WC->selected_language < 0) return;
238         uselocale(wc_locales[WC->selected_language]);   /** switch locales */
239         textdomain(textdomain(NULL));                   /** clear the cache */
240 #else
241         char *language;
242         
243         language = getenv("LANG");
244         setlocale(LC_MESSAGES, language);
245 #endif
246 }
247
248 /**
249  * \brief Deactivate the selected language for this session.
250  */
251 void stop_selected_language(void) {
252 #ifdef HAVE_USELOCALE
253         uselocale(LC_GLOBAL_LOCALE);                    /** switch locales */
254         textdomain(textdomain(NULL));                   /** clear the cache */
255 #endif
256 }
257
258 void preset_locale(void)
259 {
260 #ifndef HAVE_USELOCALE
261 #ifdef HAVE_GETTEXT
262         char *language;
263         
264         lprintf(9, "Nailing locale to %s\n", getenv("LANG"));
265         language = getenv("LANG");
266         setlocale(LC_MESSAGES, language);
267 #endif
268 #endif
269 }
270 /**
271  * \brief Create a locale_t for each available language
272  */
273 void initialize_locales(void) {
274         int i;
275         char buf[32];
276
277 #ifdef HAVE_USELOCALE
278         locale_t Empty_Locale;
279
280         /* create default locale */
281         Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
282 #endif
283
284         for (i = 0; i < NUM_LANGS; ++i) {
285                 if (i == 0) {
286                         sprintf(buf, "%s", AvailLang[i]);       // locale 0 (C) is ascii, not utf-8
287                 }
288                 else {
289                         sprintf(buf, "%s.UTF8", AvailLang[i]);
290                 }
291 #ifdef HAVE_USELOCALE
292                 wc_locales[i] = newlocale(
293                         (LC_MESSAGES_MASK|LC_TIME_MASK),
294                         buf,
295                         (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
296                 );
297                 if (wc_locales[i] == NULL) {
298                         lprintf(1, "Error configuring locale for %s: %s\n",
299                                 buf,
300                                 strerror(errno)
301                         );
302                 }
303                 else {
304                         lprintf(3, "Configured available locale: %s\n", buf);
305                 }
306 #endif
307         }
308 }
309
310
311 #else   /* ENABLE_NLS */
312 /** \brief dummy for non NLS enabled systems */
313 void offer_languages(void) {
314         wprintf("English (US)");
315 }
316
317 /** \brief dummy for non NLS enabled systems */
318 void set_selected_language(char *lang) {
319 }
320
321 /** \brief dummy for non NLS enabled systems */
322 void go_selected_language(void) {
323 }
324
325 /** \brief dummy for non NLS enabled systems */
326 void stop_selected_language(void) {
327 }
328
329 void preset_locale(void)
330 {
331 }
332 #endif  /* ENABLE_NLS */
333
334
335 /*@}*/