* fixed langage detection to work with simple 'de' and no other stuff
[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 /* 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         locale_t my_Locale;
27         locale_t my_Empty_Locale;
28
29         //      len = strlen(LocaleString);
30         memcpy(search, LocaleString, len);
31         search[len] = '\0';
32         /* the web browser sends '-', we need '_' */
33         for (i = 0; i < len; i++)
34                 if (search[i] == '-')
35                         search[i] = '_';
36         i = 0;
37         while ( !done && (nFound < 10)) {
38                 if ((search[i] == ',') || (search[i] == ';') || (search[i] == '\0'))
39                 {
40                         if ((search[i] == ';') || (search[i] == '\0'))
41                                 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         if (nFound > 0) for (i = 0; i <= nFound; i++) {
53                 for (j = 0; j < nAvail; j++) {
54                         int ret = strncasecmp(wanted_locales[i],
55                                               AvailLang[j],
56                                               strlen(wanted_locales[i]));
57                         if (!ret) {
58                                 locale = (char *) AvailLang[j]; //wanted_locales[i];
59                                 i = nFound + 1;
60                                 j = nAvail + 1;
61                                 continue;
62                         }
63
64                 }
65         }
66
67         len = strlen(locale);
68         memcpy(search, locale, len);
69         memcpy(&search[len], ".UTF8", 5);
70         search[len + 5] = '\0';
71         my_Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);   /* create default locale */
72         my_Locale = newlocale(LC_MESSAGES_MASK /*|LC_TIME_MASK FIXME */ ,
73                               search, my_Empty_Locale);
74
75         uselocale(my_Locale);
76         //      freelocale(my_Locale);
77         //      freelocale(my_Empty_Locale);
78         free(search);
79 }
80
81 #endif