]> code.citadel.org Git - citadel.git/blobdiff - webcit/gettext.c
* just calculate the language for non-static requests; else we will run this useless...
[citadel.git] / webcit / gettext.c
index 209162e8001aaf493cc2d752a54995e67b93bc1a..e38d269ac25bb35c76ce31ec798bdf9c60a2f928 100644 (file)
@@ -4,10 +4,11 @@
 
 #include "webcit.h"
 #include "webserver.h"
+#define SEARCH_LANG 20         /* how many langs should we parse? */
 
 #ifdef ENABLE_NLS
 /* actual supported locales */
-const char *AvailLang[NUM_LANGS] = {
+const char *AvailLang[] = {
        "C",
        "en_US",
        "de_DE",
@@ -17,14 +18,18 @@ const char *AvailLang[NUM_LANGS] = {
        "da_DK",
        "fr_FR",
        "nl_NL",
-       "pt_BR"
+       "pt_BR",
+       "hu_HU",
+       "et_EE",
+       "ru_RU",
+       ""
 };
 
-const char *AvailLangLoaded[NUM_LANGS];
+const char **AvailLangLoaded;
 long nLocalesLoaded = 0;
 
 #ifdef HAVE_USELOCALE
-locale_t wc_locales[NUM_LANGS]; /**< here we keep the parsed stuff */
+locale_t *wc_locales; /**< here we keep the parsed stuff */
 #endif
 
 /** Keep information about one locale */
@@ -47,7 +52,7 @@ typedef struct _lang_pref{
  * \param LocaleString the string from the browser http headers
  */
 
-void httplang_to_locale(StrBuf *LocaleString)
+void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
 {
        LangStruct wanted_locales[SEARCH_LANG];
        LangStruct *ls;
@@ -61,7 +66,7 @@ void httplang_to_locale(StrBuf *LocaleString)
        int nParts;
        StrBuf *Buf = NULL;
        StrBuf *SBuf = NULL;
-       
+
        nParts=StrBufNum_tokens(LocaleString,',');
        for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
         {
@@ -152,7 +157,7 @@ void httplang_to_locale(StrBuf *LocaleString)
                /** fall back to C */
                nBest=0;
        }
-       WC->selected_language=nBest;
+       sess->selected_language=nBest;
        lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
        FreeStrBuf(&Buf);
        FreeStrBuf(&SBuf);
@@ -175,24 +180,24 @@ void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
 
 
        if (nLocalesLoaded == 1) {
-               wprintf("<p>%s</p>", AvailLangLoaded[0]);
+               wc_printf("<p>%s</p>", AvailLangLoaded[0]);
                return;
        }
 
-       wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
+       wc_printf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
 
        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_printf("<option %s value=%s>%s</option>\n",
                        ((WC->selected_language == i) ? "selected" : ""),
                        AvailLangLoaded[i],
                        AvailLangLoaded[i]
                );
        }
 
-       wprintf("</select>\n");
+       wc_printf("</select>\n");
 }
 
 /**
@@ -245,17 +250,28 @@ void stop_selected_language(void) {
  * \brief Create a locale_t for each available language
  */
 void initialize_locales(void) {
+       int nLocales;
        int i;
        char buf[32];
        char *language = NULL;
-       
+       char *locale;
+
+
+       nLocales = 0; 
+       while (!IsEmptyStr(AvailLang[nLocales]))
+               nLocales++;
+
        language = getenv("WEBCIT_LANG");
        if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
                lprintf(9, "Nailing locale to %s\n", language);
        }
        else language = NULL;
 
+       AvailLangLoaded = malloc (sizeof(char*) * nLocales);
+       memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
 #ifdef HAVE_USELOCALE
+       wc_locales = malloc (sizeof(locale_t) * nLocales);
+       memset(wc_locales,0, sizeof(locale_t) * nLocales);
        /* create default locale */
        Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
 #endif
@@ -263,7 +279,7 @@ void initialize_locales(void) {
 
 
 
-       for (i = 0; i < NUM_LANGS; ++i) {
+       for (i = 0; i < nLocales; ++i) {
                if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
                        continue;
                if (i == 0) {
@@ -279,20 +295,21 @@ void initialize_locales(void) {
                        (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
                );
                if (wc_locales[nLocalesLoaded] == NULL) {
-                       lprintf(1, "Error configuring locale for %s: %s\n",
+                       lprintf(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
                                buf,
                                strerror(errno)
                        );
                }
                else {
-                       lprintf(3, "Configured available locale: %s\n", buf);
+                       lprintf(3, "Found locale: %s\n", buf);
                        AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
                        nLocalesLoaded++;
                }
 #else
-               if (language != NULL) {
+               if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
                        setenv("LANG", buf, 1);
                        AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
+                       setlocale(LC_MESSAGES, AvailLang[i]);
                        nLocalesLoaded++;
                }
                else if (nLocalesLoaded == 0) {
@@ -321,10 +338,20 @@ void initialize_locales(void) {
 
 #endif
 
+#ifdef ENABLE_NLS
+       locale = setlocale(LC_ALL, "");
+
+       lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
+       lprintf(9, "Text domain: %s\n", textdomain("webcit"));
+       lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
+
+#endif
 }
 
 
-void ShutdownLocale(void)
+void 
+ServerShutdownModule_GETTEXT
+(void)
 {
 #ifdef HAVE_USELOCALE
        int i;
@@ -332,17 +359,19 @@ void ShutdownLocale(void)
                if (Empty_Locale != wc_locales[i])
                        freelocale(wc_locales[i]);
        }
+       free(wc_locales);
 #endif
+       free(AvailLangLoaded);
 }
 
 #else  /* ENABLE_NLS */
-const char *AvailLang[NUM_LANGS] = {
-       "C"};
+const char *AvailLang[] = {
+       "C", ""};
 
 /** \brief dummy for non NLS enabled systems */
 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
 {
-       wprintf("English (US)");
+       wc_printf("English (US)");
 }
 
 /** \brief dummy for non NLS enabled systems */
@@ -382,11 +411,23 @@ const char *get_selected_language(void) {
 #endif
 }
 
+
+void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+       hdr->HR.browser_language = Line;
+}
+
 void 
 InitModule_GETTEXT
 (void)
 {
-       RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);
+       initialize_locales();
+       
+       RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"), 
+                             Header_HandleAcceptLanguage);
+                             
+       RegisterNamespace("LANG:SELECT", 0, 0, 
+                         tmplput_offer_languages, NULL, CTX_NONE);
 }
 
 
@@ -395,12 +436,9 @@ SessionNewModule_GETTEXT
 (wcsession *sess)
 {
 #ifdef ENABLE_NLS
-       void *vLine;
-       ////TODO: make me a header getter
-       if (GetHash(WC->Hdr->HTTPHeaders, HKEY("ACCEPT-LANGUAGE"), &vLine) && 
-           (vLine != NULL)) {
-               StrBuf *accept_language = (StrBuf*) vLine;
-               httplang_to_locale(accept_language);
+       if (!sess->Hdr->HR.Static && 
+           (sess->Hdr->HR.browser_language != NULL)) {
+               httplang_to_locale(sess->Hdr->HR.browser_language, sess);
        }
 #endif
 }