* fix non locale enabled build
[citadel.git] / webcit / gettext.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 1996-2010 by the citadel.org team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #define SEARCH_LANG 20          /* how many langs should we parse? */
24
25 #ifdef ENABLE_NLS
26 /* actual supported locales */
27 const char *AvailLang[] = {
28         "C",
29         "en_US",
30         "de_DE",
31         "it_IT",
32         "es_ES",
33         "en_GB",
34         "da_DK",
35         "fr_FR",
36         "nl_NL",
37         "pt_BR",
38         "hu_HU",
39         "et_EE",
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 #ifndef HAVE_USELOCALE
368
369
370 #endif
371
372 #ifdef ENABLE_NLS
373         locale = setlocale(LC_ALL, "");
374
375         lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
376         lprintf(9, "Text domain: %s\n", textdomain("webcit"));
377         lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
378
379 #endif
380 }
381
382
383 void 
384 ServerShutdownModule_GETTEXT
385 (void)
386 {
387 #ifdef HAVE_USELOCALE
388         int i;
389         for (i = 0; i < nLocalesLoaded; ++i) {
390                 if (Empty_Locale != wc_locales[i])
391                         freelocale(wc_locales[i]);
392         }
393         free(wc_locales);
394 #endif
395         free(AvailLangLoaded);
396 }
397
398 #else   /* ENABLE_NLS */
399 const char *AvailLang[] = {
400         "C", ""};
401
402 /* dummy for non NLS enabled systems */
403 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
404 {
405         wc_printf("English (US)");
406 }
407
408 /* dummy for non NLS enabled systems */
409 void set_selected_language(const char *lang) {
410 }
411
412 /* dummy for non NLS enabled systems */
413 void go_selected_language(void) {
414 }
415
416 /* dummy for non NLS enabled systems */
417 void stop_selected_language(void) {
418 }
419
420 void initialize_locales(void) {
421 }
422
423 #endif  /* ENABLE_NLS */
424
425
426 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
427 {
428         StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
429 }
430
431
432 /*
433  * Returns the language currently in use.
434  * This function returns a static string, so don't do anything stupid please.
435  */
436 const char *get_selected_language(void) {
437 #ifdef ENABLE_NLS
438 #ifdef HAVE_USELOCALE
439         return AvailLang[WC->selected_language];
440 #else
441         return "en";
442 #endif
443 #else
444         return "en";
445 #endif
446 }
447
448
449 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
450 {
451         hdr->HR.browser_language = Line;
452 }
453
454 void 
455 InitModule_GETTEXT
456 (void)
457 {
458         initialize_locales();
459         
460         RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"), 
461                               Header_HandleAcceptLanguage);
462                               
463         RegisterNamespace("LANG:SELECT", 0, 0, 
464                           tmplput_offer_languages, NULL, CTX_NONE);
465 }
466
467
468 void
469 SessionNewModule_GETTEXT
470 (wcsession *sess)
471 {
472 #ifdef ENABLE_NLS
473         if (!sess->Hdr->HR.Static && 
474             (sess->Hdr->HR.browser_language != NULL)) {
475                 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
476         }
477 #endif
478 }
479
480 void
481 SessionAttachModule_GETTEXT
482 (wcsession *sess)
483 {
484 #ifdef ENABLE_NLS
485         go_selected_language();                                 /* set locale */
486 #endif
487 }
488
489 void 
490 SessionDestroyModule_GETTEXT
491 (wcsession *sess)
492 {
493 #ifdef ENABLE_NLS
494         stop_selected_language();                               /* unset locale */
495 #endif
496 }