* BIG rewrite of header handling and slimming of context_loop and session_loop; shuff...
[citadel.git] / webcit / gettext.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7
8 #ifdef ENABLE_NLS
9 /* actual supported locales */
10 const char *AvailLang[NUM_LANGS] = {
11         "C",
12         "en_US",
13         "de_DE",
14         "it_IT",
15         "es_ES",
16         "en_GB",
17         "da_DK",
18         "fr_FR",
19         "nl_NL",
20         "pt_BR"
21 };
22
23 const char *AvailLangLoaded[NUM_LANGS];
24 long nLocalesLoaded = 0;
25
26 #ifdef HAVE_USELOCALE
27 locale_t wc_locales[NUM_LANGS]; /**< here we keep the parsed stuff */
28 #endif
29
30 /** Keep information about one locale */
31 typedef struct _lang_pref{
32         char lang[16];          /**< the language locale string */
33         char region[16];        /**< the region locale string */
34         long priority;          /**< which priority does it have */
35         int availability;       /**< do we know it? */
36         int selectedlang;       /**< is this the selected language? */
37 } LangStruct;
38
39 /* \brief parse browser locale header 
40  * seems as most browsers just do a one after coma value even if more than 10 locales are available. Sample strings:
41  * opera: 
42  * 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 
43  * Firefox 
44  * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' 
45  * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 
46  * 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 
47  * \param LocaleString the string from the browser http headers
48  */
49
50 void httplang_to_locale(StrBuf *LocaleString)
51 {
52         LangStruct wanted_locales[SEARCH_LANG];
53         LangStruct *ls;
54
55         int i = 0;
56         int j = 0;
57         /* size_t len = strlen(LocaleString); */
58         long prio;
59         int av;
60         int nBest;
61         int nParts;
62         StrBuf *Buf = NULL;
63         StrBuf *SBuf = NULL;
64         
65         nParts=StrBufNum_tokens(LocaleString,',');
66         for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
67         {
68                 char lbuf[16];
69                 int blen;
70                         
71                 if (Buf == NULL) {
72                         Buf = NewStrBuf();
73                         SBuf = NewStrBuf();
74                 }
75                 else {
76                         FlushStrBuf(Buf);
77                         FlushStrBuf(SBuf);
78                 }
79
80                 ls=&wanted_locales[i];
81
82                 StrBufExtract_token(Buf,LocaleString, i,',');
83                 /** we are searching, if this list item has something like ;q=n*/
84                 if (StrBufNum_tokens(Buf,'=')>1) {
85                         int sbuflen, k;
86                         StrBufExtract_token(SBuf,Buf, 1,'=');
87                         sbuflen=StrLength(SBuf);
88                         for (k=0; k<sbuflen; k++) 
89                                 if (ChrPtr(SBuf)[k]=='.') 
90                                         StrBufPeek(SBuf, NULL, k, '0');
91                         ls->priority=StrTol(SBuf);
92                 }
93                 else {
94                         ls->priority=1000;
95                 }
96                 /** get the locale part */
97                 StrBufExtract_token(SBuf ,Buf, 0, ';');
98                 /** get the lang part, which should be allways there */
99                 extract_token(&ls->lang[0], ChrPtr(SBuf), 0, '-', 16);
100                 /** get the area code if any. */
101                 if (StrBufNum_tokens(SBuf,'-') > 1) {
102                         extract_token(&ls->region[0],ChrPtr(SBuf),1,'-',16);
103                 }
104                 else { /** no ara code? use lang code */
105                         blen=strlen(&ls->lang[0]);
106                         memcpy(&ls->region[0], ls->lang,blen);
107                         ls->region[blen]='\0';
108                 } /** area codes are uppercase */
109                 blen=strlen(&ls->region[0]);
110                 for (j=0; j<blen; j++)
111                 {
112                         int chars=toupper(ls->region[j]);
113                         ls->region[j]=(char)chars;/** \todo ?! */
114                 }
115                 sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
116                         
117                 /** check if we have this lang */
118                 ls->availability=1;
119                 ls->selectedlang=-1;
120                 for (j=0; j<nLocalesLoaded; j++) {
121                         int result;
122                         /** match against the LANG part */
123                         result=strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
124                         if ((result<0)&&(result<ls->availability)){
125                                 ls->availability=result;
126                                 ls->selectedlang=j;
127                         }
128                         /** match against lang and locale */
129                         if (0==strcasecmp(&lbuf[0], AvailLangLoaded[j])){
130                                 ls->availability=0;
131                                 ls->selectedlang=j;
132                                 j=nLocalesLoaded;
133                         }
134                 }
135         }
136         
137         prio=0;
138         av=-1000;
139         nBest=-1;
140         for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++) {
141                 ls=&wanted_locales[i];
142                 if ((ls->availability<=0)&& 
143                    (av<ls->availability)&&
144                    (prio<ls->priority)&&
145                    (ls->selectedlang!=-1)) {
146                         nBest=ls->selectedlang;
147                         av=ls->availability;
148                         prio=ls->priority;
149                 }
150         }
151         if (nBest == -1) {
152                 /** fall back to C */
153                 nBest=0;
154         }
155         WC->selected_language=nBest;
156         lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
157         FreeStrBuf(&Buf);
158         FreeStrBuf(&SBuf);
159 }
160
161 /**
162  * \brief show the language chooser on the login dialog
163  * depending on the browser locale change the sequence of the 
164  * language chooser.
165  */
166 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
167 {
168         int i;
169 #ifndef HAVE_USELOCALE
170         char *Lang = getenv("LANG");
171         
172         if (Lang == NULL)
173                 Lang = "C";
174 #endif
175
176
177         if (nLocalesLoaded == 1) {
178                 wprintf("<p>%s</p>", AvailLangLoaded[0]);
179                 return;
180         }
181
182         wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
183
184         for (i=0; i < nLocalesLoaded; ++i) {
185 #ifndef HAVE_USELOCALE
186                 if (strcmp(AvailLangLoaded[i], Lang) == 0)
187 #endif
188                 wprintf("<option %s value=%s>%s</option>\n",
189                         ((WC->selected_language == i) ? "selected" : ""),
190                         AvailLangLoaded[i],
191                         AvailLangLoaded[i]
192                 );
193         }
194
195         wprintf("</select>\n");
196 }
197
198 /**
199  * \brief Set the selected language for this session.
200  * \param lang the locale to set.
201  */
202 void set_selected_language(const char *lang) {
203 #ifdef HAVE_USELOCALE
204         int i;
205         for (i=0; i<nLocalesLoaded; ++i) {
206                 if (!strcasecmp(lang, AvailLangLoaded[i])) {
207                         WC->selected_language = i;
208                 }
209         }
210 #endif
211 }
212
213 /**
214  * \brief Activate the selected language for this session.
215  */
216 void go_selected_language(void) {
217 #ifdef HAVE_USELOCALE
218         wcsession *WCC = WC;
219         if (WCC->selected_language < 0) return;
220         uselocale(wc_locales[WCC->selected_language]);  /** switch locales */
221         textdomain(textdomain(NULL));                   /** clear the cache */
222 #else
223         char *language;
224         
225         language = getenv("LANG");
226         setlocale(LC_MESSAGES, language);
227 #endif
228 }
229
230 /**
231  * \brief Deactivate the selected language for this session.
232  */
233 void stop_selected_language(void) {
234 #ifdef HAVE_USELOCALE
235         uselocale(LC_GLOBAL_LOCALE);                    /** switch locales */
236         textdomain(textdomain(NULL));                   /** clear the cache */
237 #endif
238 }
239
240 #ifdef HAVE_USELOCALE
241         locale_t Empty_Locale;
242 #endif
243
244 /**
245  * \brief Create a locale_t for each available language
246  */
247 void initialize_locales(void) {
248         int i;
249         char buf[32];
250         char *language = NULL;
251         
252         language = getenv("WEBCIT_LANG");
253         if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
254                 lprintf(9, "Nailing locale to %s\n", language);
255         }
256         else language = NULL;
257
258 #ifdef HAVE_USELOCALE
259         /* create default locale */
260         Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
261 #endif
262
263
264
265
266         for (i = 0; i < NUM_LANGS; ++i) {
267                 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
268                         continue;
269                 if (i == 0) {
270                         sprintf(buf, "%s", AvailLang[i]);       /* locale 0 (C) is ascii, not utf-8 */
271                 }
272                 else {
273                         sprintf(buf, "%s.UTF8", AvailLang[i]);
274                 }
275 #ifdef HAVE_USELOCALE
276                 wc_locales[nLocalesLoaded] = newlocale(
277                         (LC_MESSAGES_MASK|LC_TIME_MASK),
278                         buf,
279                         (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
280                 );
281                 if (wc_locales[nLocalesLoaded] == NULL) {
282                         lprintf(1, "Error configuring locale for %s: %s\n",
283                                 buf,
284                                 strerror(errno)
285                         );
286                 }
287                 else {
288                         lprintf(3, "Configured available locale: %s\n", buf);
289                         AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
290                         nLocalesLoaded++;
291                 }
292 #else
293                 if (language != NULL) {
294                         setenv("LANG", buf, 1);
295                         AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
296                         nLocalesLoaded++;
297                 }
298                 else if (nLocalesLoaded == 0) {
299                         setenv("LANG", buf, 1);
300                         AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
301                         nLocalesLoaded++;
302                 }
303 #endif
304         }
305         if ((language != NULL) && (nLocalesLoaded == 0)) {
306                 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
307 #ifdef HAVE_USELOCALE
308                 wc_locales[0] = newlocale(
309                         (LC_MESSAGES_MASK|LC_TIME_MASK),
310                         AvailLang[0],
311                         Empty_Locale);          
312 #else
313                 setlocale(LC_MESSAGES, AvailLang[0]);
314                 setenv("LANG", AvailLang[0], 1);
315 #endif
316                 AvailLangLoaded[0] = AvailLang[0];
317                 nLocalesLoaded = 1;
318         }
319 #ifndef HAVE_USELOCALE
320
321
322 #endif
323
324 }
325
326
327 void ShutdownLocale(void)
328 {
329 #ifdef HAVE_USELOCALE
330         int i;
331         for (i = 0; i < nLocalesLoaded; ++i) {
332                 if (Empty_Locale != wc_locales[i])
333                         freelocale(wc_locales[i]);
334         }
335 #endif
336 }
337
338 #else   /* ENABLE_NLS */
339 const char *AvailLang[NUM_LANGS] = {
340         "C"};
341
342 /** \brief dummy for non NLS enabled systems */
343 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
344 {
345         wprintf("English (US)");
346 }
347
348 /** \brief dummy for non NLS enabled systems */
349 void set_selected_language(char *lang) {
350 }
351
352 /** \brief dummy for non NLS enabled systems */
353 void go_selected_language(void) {
354 }
355
356 /** \brief dummy for non NLS enabled systems */
357 void stop_selected_language(void) {
358 }
359
360 #endif  /* ENABLE_NLS */
361
362
363 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
364 {
365         StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
366 }
367
368
369 /*
370  * Returns the language currently in use.
371  * This function returns a static string, so don't do anything stupid please.
372  */
373 const char *get_selected_language(void) {
374 #ifdef ENABLE_NLS
375 #ifdef HAVE_USELOCALE
376         return AvailLang[WC->selected_language];
377 #else
378         return "en";
379 #endif
380 #else
381         return "en";
382 #endif
383 }
384
385 void 
386 InitModule_GETTEXT
387 (void)
388 {
389         RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);
390 }
391
392
393 void
394 SessionNewModule_GETTEXT
395 (wcsession *sess)
396 {
397 #ifdef ENABLE_NLS
398         void *vLine;
399         ////TODO: make me a header getter
400         if (GetHash(WC->Hdr->HTTPHeaders, HKEY("ACCEPT-LANGUAGE"), &vLine) && 
401             (vLine != NULL)) {
402                 StrBuf *accept_language = (StrBuf*) vLine;
403                 httplang_to_locale(accept_language);
404         }
405 #endif
406 }
407
408 void
409 SessionAttachModule_GETTEXT
410 (wcsession *sess)
411 {
412 #ifdef ENABLE_NLS
413         go_selected_language();                                 /* set locale */
414 #endif
415 }
416
417 void 
418 SessionDestroyModule_GETTEXT
419 (wcsession *sess)
420 {
421 #ifdef ENABLE_NLS
422         stop_selected_language();                               /* unset locale */
423 #endif
424 }