* Use tokenizer function to completely parse accept-lang header.
[citadel.git] / webcit / gettext.c
1 #define _GNU_SOURCE
2 #include "webcit.h"
3 #include "webserver.h"
4
5 #ifdef ENABLE_NLS
6
7 static const char *AvailLang[] = {
8         "de_DE"
9 };
10
11 typedef struct _lang_pref{
12         char lang[16];
13         char region[16];
14         char *match;
15         double Priority;
16 } LangStruct;
17
18 /* TODO: we skip the language weightening so far. */
19 /* Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' */
20 /* Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 */
21 void httplang_to_locale(const char *LocaleString)
22 {
23         char *locale = "C";
24         LangStruct wanted_locales[20];
25         int i = 0;
26         int j = 0;
27         size_t len = strlen(LocaleString);
28         int nFound = 0;
29         int nParts;
30         const int nAvail = 1; /* Number of members in AvailLang */
31         char *search = (char *) malloc(len);
32         locale_t my_Locale;
33         locale_t my_Empty_Locale;
34
35         memcpy(search, LocaleString, len);
36         search[len] = '\0';
37         nParts=num_tokens(search,',');
38         for (i=0; ((i<nParts)&&(i<10)); i++)
39         {
40                 char buf[16];
41                 char sbuf[16];
42                 int blen;
43
44                 extract_token(&buf[0],search, 0,',',16);
45                 /* we are searching, if this list item has something like ;q=n*/
46                 if (num_tokens(&buf[0],'=')>1) {
47                         extract_token(&sbuf[0],&buf[0], 1,'=',16);
48                         wanted_locales[i].Priority=atof(&sbuf[0]);
49                 }
50                 else {
51                         wanted_locales[i].Priority=1.0;
52                 }
53                 /* get the locale part */
54                 extract_token(&sbuf[0],&buf[0],0,';',16);
55                 /* get the lang part, which should be allways there */
56                 extract_token(&wanted_locales[i].lang[0],&sbuf[0],0,'-',16);
57                 /* get the area code if any. */
58                 if (num_tokens(&sbuf[0],'-')>1) {
59                         extract_token(&wanted_locales[i].region[0],&sbuf[0],1,'-',16);
60                 }
61                 else { /* no ara code? use lang code */
62                         blen=strlen(&wanted_locales[i].lang[0]);
63                         memcpy(&wanted_locales[i].region[0], wanted_locales[i].lang,blen);
64                         wanted_locales[i].region[blen]='\0';
65                 } /* area codes are uppercase */
66                 blen=strlen(&wanted_locales[i].region[0]);
67                 for (j=0; j<blen; j++)
68                         {
69                                 int chars=toupper(wanted_locales[i].region[j]);
70                                 wanted_locales[i].region[j]=(char)chars;/*todo ?! */
71                         }
72         }
73
74         /* todo: weight  */
75         if (nFound > 0) {
76                 for (i = 0; i <= nFound; i++) {
77                         for (j = 0; j < nAvail; j++) {
78                                 int ret = strncasecmp(&wanted_locales[i].lang[0],
79                                                                           AvailLang[j],
80                                                                           strlen(&wanted_locales[i].lang[0]));
81                                 if (!ret) {
82                                         locale = (char *) AvailLang[j]; //wanted_locales[i];
83                                         i = nFound + 1;
84                                         j = nAvail + 1;
85                                         continue;
86                                 }
87                                 
88                         }
89                 }
90         }
91
92         len = strlen(locale);
93         memcpy(search, locale, len);
94         memcpy(&search[len], ".UTF8", 5);
95         search[len + 5] = '\0';
96         my_Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);   /* create default locale */
97         my_Locale = newlocale(LC_MESSAGES_MASK /*|LC_TIME_MASK FIXME */ ,
98                               search, my_Empty_Locale);
99
100         uselocale(my_Locale);
101         //      freelocale(my_Locale);
102         //      freelocale(my_Empty_Locale);
103         free(search);
104 }
105
106 #endif
107
108
109 /*
110         // the web browser sends '-', we need '_' 
111
112                 for (i = 0; i < len; i++)
113                 if (search[i] == '-')
114                         search[i] = '_';
115
116         nFound=i;
117         nParts=num_tokens(search,',');
118         if (nParts>0)
119                 {
120                         extract_token(prefers,search, 1,';',len);
121                         extract_token(langs,search, 0,';',len);
122                 }
123         else
124                 {
125                         free(prefers);
126                         prefers=NULL;
127                         memcpy(search, len);
128                         search[len] = '\0';
129                 }
130         i = 0;
131         while ( !done && (nFound < 10)) {
132                 if ((search[i] == ',') || (search[i] == ';') || (search[i] == '\0'))
133                 {
134                         if ((search[i] == ';') || (search[i] == '\0'))
135                                 done = 1;
136                         search[i] = '\0';
137                         wanted_locales[nFound] = (char *) &search[j];
138                         j = i + 1;
139                         nFound++;
140                 }
141
142                 i++;
143         }
144 */