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