* apply r8166 from the davrework-branch:
[citadel.git] / webcit / gettext.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7 #define SEARCH_LANG 20          /* how many langs should we parse? */
8
9 #ifdef ENABLE_NLS
10 /* actual supported locales */
11 const char *AvailLang[] = {
12         "C",
13         "en_US",
14         "de_DE",
15         "it_IT",
16         "es_ES",
17         "en_GB",
18         "da_DK",
19         "fr_FR",
20         "nl_NL",
21         "pt_BR",
22         "hu_HU",
23         "et_EE",
24         "ru_RU",
25         ""
26 };
27
28 const char **AvailLangLoaded;
29 long nLocalesLoaded = 0;
30
31 #ifdef HAVE_USELOCALE
32 locale_t *wc_locales; /**< 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(StrBuf *LocaleString, wcsession *sess)
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         StrBuf *Buf = NULL;
68         StrBuf *SBuf = NULL;
69
70         nParts = StrBufNum_tokens(LocaleString, ',');
71         for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
72         {
73                 char lbuf[32];
74                 int blen;
75                         
76                 if (Buf == NULL) {
77                         Buf = NewStrBuf();
78                         SBuf = NewStrBuf();
79                 }
80                 else {
81                         FlushStrBuf(Buf);
82                         FlushStrBuf(SBuf);
83                 }
84
85                 ls = &wanted_locales[i];
86
87                 StrBufExtract_token(Buf, LocaleString, i, ',');
88                 /** we are searching, if this list item has something like ;q=n*/
89                 if (StrBufNum_tokens(Buf, '=') > 1) {
90                         int sbuflen, k;
91                         StrBufExtract_token(SBuf, Buf, 1, '=');
92                         sbuflen = StrLength(SBuf);
93                         for (k = 0; k < sbuflen; k++) 
94                                 if (ChrPtr(SBuf)[k] == '.') 
95                                         StrBufPeek(SBuf, NULL, k, '0');
96                         ls->priority = StrTol(SBuf);
97                 }
98                 else {
99                         ls->priority = 1000;
100                 }
101
102                 /** get the locale part */
103                 StrBufExtract_token(SBuf, Buf, 0, ';');
104
105                 /** get the lang part, which should be allways there */
106                 extract_token(&ls->lang[0], 
107                               ChrPtr(SBuf), 
108                               0, '-', 
109                               sizeof(ls->lang));
110
111                 /** get the area code if any. */
112                 if (StrBufNum_tokens(SBuf, '-') > 1) {
113                         extract_token(&ls->region[0], 
114                                       ChrPtr(SBuf), 
115                                       1, '-', 
116                                       sizeof(ls->region));
117                 }
118                 else { /** no ara code? use lang code */
119                         blen=strlen(&ls->lang[0]);
120                         memcpy(&ls->region[0], ls->lang, blen);
121                         ls->region[blen] = '\0';
122                 }
123
124                 /** area codes are uppercase */
125                 blen = strlen(&ls->region[0]);
126                 for (j = 0; j < blen; j++)
127                 {
128                         int chars;
129                         chars = toupper(ls->region[j]);
130                         ls->region[j] = (char)chars;/** \todo ?! */
131                 }
132                 snprintf(&lbuf[0], 
133                          sizeof(lbuf), 
134                          "%s_%s", 
135                          &ls->lang[0], 
136                          &ls->region[0]);
137                         
138                 /** check if we have this lang */
139                 ls->availability = 1;
140                 ls->selectedlang = -1;
141                 for (j = 0; j < nLocalesLoaded; j++) {
142                         int result;
143                         /** match against the LANG part */
144                         result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
145                         if ((result < 0) && (result < ls->availability)){
146                                 ls->availability = result;
147                                 ls->selectedlang = j;
148                         }
149                         /** match against lang and locale */
150                         if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
151                                 ls->availability = 0;
152                                 ls->selectedlang = j;
153                                 j = nLocalesLoaded;
154                         }
155                 }
156         }
157         
158         prio = 0;
159         av = -1000;
160         nBest = -1;
161         for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
162                 ls = &wanted_locales[i];
163                 if ((ls->availability <= 0) && 
164                     (av < ls->availability) &&
165                     (prio < ls->priority) &&
166                     (ls->selectedlang != -1)) {
167                         nBest = ls->selectedlang;
168                         av = ls->availability;
169                         prio = ls->priority;
170                 }
171         }
172         if (nBest == -1) {
173                 /** fall back to C */
174                 nBest=0;
175         }
176         sess->selected_language = nBest;
177         lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
178         FreeStrBuf(&Buf);
179         FreeStrBuf(&SBuf);
180 }
181
182 /**
183  * \brief show the language chooser on the login dialog
184  * depending on the browser locale change the sequence of the 
185  * language chooser.
186  */
187 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
188 {
189         int i;
190 #ifndef HAVE_USELOCALE
191         char *Lang = getenv("LANG");
192         
193         if (Lang == NULL)
194                 Lang = "C";
195 #endif
196
197
198         if (nLocalesLoaded == 1) {
199                 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
200                 return;
201         }
202
203         wc_printf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
204
205         for (i=0; i < nLocalesLoaded; ++i) {
206 #ifndef HAVE_USELOCALE
207                 if (strcmp(AvailLangLoaded[i], Lang) == 0)
208 #endif
209                 wc_printf("<option %s value=%s>%s</option>\n",
210                         ((WC->selected_language == i) ? "selected" : ""),
211                         AvailLangLoaded[i],
212                         AvailLangLoaded[i]
213                 );
214         }
215
216         wc_printf("</select>\n");
217 }
218
219 /**
220  * \brief Set the selected language for this session.
221  * \param lang the locale to set.
222  */
223 void set_selected_language(const char *lang) {
224 #ifdef HAVE_USELOCALE
225         int i;
226         for (i = 0; i<nLocalesLoaded; ++i) {
227                 if (!strcasecmp(lang, AvailLangLoaded[i])) {
228                         WC->selected_language = i;
229                 }
230         }
231 #endif
232 }
233
234 /**
235  * \brief Activate the selected language for this session.
236  */
237 void go_selected_language(void) {
238 #ifdef HAVE_USELOCALE
239         wcsession *WCC = WC;
240         if (WCC->selected_language < 0) return;
241         uselocale(wc_locales[WCC->selected_language]);  /** switch locales */
242         textdomain(textdomain(NULL));                   /** clear the cache */
243 #else
244         char *language;
245         
246         language = getenv("LANG");
247         setlocale(LC_MESSAGES, language);
248 #endif
249 }
250
251 /**
252  * \brief Deactivate the selected language for this session.
253  */
254 void stop_selected_language(void) {
255 #ifdef HAVE_USELOCALE
256         uselocale(LC_GLOBAL_LOCALE);                    /** switch locales */
257         textdomain(textdomain(NULL));                   /** clear the cache */
258 #endif
259 }
260
261 #ifdef HAVE_USELOCALE
262         locale_t Empty_Locale;
263 #endif
264
265 /**
266  * \brief Create a locale_t for each available language
267  */
268 void initialize_locales(void) {
269         int nLocales;
270         int i;
271         char buf[32];
272         char *language = NULL;
273         char *locale;
274
275
276         nLocales = 0; 
277         while (!IsEmptyStr(AvailLang[nLocales]))
278                 nLocales++;
279
280         language = getenv("WEBCIT_LANG");
281         if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
282                 lprintf(9, "Nailing locale to %s\n", language);
283         }
284         else language = NULL;
285
286         AvailLangLoaded = malloc (sizeof(char*) * nLocales);
287         memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
288 #ifdef HAVE_USELOCALE
289         wc_locales = malloc (sizeof(locale_t) * nLocales);
290         memset(wc_locales,0, sizeof(locale_t) * nLocales);
291         /* create default locale */
292         Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
293 #endif
294
295
296
297
298         for (i = 0; i < nLocales; ++i) {
299                 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
300                         continue;
301                 if (i == 0) {
302                         sprintf(buf, "%s", AvailLang[i]);       /* locale 0 (C) is ascii, not utf-8 */
303                 }
304                 else {
305                         sprintf(buf, "%s.UTF8", AvailLang[i]);
306                 }
307 #ifdef HAVE_USELOCALE
308                 wc_locales[nLocalesLoaded] = newlocale(
309                         (LC_MESSAGES_MASK|LC_TIME_MASK),
310                         buf,
311                         (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
312                 );
313                 if (wc_locales[nLocalesLoaded] == NULL) {
314                         lprintf(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
315                                 buf,
316                                 strerror(errno)
317                         );
318                 }
319                 else {
320                         lprintf(3, "Found locale: %s\n", buf);
321                         AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
322                         nLocalesLoaded++;
323                 }
324 #else
325                 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
326                         setenv("LANG", buf, 1);
327                         AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
328                         setlocale(LC_MESSAGES, AvailLang[i]);
329                         nLocalesLoaded++;
330                 }
331                 else if (nLocalesLoaded == 0) {
332                         setenv("LANG", buf, 1);
333                         AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
334                         nLocalesLoaded++;
335                 }
336 #endif
337         }
338         if ((language != NULL) && (nLocalesLoaded == 0)) {
339                 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
340 #ifdef HAVE_USELOCALE
341                 wc_locales[0] = newlocale(
342                         (LC_MESSAGES_MASK|LC_TIME_MASK),
343                         AvailLang[0],
344                         Empty_Locale);          
345 #else
346                 setlocale(LC_MESSAGES, AvailLang[0]);
347                 setenv("LANG", AvailLang[0], 1);
348 #endif
349                 AvailLangLoaded[0] = AvailLang[0];
350                 nLocalesLoaded = 1;
351         }
352 #ifndef HAVE_USELOCALE
353
354
355 #endif
356
357 #ifdef ENABLE_NLS
358         locale = setlocale(LC_ALL, "");
359
360         lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
361         lprintf(9, "Text domain: %s\n", textdomain("webcit"));
362         lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
363
364 #endif
365 }
366
367
368 void 
369 ServerShutdownModule_GETTEXT
370 (void)
371 {
372 #ifdef HAVE_USELOCALE
373         int i;
374         for (i = 0; i < nLocalesLoaded; ++i) {
375                 if (Empty_Locale != wc_locales[i])
376                         freelocale(wc_locales[i]);
377         }
378         free(wc_locales);
379 #endif
380         free(AvailLangLoaded);
381 }
382
383 #else   /* ENABLE_NLS */
384 const char *AvailLang[] = {
385         "C", ""};
386
387 /** \brief dummy for non NLS enabled systems */
388 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
389 {
390         wc_printf("English (US)");
391 }
392
393 /** \brief dummy for non NLS enabled systems */
394 void set_selected_language(char *lang) {
395 }
396
397 /** \brief dummy for non NLS enabled systems */
398 void go_selected_language(void) {
399 }
400
401 /** \brief dummy for non NLS enabled systems */
402 void stop_selected_language(void) {
403 }
404
405 #endif  /* ENABLE_NLS */
406
407
408 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
409 {
410         StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
411 }
412
413
414 /*
415  * Returns the language currently in use.
416  * This function returns a static string, so don't do anything stupid please.
417  */
418 const char *get_selected_language(void) {
419 #ifdef ENABLE_NLS
420 #ifdef HAVE_USELOCALE
421         return AvailLang[WC->selected_language];
422 #else
423         return "en";
424 #endif
425 #else
426         return "en";
427 #endif
428 }
429
430
431 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
432 {
433         hdr->HR.browser_language = Line;
434 }
435
436 void 
437 InitModule_GETTEXT
438 (void)
439 {
440         initialize_locales();
441         
442         RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"), 
443                               Header_HandleAcceptLanguage);
444                               
445         RegisterNamespace("LANG:SELECT", 0, 0, 
446                           tmplput_offer_languages, NULL, CTX_NONE);
447 }
448
449
450 void
451 SessionNewModule_GETTEXT
452 (wcsession *sess)
453 {
454 #ifdef ENABLE_NLS
455         if (!sess->Hdr->HR.Static && 
456             (sess->Hdr->HR.browser_language != NULL)) {
457                 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
458         }
459 #endif
460 }
461
462 void
463 SessionAttachModule_GETTEXT
464 (wcsession *sess)
465 {
466 #ifdef ENABLE_NLS
467         go_selected_language();                                 /* set locale */
468 #endif
469 }
470
471 void 
472 SessionDestroyModule_GETTEXT
473 (wcsession *sess)
474 {
475 #ifdef ENABLE_NLS
476         stop_selected_language();                               /* unset locale */
477 #endif
478 }