oops, fixed it
[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(char *lang) {
224         int i;
225 #ifdef HAVE_USELOCALE
226         for (i=0; i<NUM_LANGS; ++i) {
227                 if (!strcasecmp(lang, AvailLang[i])) {
228                         WC->selected_language = i;
229                 }
230         }
231 #endif
232 }
233
234 /**
235  * \brief Activate the selected language for this session.
236  */
237 void go_selected_language(void) {
238 #ifdef HAVE_USELOCALE
239         if (WC->selected_language < 0) return;
240         uselocale(wc_locales[WC->selected_language]);   /** switch locales */
241         textdomain(textdomain(NULL));                   /** clear the cache */
242 #else
243         char *language;
244         
245         language = getenv("LANG");
246         setlocale(LC_MESSAGES, language);
247 #endif
248 }
249
250 /**
251  * \brief Deactivate the selected language for this session.
252  */
253 void stop_selected_language(void) {
254 #ifdef HAVE_USELOCALE
255         uselocale(LC_GLOBAL_LOCALE);                    /** switch locales */
256         textdomain(textdomain(NULL));                   /** clear the cache */
257 #endif
258 }
259
260 void preset_locale(void)
261 {
262 #ifndef HAVE_USELOCALE
263 #ifdef HAVE_GETTEXT
264         char *language;
265         
266         lprintf(9, "Nailing locale to %s\n", getenv("LANG"));
267         language = getenv("LANG");
268         setlocale(LC_MESSAGES, language);
269 #endif
270 #endif
271 }
272 /**
273  * \brief Create a locale_t for each available language
274  */
275 void initialize_locales(void) {
276         int i;
277         char buf[32];
278
279 #ifdef HAVE_USELOCALE
280         locale_t Empty_Locale;
281
282         /* create default locale */
283         Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
284 #endif
285
286         for (i = 0; i < NUM_LANGS; ++i) {
287                 if (i == 0) {
288                         sprintf(buf, "%s", AvailLang[i]);       // locale 0 (C) is ascii, not utf-8
289                 }
290                 else {
291                         sprintf(buf, "%s.UTF8", AvailLang[i]);
292                 }
293 #ifdef HAVE_USELOCALE
294                 wc_locales[i] = newlocale(
295                         (LC_MESSAGES_MASK|LC_TIME_MASK),
296                         buf,
297                         (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
298                 );
299                 if (wc_locales[i] == NULL) {
300                         lprintf(1, "Error configuring locale for %s: %s\n",
301                                 buf,
302                                 strerror(errno)
303                         );
304                 }
305                 else {
306                         lprintf(3, "Configured available locale: %s\n", buf);
307                 }
308 #endif
309         }
310 }
311
312
313 #else   /* ENABLE_NLS */
314 /** \brief dummy for non NLS enabled systems */
315 void offer_languages(void) {
316         wprintf("English (US)");
317 }
318
319 /** \brief dummy for non NLS enabled systems */
320 void set_selected_language(char *lang) {
321 }
322
323 /** \brief dummy for non NLS enabled systems */
324 void go_selected_language(void) {
325 }
326
327 /** \brief dummy for non NLS enabled systems */
328 void stop_selected_language(void) {
329 }
330
331 void preset_locale(void)
332 {
333 }
334 #endif  /* ENABLE_NLS */
335
336