* Detect locale from Browser Environment
[citadel.git] / webcit / gettext.c
1 #include "webcit.h"
2 #include "webserver.h"
3
4 static const char* AvailLang[]=
5         {
6                 "de_DE"
7
8
9         };
10
11 /* TODO: we skip the language weightening so far. */
12 /* Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' */
13 void httplang_to_locale(const char* LocaleString)
14 {
15         char *locale="C";
16         char *wanted_locales[10];
17         int i=0;
18         int j=0;
19         size_t len=strlen(LocaleString);
20         int nFound=0;
21         int nAvail=1;
22         char *search=(char*)malloc(len);
23         int done=0;
24         char *mo;
25         char *webcitdir = WEBCITDIR;
26
27         memcpy(search,LocaleString,len);
28         search[len+1]='\0';
29         len=strlen(search);
30         /* the web browser sends '-', we need '_' */
31         for (i=0;i<len; i++)
32                 if (search[i]=='-') search[i]='_';
33         i=0;
34         while ((search[i]!='\0')&&
35                    !done &&
36                    (nFound<10))
37                 {
38                         if ((search[i]==',')||(search[i]==';'))
39                 
40                                 {
41                                         if (search[i]==';') done=1;
42                                         search[i]='\0';
43                                         wanted_locales[nFound]=(char*)&search[j];
44                                         j=i+1;
45                                         nFound++;
46                                 }
47                         
48                         i++;
49                 }
50         /* todo: weight  */
51
52         for (i=0; i<=nFound; i++)
53                 {
54                         for (j=0;j<nAvail; j++)
55                                 {
56                                         int ret=strncasecmp(wanted_locales[i],
57                                                                                 AvailLang[j],
58                                                                                 strlen(wanted_locales[i]));
59                                         if (!ret)
60                                                 {
61                                                         locale=AvailLang[j];//wanted_locales[i];
62                                                         i=nFound+1;
63                                                         j=nAvail+1;
64                                                         continue;
65                                                 }
66
67                                 }
68                 }
69         setlocale(LC_ALL,"C");
70         len=strlen(locale);
71         memcpy(search,locale,len);
72         memcpy(&search[len],".UTF8",5);
73         search[len+5]='\0';
74         /*
75         len=strlen(search);
76         mo=malloc(len+1);
77         memcpy(mo,search,len+1);
78         */
79         /*
80         mo = malloc(strlen(webcitdir) + 20);
81         sprintf(mo, "%s/locale", webcitdir);
82
83         lprintf(9, "Message catalog directory: %s\n",
84                 bindtextdomain("webcit", mo)
85                 );
86         free(mo);
87         
88         lprintf(9, "Text domain: %s\n",
89                 textdomain("webcit")
90                 );
91         
92         lprintf(9, "Text domain Charset: %s\n",
93                         bind_textdomain_codeset("webcit","UTF8")
94                         );
95         
96         */
97         //setlocale(LC_MESSAGES,mo);//search);
98         setlocale(LC_MESSAGES,search);
99         //      free(mo);
100         free(search);
101
102 }