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