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