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