]> code.citadel.org Git - citadel.git/blobdiff - webcit/gettext.c
* tiny 'file'-extract, now we can detect the type of an image. and set the mimetype...
[citadel.git] / webcit / gettext.c
index 36e7548b3fc943068d0b55f21f3ac72a09998827..37ef96545f418cc51afd6586be8f63aa2476f826 100644 (file)
@@ -1,36 +1,53 @@
-#define _GNU_SOURCE
+/*
+ * $Id
+ */
+/**
+ * \defgroup LocaleHeaderParser Parse the browser http locale headers and set the NLS stuff.
+ * \ingroup WebcitHttpServer 
+ */
+/*@{*/
 #include "webcit.h"
 #include "webserver.h"
 
 #ifdef ENABLE_NLS
 
-#define NUM_LANGS 4
-#define SEARCH_LANG 20
+#define NUM_LANGS 9 /**< how many different locales do we know? */
+#define SEARCH_LANG 20 /**< how many langs should we parse? */
 
+/** actual supported locales */
 char *AvailLang[NUM_LANGS] = {
        "C",
        "en_US",
        "de_DE",
-       "it_IT"
+       "it_IT",
+       "es_ES",
+       "en_GB",
+       "da_DK",
+       "fr_FR",
+       "nl_NL"
 };
 
-locale_t wc_locales[NUM_LANGS];
+locale_t wc_locales[NUM_LANGS]; /**< here we keep the parsed stuff */
 
+/** Keep information about one locale */
 typedef struct _lang_pref{
-       char lang[16];
-       char region[16];
-       long priority;
-       int availability;
-       int selectedlang;
+       char lang[16];          /**< the language locale string */
+       char region[16];        /**< the region locale string */
+       long priority;          /**< which priority does it have */
+       int availability;       /**< do we know it? */
+       int selectedlang;       /**< is this the selected language? */
 } LangStruct;
 
-/* seems as most browsers just do a one after coma value even if more than 10 locales are available. */
-/* opera: */
-/* 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 */
-/* Firefox */
-/* Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' */
-/* Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 */
-/* 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 */
+/* \brief parse browser locale header 
+ * seems as most browsers just do a one after coma value even if more than 10 locales are available. Sample strings:
+ * opera: 
+ * 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 
+ * Firefox 
+ * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' 
+ * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 
+ * 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 
+ * \param LocaleString the string from the browser http headers
+ */
 
 void httplang_to_locale(char *LocaleString)
 {
@@ -39,17 +56,14 @@ void httplang_to_locale(char *LocaleString)
 
        int i = 0;
        int j = 0;
-       size_t len = strlen(LocaleString);
+       /* size_t len = strlen(LocaleString); */
        long prio;
        int av;
        int nBest;
        int nParts;
-       char *search = (char *) malloc(len);
-       // locale_t my_Locale;
-       // locale_t my_Empty_Locale;
+       char search[1024];
        
-       memcpy(search, LocaleString, len);
-       search[len] = '\0';
+       safestrncpy(search, LocaleString, sizeof search);
        nParts=num_tokens(search,',');
        for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
         {
@@ -61,7 +75,7 @@ void httplang_to_locale(char *LocaleString)
                        ls=&wanted_locales[i];
 
                        extract_token(&buf[0],search, i,',',16);
-                       /* we are searching, if this list item has something like ;q=n*/
+                       /** we are searching, if this list item has something like ;q=n*/
                        if (num_tokens(&buf[0],'=')>1) {
                                int sbuflen, k;
                                extract_token(&sbuf[0],&buf[0], 1,'=',16);
@@ -72,39 +86,39 @@ void httplang_to_locale(char *LocaleString)
                        else {
                                ls->priority=1000;
                        }
-                       /* get the locale part */
+                       /** get the locale part */
                        extract_token(&sbuf[0],&buf[0],0,';',16);
-                       /* get the lang part, which should be allways there */
+                       /** get the lang part, which should be allways there */
                        extract_token(&ls->lang[0],&sbuf[0],0,'-',16);
-                       /* get the area code if any. */
+                       /** get the area code if any. */
                        if (num_tokens(&sbuf[0],'-')>1) {
                                extract_token(&ls->region[0],&sbuf[0],1,'-',16);
                        }
-                       else { /* no ara code? use lang code */
+                       else { /** no ara code? use lang code */
                                blen=strlen(&ls->lang[0]);
                                memcpy(&ls->region[0], ls->lang,blen);
                                ls->region[blen]='\0';
-                       } /* area codes are uppercase */
+                       } /** area codes are uppercase */
                        blen=strlen(&ls->region[0]);
                        for (j=0; j<blen; j++)
                                {
                                        int chars=toupper(ls->region[j]);
-                                       ls->region[j]=(char)chars;/*todo ?! */
+                                       ls->region[j]=(char)chars;/** \todo ?! */
                                }
                        sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
                        
-                       /* check if we have this lang */
+                       /** check if we have this lang */
                        ls->availability=1;
                        ls->selectedlang=-1;
                        for (j=0; j<NUM_LANGS; j++) {
                                int result;
-                               /* match against the LANG part */
+                               /** match against the LANG part */
                                result=strcasecmp(&ls->lang[0], AvailLang[j]);
                                if ((result<0)&&(result<ls->availability)){
                                        ls->availability=result;
                                        ls->selectedlang=j;
                                }
-                               /* match against lang and locale */
+                               /** match against lang and locale */
                                if (0==strcasecmp(&lbuf[0], AvailLang[j])){
                                        ls->availability=0;
                                        ls->selectedlang=j;
@@ -116,26 +130,26 @@ void httplang_to_locale(char *LocaleString)
        prio=0;
        av=-1000;
        nBest=-1;
-       for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
-               {
-                       ls=&wanted_locales[i];
-                       if ((ls->availability<=0)&& 
-                               (av<ls->availability)&&
-                               (prio<ls->priority)&&
-                               (ls->selectedlang!=-1)){
-                               nBest=ls->selectedlang;
-                               av=ls->availability;
-                               prio=ls->priority;
-                       }
+       for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++) {
+               ls=&wanted_locales[i];
+               if ((ls->availability<=0)&& 
+                  (av<ls->availability)&&
+                  (prio<ls->priority)&&
+                  (ls->selectedlang!=-1)) {
+                       nBest=ls->selectedlang;
+                       av=ls->availability;
+                       prio=ls->priority;
                }
-       if (nBest==-1) /* fall back to C */
+       }
+       if (nBest == -1) {
+               /** fall back to C */
                nBest=0;
+       }
        WC->selected_language=nBest;
        lprintf(9, "language found: %s\n", AvailLang[WC->selected_language]);
-       //      set_selected_language(selected_locale);
 }
 
-/* TODO: we skip the language weightening so far. */
+/* TODO: we skip the language weighting so far. */
 /* Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' */
 /* Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 */
 //void httplang_to_locale(char *LocaleString)
@@ -173,10 +187,15 @@ void httplang_to_locale(char *LocaleString)
 //}
 
 
+/**
+ * \brief show the language chooser on the login dialog
+ * depending on the browser locale change the sequence of the 
+ * language chooser.
+ */
 void offer_languages(void) {
        int i;
 
-       wprintf("<select name=\"language\" size=\"1\">\n");
+       wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
 
        for (i=0; i < NUM_LANGS; ++i) {
                wprintf("<option %s value=%s>%s</option>\n",
@@ -189,8 +208,9 @@ void offer_languages(void) {
        wprintf("</select>\n");
 }
 
-/*
- * Set the selected language for this session.
+/**
+ * \brief Set the selected language for this session.
+ * \param lang the locale to set.
  */
 void set_selected_language(char *lang) {
        int i;
@@ -202,23 +222,40 @@ void set_selected_language(char *lang) {
        }
 }
 
-/*
- * Activate and deactivate the selected language for this session.
+/**
+ * \brief Activate the selected language for this session.
  */
 void go_selected_language(void) {
+#ifdef HAVE_USELOCALE
        if (WC->selected_language < 0) return;
-       uselocale(wc_locales[WC->selected_language]);   /* switch locales */
-       textdomain(textdomain(NULL));                   /* clear the cache */
+       uselocale(wc_locales[WC->selected_language]);   /** switch locales */
+       textdomain(textdomain(NULL));                   /** clear the cache */
+#endif
 }
 
+/**
+ * \brief Deactivate the selected language for this session.
+ */
 void stop_selected_language(void) {
-       uselocale(LC_GLOBAL_LOCALE);                    /* switch locales */
-       textdomain(textdomain(NULL));                   /* clear the cache */
+#ifdef HAVE_USELOCALE
+       uselocale(LC_GLOBAL_LOCALE);                    /** switch locales */
+       textdomain(textdomain(NULL));                   /** clear the cache */
+#endif
 }
 
-
-/*
- * Create a locale_t for each available language
+void preset_locale(void)
+{
+#ifndef HAVE_USELOCALE
+#ifdef HAVE_GETTEXT
+       char *language;
+       
+       language = getenv("LANG");
+       setlocale(LC_MESSAGES, language);
+#endif
+#endif
+}
+/**
+ * \brief Create a locale_t for each available language
  */
 void initialize_locales(void) {
        int i;
@@ -229,28 +266,52 @@ void initialize_locales(void) {
        Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
 
        for (i = 0; i < NUM_LANGS; ++i) {
-               sprintf(buf, "%s.UTF8", AvailLang[i]);
-               wc_locales[i] = newlocale(LC_MESSAGES_MASK /* |LC_TIME_MASK FIXME */ ,
+               if (i == 0) {
+                       sprintf(buf, "%s", AvailLang[i]);       // locale 0 (C) is ascii, not utf-8
+               }
+               else {
+                       sprintf(buf, "%s.UTF8", AvailLang[i]);
+               }
+               wc_locales[i] = newlocale(
+                       (LC_MESSAGES_MASK|LC_TIME_MASK),
                        buf,
-                       Empty_Locale
+                       (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
                );
+               if (wc_locales[i] == NULL) {
+                       lprintf(1, "Error configuring locale for %s: %s\n",
+                               buf,
+                               strerror(errno)
+                       );
+               }
+               else {
+                       lprintf(3, "Configured available locale: %s\n", buf);
+               }
        }
 }
 
 
 #else  /* ENABLE_NLS */
-
+/** \brief dummy for non NLS enabled systems */
 void offer_languages(void) {
        wprintf("English (US)");
 }
 
+/** \brief dummy for non NLS enabled systems */
 void set_selected_language(char *lang) {
 }
 
+/** \brief dummy for non NLS enabled systems */
 void go_selected_language(void) {
 }
 
+/** \brief dummy for non NLS enabled systems */
 void stop_selected_language(void) {
 }
 
+void preset_locale(void)
+{
+}
 #endif /* ENABLE_NLS */
+
+
+/*@}*/