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