* fix *bsd single locale mode
[citadel.git] / webcit / gettext.c
index fc2cc6000f33a1ea0dffd2f2e342ef5581bc7c4c..de7edb80143bebccf49a166280238766fe5e87c0 100644 (file)
@@ -4,14 +4,11 @@
 
 #include "webcit.h"
 #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? */
 
+#ifdef ENABLE_NLS
 /* actual supported locales */
-const char *AvailLang[NUM_LANGS] = {
+const char *AvailLang[] = {
        "C",
        "en_US",
        "de_DE",
@@ -21,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 */
@@ -51,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;
@@ -65,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++)
         {
@@ -156,56 +157,19 @@ 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);
 }
 
-/* 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 tmplput_offer_languages(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType) {
+void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
+{
        int i;
 #ifndef HAVE_USELOCALE
        char *Lang = getenv("LANG");
@@ -214,6 +178,12 @@ void tmplput_offer_languages(StrBuf *Target, int nArgs, WCTemplateToken *Tokens,
                Lang = "C";
 #endif
 
+
+       if (nLocalesLoaded == 1) {
+               wprintf("<p>%s</p>", AvailLangLoaded[0]);
+               return;
+       }
+
        wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
 
        for (i=0; i < nLocalesLoaded; ++i) {
@@ -235,9 +205,8 @@ void tmplput_offer_languages(StrBuf *Target, int nArgs, WCTemplateToken *Tokens,
  * \param lang the locale to set.
  */
 void set_selected_language(const char *lang) {
-       int i;
-
 #ifdef HAVE_USELOCALE
+       int i;
        for (i=0; i<nLocalesLoaded; ++i) {
                if (!strcasecmp(lang, AvailLangLoaded[i])) {
                        WC->selected_language = i;
@@ -273,19 +242,6 @@ void stop_selected_language(void) {
 #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
@@ -294,17 +250,40 @@ void preset_locale(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
 
-       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) {
-                       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]);
@@ -316,35 +295,82 @@ 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) && (strcmp(language, AvailLang[i]) == 0)) {
+                       setenv("LANG", buf, 1);
                        AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
+                       setlocale(LC_MESSAGES, AvailLang[i]);
                        nLocalesLoaded++;
                }
+               else if (nLocalesLoaded == 0) {
+                       setenv("LANG", buf, 1);
+                       AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
+                       nLocalesLoaded++;
+               }
+#endif
+       }
+       if ((language != NULL) && (nLocalesLoaded == 0)) {
+               lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
+#ifdef HAVE_USELOCALE
+               wc_locales[0] = newlocale(
+                       (LC_MESSAGES_MASK|LC_TIME_MASK),
+                       AvailLang[0],
+                       Empty_Locale);          
+#else
+               setlocale(LC_MESSAGES, AvailLang[0]);
+               setenv("LANG", AvailLang[0], 1);
 #endif
+               AvailLangLoaded[0] = AvailLang[0];
+               nLocalesLoaded = 1;
        }
+#ifndef HAVE_USELOCALE
+
+
+#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)
 {
-       int i;
 #ifdef HAVE_USELOCALE
+       int i;
        for (i = 0; i < nLocalesLoaded; ++i) {
                if (Empty_Locale != wc_locales[i])
                        freelocale(wc_locales[i]);
        }
+       free(wc_locales);
+       free(AvailLangLoaded);
 #endif
 }
 
 #else  /* ENABLE_NLS */
+const char *AvailLang[] = {
+       "C", ""};
+
 /** \brief dummy for non NLS enabled systems */
-void tmplput_offer_languages(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType) {
+void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
+{
        wprintf("English (US)");
 }
 
@@ -360,15 +386,12 @@ void go_selected_language(void) {
 void stop_selected_language(void) {
 }
 
-void preset_locale(void)
-{
-}
 #endif /* ENABLE_NLS */
 
 
-void TmplGettext(StrBuf *Target, int nTokens, WCTemplateToken *Tokens)
+void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
 {
-       StrBufAppendBufPlain(Target, _(Tokens->Params[0]->Start), -1, 0);
+       StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
 }
 
 
@@ -381,10 +404,10 @@ const char *get_selected_language(void) {
 #ifdef HAVE_USELOCALE
        return AvailLang[WC->selected_language];
 #else
-       return "en"
+       return "en";
 #endif
 #else
-       return "en"
+       return "en";
 #endif
 }
 
@@ -392,5 +415,43 @@ void
 InitModule_GETTEXT
 (void)
 {
+       initialize_locales();
        RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);
 }
+
+
+void
+SessionNewModule_GETTEXT
+(wcsession *sess)
+{
+#ifdef ENABLE_NLS
+       OneHttpHeader *vLine = NULL;
+
+       if (    (sess->Hdr->HTTPHeaders != NULL)
+               && GetHash(sess->Hdr->HTTPHeaders, HKEY("ACCEPT-LANGUAGE"), (void *)&vLine)
+               && (vLine != NULL)
+               && (vLine->Val != NULL)
+       ) {
+               StrBuf *accept_language = vLine->Val;
+               httplang_to_locale(accept_language, sess);
+       }
+#endif
+}
+
+void
+SessionAttachModule_GETTEXT
+(wcsession *sess)
+{
+#ifdef ENABLE_NLS
+       go_selected_language();                                 /* set locale */
+#endif
+}
+
+void 
+SessionDestroyModule_GETTEXT
+(wcsession *sess)
+{
+#ifdef ENABLE_NLS
+       stop_selected_language();                               /* unset locale */
+#endif
+}