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