+ link gettext.c into setup, so we know the servers locales
[citadel.git] / webcit / gettext.c
index de380d6c84e1f951f870a942ea92b62d9c8c1c1c..e486fc4d94db9bed56fe12554abf75b86eb29f1a 100644 (file)
@@ -6,10 +6,6 @@
 #include "webserver.h"
 
 #ifdef ENABLE_NLS
-
-#define NUM_LANGS 10           /* how many different locales do we know? */
-#define SEARCH_LANG 20         /* how many langs should we parse? */
-
 /* actual supported locales */
 const char *AvailLang[NUM_LANGS] = {
        "C",
@@ -64,77 +60,78 @@ void httplang_to_locale(StrBuf *LocaleString)
        int nBest;
        int nParts;
        StrBuf *Buf = NULL;
-       StrBuf *SBuf;
+       StrBuf *SBuf = NULL;
        
        nParts=StrBufNum_tokens(LocaleString,',');
        for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
         {
-                       char sbuf[16];
-                       char lbuf[16];
-                       int blen;
+               char lbuf[16];
+               int blen;
                        
-                       if (Buf == NULL) {
-                               Buf = NewStrBuf();
-                               SBuf = NewStrBuf();
-                       }
-                       else {
-                               FlushStrBuf(Buf);
-                               FlushStrBuf(SBuf);
-                       }
+               if (Buf == NULL) {
+                       Buf = NewStrBuf();
+                       SBuf = NewStrBuf();
+               }
+               else {
+                       FlushStrBuf(Buf);
+                       FlushStrBuf(SBuf);
+               }
 
-                       ls=&wanted_locales[i];
+               ls=&wanted_locales[i];
 
-                       StrBufExtract_token(Buf,LocaleString, i,',');
-                       /** we are searching, if this list item has something like ;q=n*/
-                       if (StrBufNum_tokens(Buf,'=')>1) {
-                               int sbuflen, k;
-                               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]);
-                       }
-                       else {
-                               ls->priority=1000;
-                       }
-                       /** get the locale part */
-                       StrBufExtract_token(SBuf ,Buf, 0, ';');
-                       /** get the lang part, which should be allways there */
-                       extract_token(&ls->lang[0], ChrPtr(SBuf), 0, '-', 16);
-                       /** get the area code if any. */
-                       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]);
-                               memcpy(&ls->region[0], ls->lang,blen);
-                               ls->region[blen]='\0';
-                       } /** 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 ?! */
-                               }
-                       sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
+               StrBufExtract_token(Buf,LocaleString, i,',');
+               /** we are searching, if this list item has something like ;q=n*/
+               if (StrBufNum_tokens(Buf,'=')>1) {
+                       int sbuflen, k;
+                       StrBufExtract_token(SBuf,Buf, 1,'=');
+                       sbuflen=StrLength(SBuf);
+                       for (k=0; k<sbuflen; k++) 
+                               if (ChrPtr(SBuf)[k]=='.') 
+                                       StrBufPeek(SBuf, NULL, k, '0');
+                       ls->priority=StrTol(SBuf);
+               }
+               else {
+                       ls->priority=1000;
+               }
+               /** get the locale part */
+               StrBufExtract_token(SBuf ,Buf, 0, ';');
+               /** get the lang part, which should be allways there */
+               extract_token(&ls->lang[0], ChrPtr(SBuf), 0, '-', 16);
+               /** get the area code if any. */
+               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]);
+                       memcpy(&ls->region[0], ls->lang,blen);
+                       ls->region[blen]='\0';
+               } /** 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 ?! */
+               }
+               sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
                        
-                       /** check if we have this lang */
-                       ls->availability=1;
-                       ls->selectedlang=-1;
-                       for (j=0; j<nLocalesLoaded; j++) {
-                               int result;
-                               /** match against the LANG part */
-                               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], AvailLangLoaded[j])){
-                                       ls->availability=0;
-                                       ls->selectedlang=j;
-                                       j=nLocalesLoaded;
-                               }
+               /** check if we have this lang */
+               ls->availability=1;
+               ls->selectedlang=-1;
+               for (j=0; j<nLocalesLoaded; j++) {
+                       int result;
+                       /** match against the LANG part */
+                       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], AvailLangLoaded[j])){
+                               ls->availability=0;
+                               ls->selectedlang=j;
+                               j=nLocalesLoaded;
                        }
+               }
         }
        
        prio=0;
@@ -161,50 +158,13 @@ void httplang_to_locale(StrBuf *LocaleString)
        FreeStrBuf(&SBuf);
 }
 
-/* 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)
-//{
-//     char selected_locale[16];
-//     int i, j;
-//     char lang[64];
-//     int num_accept = 0;
-//
-//     lprintf(9, "languageAccept: %s\n", LocaleString);
-//
-//     strcpy(selected_locale, "C");
-//     num_accept = num_tokens(LocaleString, ',');
-//
-//     for (i=num_accept-1; i>=0; --i) {
-//             extract_token(lang, LocaleString, i, ',', sizeof lang);
-//
-//             /* Strip out the weights; we don't use them.  Also convert
-//              * hyphens to underscores.
-//              */
-//             for (j=0; j<strlen(lang); ++j) {
-//                     if (lang[j] == '-') lang[j] = '_';
-//                     if (lang[j] == ';') lang[j] = 0;
-//             }
-//
-//             for (j=0; j<NUM_LANGS; ++j) {
-//                     if (!strncasecmp(lang, AvailLang[j], strlen(lang))) {
-//                             strcpy(selected_locale, AvailLang[j]);
-//                     }
-//             }
-//     }
-//
-//     lprintf(9, "language found: %s\n", selected_locale);
-//     set_selected_language(selected_locale);
-//}
-
-
 /**
  * \brief show the language chooser on the login dialog
  * depending on the browser locale change the sequence of the 
  * language chooser.
  */
-void offer_languages(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType) {
+void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
+{
        int i;
 #ifndef HAVE_USELOCALE
        char *Lang = getenv("LANG");
@@ -250,7 +210,7 @@ void set_selected_language(const char *lang) {
  */
 void go_selected_language(void) {
 #ifdef HAVE_USELOCALE
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        if (WCC->selected_language < 0) return;
        uselocale(wc_locales[WCC->selected_language]);  /** switch locales */
        textdomain(textdomain(NULL));                   /** clear the cache */
@@ -303,7 +263,7 @@ void initialize_locales(void) {
 
        for (i = 0; i < NUM_LANGS; ++i) {
                if (i == 0) {
-                       sprintf(buf, "%s", AvailLang[i]);       // locale 0 (C) is ascii, not utf-8
+                       sprintf(buf, "%s", AvailLang[i]);       /* locale 0 (C) is ascii, not utf-8 */
                }
                else {
                        sprintf(buf, "%s.UTF8", AvailLang[i]);
@@ -342,8 +302,12 @@ void ShutdownLocale(void)
 }
 
 #else  /* ENABLE_NLS */
+const char *AvailLang[NUM_LANGS] = {
+       "C"};
+
 /** \brief dummy for non NLS enabled systems */
-void offer_languages(void) {
+void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
+{
        wprintf("English (US)");
 }
 
@@ -365,8 +329,31 @@ void preset_locale(void)
 #endif /* ENABLE_NLS */
 
 
-void TmplGettext(StrBuf *Target, int nTokens, WCTemplateToken *Token)
+void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
 {
-       StrBufAppendBufPlain(Target, _(Token->Params[0]->Start), -1, 0);
+       StrBufAppendBufPlain(Target, _(TP->Tokens->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
+}
 
+void 
+InitModule_GETTEXT
+(void)
+{
+       RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);
 }