* Added a null pointer check to the 'language' variable to prevent crash
[citadel.git] / webcit / gettext.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7
8 #ifdef ENABLE_NLS
9 /* actual supported locales */
10 const char *AvailLang[NUM_LANGS] = {
11         "C",
12         "en_US",
13         "de_DE",
14         "it_IT",
15         "es_ES",
16         "en_GB",
17         "da_DK",
18         "fr_FR",
19         "nl_NL",
20         "pt_BR"
21 };
22
23 const char *AvailLangLoaded[NUM_LANGS];
24 long nLocalesLoaded = 0;
25
26 #ifdef HAVE_USELOCALE
27 locale_t wc_locales[NUM_LANGS]; /**< here we keep the parsed stuff */
28 #endif
29
30 /** Keep information about one locale */
31 typedef struct _lang_pref{
32         char lang[16];          /**< the language locale string */
33         char region[16];        /**< the region locale string */
34         long priority;          /**< which priority does it have */
35         int availability;       /**< do we know it? */
36         int selectedlang;       /**< is this the selected language? */
37 } LangStruct;
38
39 /* \brief parse browser locale header 
40  * seems as most browsers just do a one after coma value even if more than 10 locales are available. Sample strings:
41  * opera: 
42  * 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 
43  * Firefox 
44  * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' 
45  * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 
46  * 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 
47  * \param LocaleString the string from the browser http headers
48  */
49
50 void httplang_to_locale(StrBuf *LocaleString)
51 {
52         LangStruct wanted_locales[SEARCH_LANG];
53         LangStruct *ls;
54
55         int i = 0;
56         int j = 0;
57         /* size_t len = strlen(LocaleString); */
58         long prio;
59         int av;
60         int nBest;
61         int nParts;
62         StrBuf *Buf = NULL;
63         StrBuf *SBuf = NULL;
64         
65         nParts=StrBufNum_tokens(LocaleString,',');
66         for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
67         {
68                 char lbuf[16];
69                 int blen;
70                         
71                 if (Buf == NULL) {
72                         Buf = NewStrBuf();
73                         SBuf = NewStrBuf();
74                 }
75                 else {
76                         FlushStrBuf(Buf);
77                         FlushStrBuf(SBuf);
78                 }
79
80                 ls=&wanted_locales[i];
81
82                 StrBufExtract_token(Buf,LocaleString, i,',');
83                 /** we are searching, if this list item has something like ;q=n*/
84                 if (StrBufNum_tokens(Buf,'=')>1) {
85                         int sbuflen, k;
86                         StrBufExtract_token(SBuf,Buf, 1,'=');
87                         sbuflen=StrLength(SBuf);
88                         for (k=0; k<sbuflen; k++) 
89                                 if (ChrPtr(SBuf)[k]=='.') 
90                                         StrBufPeek(SBuf, NULL, k, '0');
91                         ls->priority=StrTol(SBuf);
92                 }
93                 else {
94                         ls->priority=1000;
95                 }
96                 /** get the locale part */
97                 StrBufExtract_token(SBuf ,Buf, 0, ';');
98                 /** get the lang part, which should be allways there */
99                 extract_token(&ls->lang[0], ChrPtr(SBuf), 0, '-', 16);
100                 /** get the area code if any. */
101                 if (StrBufNum_tokens(SBuf,'-') > 1) {
102                         extract_token(&ls->region[0],ChrPtr(SBuf),1,'-',16);
103                 }
104                 else { /** no ara code? use lang code */
105                         blen=strlen(&ls->lang[0]);
106                         memcpy(&ls->region[0], ls->lang,blen);
107                         ls->region[blen]='\0';
108                 } /** area codes are uppercase */
109                 blen=strlen(&ls->region[0]);
110                 for (j=0; j<blen; j++)
111                 {
112                         int chars=toupper(ls->region[j]);
113                         ls->region[j]=(char)chars;/** \todo ?! */
114                 }
115                 sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
116                         
117                 /** check if we have this lang */
118                 ls->availability=1;
119                 ls->selectedlang=-1;
120                 for (j=0; j<nLocalesLoaded; j++) {
121                         int result;
122                         /** match against the LANG part */
123                         result=strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
124                         if ((result<0)&&(result<ls->availability)){
125                                 ls->availability=result;
126                                 ls->selectedlang=j;
127                         }
128                         /** match against lang and locale */
129                         if (0==strcasecmp(&lbuf[0], AvailLangLoaded[j])){
130                                 ls->availability=0;
131                                 ls->selectedlang=j;
132                                 j=nLocalesLoaded;
133                         }
134                 }
135         }
136         
137         prio=0;
138         av=-1000;
139         nBest=-1;
140         for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++) {
141                 ls=&wanted_locales[i];
142                 if ((ls->availability<=0)&& 
143                    (av<ls->availability)&&
144                    (prio<ls->priority)&&
145                    (ls->selectedlang!=-1)) {
146                         nBest=ls->selectedlang;
147                         av=ls->availability;
148                         prio=ls->priority;
149                 }
150         }
151         if (nBest == -1) {
152                 /** fall back to C */
153                 nBest=0;
154         }
155         WC->selected_language=nBest;
156         lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
157         FreeStrBuf(&Buf);
158         FreeStrBuf(&SBuf);
159 }
160
161 /**
162  * \brief show the language chooser on the login dialog
163  * depending on the browser locale change the sequence of the 
164  * language chooser.
165  */
166 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
167 {
168         int i;
169 #ifndef HAVE_USELOCALE
170         char *Lang = getenv("LANG");
171         
172         if (Lang == NULL)
173                 Lang = "C";
174 #endif
175
176         wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
177
178         for (i=0; i < nLocalesLoaded; ++i) {
179 #ifndef HAVE_USELOCALE
180                 if (strcmp(AvailLangLoaded[i], Lang) == 0)
181 #endif
182                 wprintf("<option %s value=%s>%s</option>\n",
183                         ((WC->selected_language == i) ? "selected" : ""),
184                         AvailLangLoaded[i],
185                         AvailLangLoaded[i]
186                 );
187         }
188
189         wprintf("</select>\n");
190 }
191
192 /**
193  * \brief Set the selected language for this session.
194  * \param lang the locale to set.
195  */
196 void set_selected_language(const char *lang) {
197         int i;
198
199 #ifdef HAVE_USELOCALE
200         for (i=0; i<nLocalesLoaded; ++i) {
201                 if (!strcasecmp(lang, AvailLangLoaded[i])) {
202                         WC->selected_language = i;
203                 }
204         }
205 #endif
206 }
207
208 /**
209  * \brief Activate the selected language for this session.
210  */
211 void go_selected_language(void) {
212 #ifdef HAVE_USELOCALE
213         wcsession *WCC = WC;
214         if (WCC->selected_language < 0) return;
215         uselocale(wc_locales[WCC->selected_language]);  /** switch locales */
216         textdomain(textdomain(NULL));                   /** clear the cache */
217 #else
218         char *language;
219         
220         language = getenv("LANG");
221         setlocale(LC_MESSAGES, language);
222 #endif
223 }
224
225 /**
226  * \brief Deactivate the selected language for this session.
227  */
228 void stop_selected_language(void) {
229 #ifdef HAVE_USELOCALE
230         uselocale(LC_GLOBAL_LOCALE);                    /** switch locales */
231         textdomain(textdomain(NULL));                   /** clear the cache */
232 #endif
233 }
234
235 void preset_locale(void)
236 {
237 #ifdef HAVE_GETTEXT
238         char *language;
239         
240         language = getenv("WEBCIT_LANG");
241         if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
242                 lprintf(9, "Nailing locale to %s\n", language);
243                 setlocale(LC_MESSAGES, language);
244         }
245 #endif
246 }
247
248 #ifdef HAVE_USELOCALE
249         locale_t Empty_Locale;
250 #endif
251
252 /**
253  * \brief Create a locale_t for each available language
254  */
255 void initialize_locales(void) {
256         int i;
257         char buf[32];
258
259 #ifdef HAVE_USELOCALE
260         /* create default locale */
261         Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
262 #endif
263
264         for (i = 0; i < NUM_LANGS; ++i) {
265                 if (i == 0) {
266                         sprintf(buf, "%s", AvailLang[i]);       /* locale 0 (C) is ascii, not utf-8 */
267                 }
268                 else {
269                         sprintf(buf, "%s.UTF8", AvailLang[i]);
270                 }
271 #ifdef HAVE_USELOCALE
272                 wc_locales[nLocalesLoaded] = newlocale(
273                         (LC_MESSAGES_MASK|LC_TIME_MASK),
274                         buf,
275                         (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
276                 );
277                 if (wc_locales[nLocalesLoaded] == NULL) {
278                         lprintf(1, "Error configuring locale for %s: %s\n",
279                                 buf,
280                                 strerror(errno)
281                         );
282                 }
283                 else {
284                         lprintf(3, "Configured available locale: %s\n", buf);
285                         AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
286                         nLocalesLoaded++;
287                 }
288 #endif
289         }
290 }
291
292
293 void ShutdownLocale(void)
294 {
295         int i;
296 #ifdef HAVE_USELOCALE
297         for (i = 0; i < nLocalesLoaded; ++i) {
298                 if (Empty_Locale != wc_locales[i])
299                         freelocale(wc_locales[i]);
300         }
301 #endif
302 }
303
304 #else   /* ENABLE_NLS */
305 const char *AvailLang[NUM_LANGS] = {
306         "C"};
307
308 /** \brief dummy for non NLS enabled systems */
309 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
310 {
311         wprintf("English (US)");
312 }
313
314 /** \brief dummy for non NLS enabled systems */
315 void set_selected_language(char *lang) {
316 }
317
318 /** \brief dummy for non NLS enabled systems */
319 void go_selected_language(void) {
320 }
321
322 /** \brief dummy for non NLS enabled systems */
323 void stop_selected_language(void) {
324 }
325
326 void preset_locale(void)
327 {
328 }
329 #endif  /* ENABLE_NLS */
330
331
332 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
333 {
334         StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
335 }
336
337
338 /*
339  * Returns the language currently in use.
340  * This function returns a static string, so don't do anything stupid please.
341  */
342 const char *get_selected_language(void) {
343 #ifdef ENABLE_NLS
344 #ifdef HAVE_USELOCALE
345         return AvailLang[WC->selected_language];
346 #else
347         return "en"
348 #endif
349 #else
350         return "en"
351 #endif
352 }
353
354 void 
355 InitModule_GETTEXT
356 (void)
357 {
358         RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);
359 }