]> code.citadel.org Git - citadel.git/blobdiff - webcit/gettext.c
* cleanup shutdown procedure; free all structures and their properties
[citadel.git] / webcit / gettext.c
index 9c248454026eeb2f5568e65cd5f01e63190d21ce..ea810deb32e925e44f0666b1c1489e62052110ff 100644 (file)
@@ -1,21 +1,17 @@
 /*
- * $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 9 /**< 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",
@@ -24,9 +20,13 @@ char *AvailLang[NUM_LANGS] = {
        "en_GB",
        "da_DK",
        "fr_FR",
-       "nl_NL"
+       "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
@@ -112,19 +112,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;
                                }
                        }
         }
@@ -148,7 +148,7 @@ void httplang_to_locale(char *LocaleString)
                nBest=0;
        }
        WC->selected_language=nBest;
-       lprintf(9, "language found: %s\n", AvailLang[WC->selected_language]);
+       lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
 }
 
 /* TODO: we skip the language weighting so far. */
@@ -196,14 +196,23 @@ void httplang_to_locale(char *LocaleString)
  */
 void offer_languages(void) {
        int i;
+#ifndef HAVE_USELOCALE
+       char *Lang = getenv("LANG");
+       
+       if (Lang == NULL)
+               Lang = "C";
+#endif
 
        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]
                );
        }
 
@@ -214,14 +223,16 @@ 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
 }
 
 /**
@@ -229,9 +240,15 @@ void set_selected_language(char *lang) {
  */
 void go_selected_language(void) {
 #ifdef HAVE_USELOCALE
-       if (WC->selected_language < 0) return;
-       uselocale(wc_locales[WC->selected_language]);   /** switch locales */
+       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
 }
 
@@ -251,11 +268,17 @@ void preset_locale(void)
 #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
  */
@@ -264,8 +287,6 @@ void initialize_locales(void) {
        char buf[32];
 
 #ifdef HAVE_USELOCALE
-       locale_t Empty_Locale;
-
        /* create default locale */
        Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
 #endif
@@ -278,12 +299,12 @@ void initialize_locales(void) {
                        sprintf(buf, "%s.UTF8", AvailLang[i]);
                }
 #ifdef HAVE_USELOCALE
-               wc_locales[i] = newlocale(
+               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)
@@ -291,12 +312,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) {
@@ -321,4 +355,8 @@ void preset_locale(void)
 #endif /* ENABLE_NLS */
 
 
-/*@}*/
+void TmplGettext(StrBuf *Target, int nTokens, WCTemplateToken *Token)
+{
+       StrBufAppendBufPlain(Target, _(Token->Params[0]->Start), -1, 0);
+
+}