Date picker is now localized to the currently selected
[citadel.git] / webcit / gettext.c
index b7f0a784b6d95c65a27a76f3e2a9ffe7c34853ed..352b6f7cb5f93a9eeb4d8486793932d8453258c0 100644 (file)
@@ -1,30 +1,35 @@
 /*
- * $Id
+ * $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 6 /**< how many different locales do we know? */
-#define SEARCH_LANG 20 /**< how many langs should we parse? */
+#define NUM_LANGS 10           /* how many different locales do we know? */
+#define SEARCH_LANG 20         /* how many langs should we parse? */
 
-/** actual supported locales */
-char *AvailLang[NUM_LANGS] = {
+/* actual supported locales */
+const char *AvailLang[NUM_LANGS] = {
        "C",
        "en_US",
        "de_DE",
        "it_IT",
        "es_ES",
-       "en_GB"
+       "en_GB",
+       "da_DK",
+       "fr_FR",
+       "nl_NL",
+       "pt_BR"
 };
 
+const char *AvailLangLoaded[NUM_LANGS];
+long nLocalesLoaded = 0;
+
+#ifdef HAVE_USELOCALE
 locale_t wc_locales[NUM_LANGS]; /**< here we keep the parsed stuff */
+#endif
 
 /** Keep information about one locale */
 typedef struct _lang_pref{
@@ -46,37 +51,44 @@ typedef struct _lang_pref{
  * \param LocaleString the string from the browser http headers
  */
 
-void httplang_to_locale(char *LocaleString)
+void httplang_to_locale(StrBuf *LocaleString)
 {
        LangStruct wanted_locales[SEARCH_LANG];
        LangStruct *ls;
 
        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);
+       StrBuf *Buf = NULL;
+       StrBuf *SBuf;
        
-       memcpy(search, LocaleString, len);
-       search[len] = '\0';
-       nParts=num_tokens(search,',');
+       nParts=StrBufNum_tokens(LocaleString,',');
        for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
         {
-                       char buf[16];
                        char sbuf[16];
                        char lbuf[16];
                        int blen;
                        
+                       if (Buf == NULL) {
+                               Buf = NewStrBuf();
+                               SBuf = NewStrBuf();
+                       }
+                       else {
+                               FlushStrBuf(Buf);
+                               FlushStrBuf(SBuf);
+                       }
+
                        ls=&wanted_locales[i];
 
-                       extract_token(&buf[0],search, i,',',16);
+                       StrBufExtract_token(Buf,LocaleString, i,',');
                        /** we are searching, if this list item has something like ;q=n*/
-                       if (num_tokens(&buf[0],'=')>1) {
+                       if (StrBufNum_tokens(Buf,'=')>1) {
                                int sbuflen, k;
-                               extract_token(&sbuf[0],&buf[0], 1,'=',16);
+                               StrBufExtract_token(SBuf,Buf, 1,'=');
                                sbuflen=strlen(&sbuf[0]);
                                for (k=0; k<sbuflen; k++) if (sbuf[k]=='.') sbuf[k]='0';
                                ls->priority=atol(&sbuf[0]);
@@ -85,12 +97,12 @@ void httplang_to_locale(char *LocaleString)
                                ls->priority=1000;
                        }
                        /** get the locale part */
-                       extract_token(&sbuf[0],&buf[0],0,';',16);
+                       StrBufExtract_token(SBuf ,Buf, 0, ';');
                        /** get the lang part, which should be allways there */
-                       extract_token(&ls->lang[0],&sbuf[0],0,'-',16);
+                       extract_token(&ls->lang[0], ChrPtr(SBuf), 0, '-', 16);
                        /** get the area code if any. */
-                       if (num_tokens(&sbuf[0],'-')>1) {
-                               extract_token(&ls->region[0],&sbuf[0],1,'-',16);
+                       if (StrBufNum_tokens(SBuf,'-') > 1) {
+                               extract_token(&ls->region[0],ChrPtr(SBuf),1,'-',16);
                        }
                        else { /** no ara code? use lang code */
                                blen=strlen(&ls->lang[0]);
@@ -108,19 +120,19 @@ void httplang_to_locale(char *LocaleString)
                        /** check if we have this lang */
                        ls->availability=1;
                        ls->selectedlang=-1;
-                       for (j=0; j<NUM_LANGS; j++) {
+                       for (j=0; j<nLocalesLoaded; j++) {
                                int result;
                                /** match against the LANG part */
-                               result=strcasecmp(&ls->lang[0], AvailLang[j]);
+                               result=strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
                                if ((result<0)&&(result<ls->availability)){
                                        ls->availability=result;
                                        ls->selectedlang=j;
                                }
                                /** match against lang and locale */
-                               if (0==strcasecmp(&lbuf[0], AvailLang[j])){
+                               if (0==strcasecmp(&lbuf[0], AvailLangLoaded[j])){
                                        ls->availability=0;
                                        ls->selectedlang=j;
-                                       j=NUM_LANGS;
+                                       j=nLocalesLoaded;
                                }
                        }
         }
@@ -128,28 +140,28 @@ 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]);
-       if (search != NULL) {
-               free(search);
        }
+       WC->selected_language=nBest;
+       lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
+       FreeStrBuf(&Buf);
+       FreeStrBuf(&SBuf);
 }
 
-/* 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)
@@ -192,16 +204,25 @@ void httplang_to_locale(char *LocaleString)
  * depending on the browser locale change the sequence of the 
  * language chooser.
  */
-void offer_languages(void) {
+void offer_languages(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType) {
        int i;
+#ifndef HAVE_USELOCALE
+       char *Lang = getenv("LANG");
+       
+       if (Lang == NULL)
+               Lang = "C";
+#endif
 
-       wprintf("<select name=\"language\" size=\"1\">\n");
+       wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
 
-       for (i=0; i < NUM_LANGS; ++i) {
+       for (i=0; i < nLocalesLoaded; ++i) {
+#ifndef HAVE_USELOCALE
+               if (strcmp(AvailLangLoaded[i], Lang) == 0)
+#endif
                wprintf("<option %s value=%s>%s</option>\n",
                        ((WC->selected_language == i) ? "selected" : ""),
-                       AvailLang[i],
-                       AvailLang[i]
+                       AvailLangLoaded[i],
+                       AvailLangLoaded[i]
                );
        }
 
@@ -212,44 +233,73 @@ void offer_languages(void) {
  * \brief Set the selected language for this session.
  * \param lang the locale to set.
  */
-void set_selected_language(char *lang) {
+void set_selected_language(const char *lang) {
        int i;
 
-       for (i=0; i<NUM_LANGS; ++i) {
-               if (!strcasecmp(lang, AvailLang[i])) {
+#ifdef HAVE_USELOCALE
+       for (i=0; i<nLocalesLoaded; ++i) {
+               if (!strcasecmp(lang, AvailLangLoaded[i])) {
                        WC->selected_language = i;
                }
        }
+#endif
 }
 
 /**
  * \brief Activate the selected language for this session.
  */
 void go_selected_language(void) {
-       if (WC->selected_language < 0) return;
-       uselocale(wc_locales[WC->selected_language]);   /** switch locales */
+#ifdef HAVE_USELOCALE
+       struct wcsession *WCC = WC;
+       if (WCC->selected_language < 0) return;
+       uselocale(wc_locales[WCC->selected_language]);  /** switch locales */
        textdomain(textdomain(NULL));                   /** clear the cache */
+#else
+       char *language;
+       
+       language = getenv("LANG");
+       setlocale(LC_MESSAGES, language);
+#endif
 }
 
 /**
  * \brief Deactivate the selected language for this session.
  */
 void stop_selected_language(void) {
+#ifdef HAVE_USELOCALE
        uselocale(LC_GLOBAL_LOCALE);                    /** switch locales */
        textdomain(textdomain(NULL));                   /** clear the cache */
+#endif
+}
+
+void preset_locale(void)
+{
+#ifndef HAVE_USELOCALE
+#ifdef HAVE_GETTEXT
+       char *language;
+       
+       lprintf(9, "Nailing locale to %s\n", getenv("LANG"));
+       language = getenv("LANG");
+       setlocale(LC_MESSAGES, language);
+#endif
+#endif
 }
 
+#ifdef HAVE_USELOCALE
+       locale_t Empty_Locale;
+#endif
 
 /**
  * \brief Create a locale_t for each available language
  */
 void initialize_locales(void) {
        int i;
-       locale_t Empty_Locale;
        char buf[32];
 
+#ifdef HAVE_USELOCALE
        /* create default locale */
        Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
+#endif
 
        for (i = 0; i < NUM_LANGS; ++i) {
                if (i == 0) {
@@ -258,12 +308,13 @@ void initialize_locales(void) {
                else {
                        sprintf(buf, "%s.UTF8", AvailLang[i]);
                }
-               wc_locales[i] = newlocale(
+#ifdef HAVE_USELOCALE
+               wc_locales[nLocalesLoaded] = newlocale(
                        (LC_MESSAGES_MASK|LC_TIME_MASK),
                        buf,
                        (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
                );
-               if (wc_locales[i] == NULL) {
+               if (wc_locales[nLocalesLoaded] == NULL) {
                        lprintf(1, "Error configuring locale for %s: %s\n",
                                buf,
                                strerror(errno)
@@ -271,11 +322,25 @@ void initialize_locales(void) {
                }
                else {
                        lprintf(3, "Configured available locale: %s\n", buf);
+                       AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
+                       nLocalesLoaded++;
                }
+#endif
        }
 }
 
 
+void ShutdownLocale(void)
+{
+       int i;
+#ifdef HAVE_USELOCALE
+       for (i = 0; i < nLocalesLoaded; ++i) {
+               if (Empty_Locale != wc_locales[i])
+                       freelocale(wc_locales[i]);
+       }
+#endif
+}
+
 #else  /* ENABLE_NLS */
 /** \brief dummy for non NLS enabled systems */
 void offer_languages(void) {
@@ -294,7 +359,33 @@ void go_selected_language(void) {
 void stop_selected_language(void) {
 }
 
+void preset_locale(void)
+{
+}
 #endif /* ENABLE_NLS */
 
 
-/*@}*/
+void TmplGettext(StrBuf *Target, int nTokens, WCTemplateToken *Token)
+{
+       StrBufAppendBufPlain(Target, _(Token->Params[0]->Start), -1, 0);
+
+}
+
+
+/*
+ * Returns the language currently in use.
+ * This function returns a static string, so don't do anything stupid please.
+ */
+const char *get_selected_language(void) {
+#ifdef ENABLE_NLS
+#ifdef HAVE_USELOCALE
+       return AvailLang[WC->selected_language];
+#else
+       return "en"
+#endif
+#else
+       return "en"
+#endif
+}
+
+