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