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