* now template tokens can work as functions, you can add params to them after in...
[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(char *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         char search[1024];
67         
68         safestrncpy(search, LocaleString, sizeof search);
69         nParts=num_tokens(search,',');
70         for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
71         {
72                         char buf[16];
73                         char sbuf[16];
74                         char lbuf[16];
75                         int blen;
76                         
77                         ls=&wanted_locales[i];
78
79                         extract_token(&buf[0],search, i,',',16);
80                         /** we are searching, if this list item has something like ;q=n*/
81                         if (num_tokens(&buf[0],'=')>1) {
82                                 int sbuflen, k;
83                                 extract_token(&sbuf[0],&buf[0], 1,'=',16);
84                                 sbuflen=strlen(&sbuf[0]);
85                                 for (k=0; k<sbuflen; k++) if (sbuf[k]=='.') sbuf[k]='0';
86                                 ls->priority=atol(&sbuf[0]);
87                         }
88                         else {
89                                 ls->priority=1000;
90                         }
91                         /** get the locale part */
92                         extract_token(&sbuf[0],&buf[0],0,';',16);
93                         /** get the lang part, which should be allways there */
94                         extract_token(&ls->lang[0],&sbuf[0],0,'-',16);
95                         /** get the area code if any. */
96                         if (num_tokens(&sbuf[0],'-')>1) {
97                                 extract_token(&ls->region[0],&sbuf[0],1,'-',16);
98                         }
99                         else { /** no ara code? use lang code */
100                                 blen=strlen(&ls->lang[0]);
101                                 memcpy(&ls->region[0], ls->lang,blen);
102                                 ls->region[blen]='\0';
103                         } /** area codes are uppercase */
104                         blen=strlen(&ls->region[0]);
105                         for (j=0; j<blen; j++)
106                                 {
107                                         int chars=toupper(ls->region[j]);
108                                         ls->region[j]=(char)chars;/** \todo ?! */
109                                 }
110                         sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
111                         
112                         /** check if we have this lang */
113                         ls->availability=1;
114                         ls->selectedlang=-1;
115                         for (j=0; j<nLocalesLoaded; j++) {
116                                 int result;
117                                 /** match against the LANG part */
118                                 result=strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
119                                 if ((result<0)&&(result<ls->availability)){
120                                         ls->availability=result;
121                                         ls->selectedlang=j;
122                                 }
123                                 /** match against lang and locale */
124                                 if (0==strcasecmp(&lbuf[0], AvailLangLoaded[j])){
125                                         ls->availability=0;
126                                         ls->selectedlang=j;
127                                         j=nLocalesLoaded;
128                                 }
129                         }
130         }
131         
132         prio=0;
133         av=-1000;
134         nBest=-1;
135         for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++) {
136                 ls=&wanted_locales[i];
137                 if ((ls->availability<=0)&& 
138                    (av<ls->availability)&&
139                    (prio<ls->priority)&&
140                    (ls->selectedlang!=-1)) {
141                         nBest=ls->selectedlang;
142                         av=ls->availability;
143                         prio=ls->priority;
144                 }
145         }
146         if (nBest == -1) {
147                 /** fall back to C */
148                 nBest=0;
149         }
150         WC->selected_language=nBest;
151         lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
152 }
153
154 /* TODO: we skip the language weighting so far. */
155 /* Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' */
156 /* Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 */
157 //void httplang_to_locale(char *LocaleString)
158 //{
159 //      char selected_locale[16];
160 //      int i, j;
161 //      char lang[64];
162 //      int num_accept = 0;
163 //
164 //      lprintf(9, "languageAccept: %s\n", LocaleString);
165 //
166 //      strcpy(selected_locale, "C");
167 //      num_accept = num_tokens(LocaleString, ',');
168 //
169 //      for (i=num_accept-1; i>=0; --i) {
170 //              extract_token(lang, LocaleString, i, ',', sizeof lang);
171 //
172 //              /* Strip out the weights; we don't use them.  Also convert
173 //               * hyphens to underscores.
174 //               */
175 //              for (j=0; j<strlen(lang); ++j) {
176 //                      if (lang[j] == '-') lang[j] = '_';
177 //                      if (lang[j] == ';') lang[j] = 0;
178 //              }
179 //
180 //              for (j=0; j<NUM_LANGS; ++j) {
181 //                      if (!strncasecmp(lang, AvailLang[j], strlen(lang))) {
182 //                              strcpy(selected_locale, AvailLang[j]);
183 //                      }
184 //              }
185 //      }
186 //
187 //      lprintf(9, "language found: %s\n", selected_locale);
188 //      set_selected_language(selected_locale);
189 //}
190
191
192 /**
193  * \brief show the language chooser on the login dialog
194  * depending on the browser locale change the sequence of the 
195  * language chooser.
196  */
197 void offer_languages(void) {
198         int i;
199 #ifndef HAVE_USELOCALE
200         char *Lang = getenv("LANG");
201         
202         if (Lang == NULL)
203                 Lang = "C";
204 #endif
205
206         wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
207
208         for (i=0; i < nLocalesLoaded; ++i) {
209 #ifndef HAVE_USELOCALE
210                 if (strcmp(AvailLangLoaded[i], Lang) == 0)
211 #endif
212                 wprintf("<option %s value=%s>%s</option>\n",
213                         ((WC->selected_language == i) ? "selected" : ""),
214                         AvailLangLoaded[i],
215                         AvailLangLoaded[i]
216                 );
217         }
218
219         wprintf("</select>\n");
220 }
221
222 /**
223  * \brief Set the selected language for this session.
224  * \param lang the locale to set.
225  */
226 void set_selected_language(const char *lang) {
227         int i;
228
229 #ifdef HAVE_USELOCALE
230         for (i=0; i<nLocalesLoaded; ++i) {
231                 if (!strcasecmp(lang, AvailLangLoaded[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         struct wcsession *WCC = WC;
244         if (WCC->selected_language < 0) return;
245         uselocale(wc_locales[WCC->selected_language]);  /** switch locales */
246         textdomain(textdomain(NULL));                   /** clear the cache */
247 #else
248         char *language;
249         
250         language = getenv("LANG");
251         setlocale(LC_MESSAGES, language);
252 #endif
253 }
254
255 /**
256  * \brief Deactivate the selected language for this session.
257  */
258 void stop_selected_language(void) {
259 #ifdef HAVE_USELOCALE
260         uselocale(LC_GLOBAL_LOCALE);                    /** switch locales */
261         textdomain(textdomain(NULL));                   /** clear the cache */
262 #endif
263 }
264
265 void preset_locale(void)
266 {
267 #ifndef HAVE_USELOCALE
268 #ifdef HAVE_GETTEXT
269         char *language;
270         
271         lprintf(9, "Nailing locale to %s\n", getenv("LANG"));
272         language = getenv("LANG");
273         setlocale(LC_MESSAGES, language);
274 #endif
275 #endif
276 }
277
278 #ifdef HAVE_USELOCALE
279         locale_t Empty_Locale;
280 #endif
281
282 /**
283  * \brief Create a locale_t for each available language
284  */
285 void initialize_locales(void) {
286         int i;
287         char buf[32];
288
289 #ifdef HAVE_USELOCALE
290         /* create default locale */
291         Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
292 #endif
293
294         for (i = 0; i < NUM_LANGS; ++i) {
295                 if (i == 0) {
296                         sprintf(buf, "%s", AvailLang[i]);       // locale 0 (C) is ascii, not utf-8
297                 }
298                 else {
299                         sprintf(buf, "%s.UTF8", AvailLang[i]);
300                 }
301 #ifdef HAVE_USELOCALE
302                 wc_locales[nLocalesLoaded] = newlocale(
303                         (LC_MESSAGES_MASK|LC_TIME_MASK),
304                         buf,
305                         (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
306                 );
307                 if (wc_locales[nLocalesLoaded] == NULL) {
308                         lprintf(1, "Error configuring locale for %s: %s\n",
309                                 buf,
310                                 strerror(errno)
311                         );
312                 }
313                 else {
314                         lprintf(3, "Configured available locale: %s\n", buf);
315                         AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
316                         nLocalesLoaded++;
317                 }
318 #endif
319         }
320 }
321
322
323 void ShutdownLocale(void)
324 {
325         int i;
326 #ifdef HAVE_USELOCALE
327         for (i = 0; i < nLocalesLoaded; ++i) {
328                 if (Empty_Locale != wc_locales[i])
329                         freelocale(wc_locales[i]);
330         }
331         freelocale(Empty_Locale);
332 #endif
333 }
334
335 #else   /* ENABLE_NLS */
336 /** \brief dummy for non NLS enabled systems */
337 void offer_languages(void) {
338         wprintf("English (US)");
339 }
340
341 /** \brief dummy for non NLS enabled systems */
342 void set_selected_language(char *lang) {
343 }
344
345 /** \brief dummy for non NLS enabled systems */
346 void go_selected_language(void) {
347 }
348
349 /** \brief dummy for non NLS enabled systems */
350 void stop_selected_language(void) {
351 }
352
353 void preset_locale(void)
354 {
355 }
356 #endif  /* ENABLE_NLS */
357
358
359 void TmplGettext(StrBuf *Target, int nTokens, WCTemplateToken *Token)
360 {
361         StrBufAppendBufPlain(Target, _(Token->Params[0]->Start), -1, 0);
362
363 }