Reverts commit c6aec42f213ec284e34648f3d69bcf927dccddb1 because putting the opening...
[citadel.git] / webcit / gettext.c
index c037f7a654357382bb889e6096e42d3b83d0f85e..1bc40c8f40a5dbe18f08331d85dd8a8ccf46578f 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 1996-2012 by the citadel.org team
  *
@@ -16,7 +15,6 @@
 #define SEARCH_LANG 20         /* how many langs should we parse? */
 
 #ifdef ENABLE_NLS
-
 /* actual supported locales */
 const char *AvailLang[] = {
        "en_US",
@@ -40,7 +38,7 @@ const char *AvailLang[] = {
        "pt_BR",
        "ru_RU",
        "zh_CN",
-       "he_IL",
+       "he_IL",
        "kk_KK",
        "ro_RO",
        "sl_SL",
@@ -52,16 +50,16 @@ const char **AvailLangLoaded;
 long nLocalesLoaded = 0;
 
 #ifdef HAVE_USELOCALE
-locale_t *wc_locales;          /* here we keep the parsed stuff */
+locale_t *wc_locales; /* here we keep the parsed stuff */
 #endif
 
 /* Keep information about one locale */
 typedef struct _lang_pref {
-       char lang[16];          /* the language locale string */
-       char region[16];        /* the region locale string */
-       long priority;          /* which priority does it have */
-       int availability;       /* do we know it? */
-       int selectedlang;       /* is this the selected language? */
+       char lang[16];          /* the language locale string */
+       char region[16];        /* the region locale string */
+       long priority;          /* which priority does it have */
+       int availability;       /* do we know it? */
+       int selectedlang;       /* is this the selected language? */
 } LangStruct;
 
 /* parse browser locale header 
@@ -75,7 +73,8 @@ typedef struct _lang_pref {
  * Accept-Language: de,en-us;q=0.9,it;q=0.9,de-de;q=0.8,en-ph;q=0.7,de-at;q=0.7,zh-cn;q=0.6,cy;q=0.5,ar-om;q=0.5,en-tt;q=0.4,xh;q=0.3,nl-be;q=0.3,cs;q=0.2,sv;q=0.1,tk;q=0.1 
  */
 
-void httplang_to_locale(StrBuf * LocaleString, wcsession * sess) {
+void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
+{
        LangStruct wanted_locales[SEARCH_LANG];
        LangStruct *ls;
 
@@ -91,10 +90,11 @@ void httplang_to_locale(StrBuf * LocaleString, wcsession * sess) {
        StrBuf *SBuf = NULL;
 
        nParts = StrBufNum_tokens(LocaleString, ',');
-       for (i = 0; ((i < nParts) && (i < SEARCH_LANG)); i++) {
+       for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
+        {
                char lbuf[32];
                int blen;
-
+                       
                if (Buf == NULL) {
                        Buf = NewStrBuf();
                        SBuf = NewStrBuf();
@@ -107,7 +107,7 @@ void httplang_to_locale(StrBuf * LocaleString, wcsession * sess) {
                ls = &wanted_locales[i];
 
                StrBufExtract_token(Buf, LocaleString, i, ',');
-               /* we are searching, if this list item has something like ;q=n */
+               /* 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, '=');
@@ -127,14 +127,20 @@ void httplang_to_locale(StrBuf * LocaleString, wcsession * sess) {
                StrBufExtract_token(SBuf, Buf, 0, ';');
 
                /* get the lang part, which should be allways there */
-               extract_token(ls->lang, ChrPtr(SBuf), 0, '-', sizeof(ls->lang));
+               extract_token(ls->lang, 
+                             ChrPtr(SBuf), 
+                             0, '-', 
+                             sizeof(ls->lang));
 
                /* get the area code if any. */
                if (StrBufNum_tokens(SBuf, '-') > 1) {
-                       extract_token(ls->region, ChrPtr(SBuf), 1, '-', sizeof(ls->region)
-                           );
+                       extract_token(ls->region, 
+                                     ChrPtr(SBuf), 
+                                     1, '-', 
+                                     sizeof(ls->region)
+                       );
                }
-               else {          /* no ara code? use lang code */
+               else { /* no ara code? use lang code */
                        blen = strlen(ls->lang);
                        memcpy(ls->region, ls->lang, blen);
                        ls->region[blen] = '\0';
@@ -142,13 +148,18 @@ void httplang_to_locale(StrBuf * LocaleString, wcsession * sess) {
 
                /* area codes are uppercase */
                blen = strlen(&ls->region[0]);
-               for (j = 0; j < blen; j++) {
+               for (j = 0; j < blen; j++)
+               {
                        int chars;
                        chars = toupper(ls->region[j]);
-                       ls->region[j] = (char) chars;   /* todo ? */
+                       ls->region[j] = (char)chars; /* todo ? */
                }
-               snprintf(lbuf, sizeof(lbuf), "%s_%s", ls->lang, ls->region);
-
+               snprintf(lbuf,
+                        sizeof(lbuf), 
+                        "%s_%s", 
+                        ls->lang, 
+                        ls->region);
+                       
                /* check if we have this lang */
                ls->availability = 1;
                ls->selectedlang = -1;
@@ -157,29 +168,29 @@ void httplang_to_locale(StrBuf * LocaleString, wcsession * sess) {
                        int result;
                        /* match against the LANG part */
                        result = strncasecmp(ls->lang, AvailLangLoaded[j], len);
-                       if ((result == 0) && (result < ls->availability)) {
+                       if ((result == 0) && (result < ls->availability)){
                                ls->availability = result;
                                ls->selectedlang = j;
                        }
                        /* match against lang and locale */
-                       if (0 == strcasecmp(lbuf, AvailLangLoaded[j])) {
+                       if (0 == strcasecmp(lbuf, AvailLangLoaded[j])){
                                ls->availability = 0;
                                ls->selectedlang = j;
                                j = nLocalesLoaded;
                        }
                }
-       }
-
+        }
+       
        prio = 0;
        av = -1000;
        nBest = -1;
-       for (i = 0; ((i < nParts) && (i < SEARCH_LANG)); i++) {
+       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)
-                   ) {
+               if (    (ls->availability <= 0)
+                       && (av < ls->availability)
+                       && (prio < ls->priority)
+                       && (ls->selectedlang != -1)
+               ) {
                        nBest = ls->selectedlang;
                        av = ls->availability;
                        prio = ls->priority;
@@ -187,7 +198,7 @@ void httplang_to_locale(StrBuf * LocaleString, wcsession * sess) {
        }
        if (nBest == -1) {
                /* fall back to C */
-               nBest = 0;
+               nBest=0;
        }
        sess->selected_language = nBest;
        syslog(LOG_DEBUG, "language found: %s", AvailLangLoaded[sess->selected_language]);
@@ -201,11 +212,12 @@ void httplang_to_locale(StrBuf * LocaleString, wcsession * sess) {
  * depending on the browser locale change the sequence of the 
  * language chooser.
  */
-void tmplput_offer_languages(StrBuf * Target, WCTemplputParams * TP) {
+void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
+{
        int i;
 #ifndef HAVE_USELOCALE
        char *Lang = getenv("LANG");
-
+       
        if (Lang == NULL)
                Lang = "C";
 #endif
@@ -217,13 +229,15 @@ void tmplput_offer_languages(StrBuf * Target, WCTemplputParams * TP) {
 
        wc_printf("<select name=\"language\" id=\"lname\" size=\"1\" onChange=\"switch_to_lang($('lname').value);\">\n");
 
-       for (i = 0; i < nLocalesLoaded; ++i) {
+       for (i=0; i < nLocalesLoaded; ++i) {
 #ifndef HAVE_USELOCALE
                if (strcmp(AvailLangLoaded[i], Lang) == 0)
 #endif
-                       wc_printf("<option %s value=%s>%s</option>\n",
-                                 ((WC->selected_language == i) ? "selected" : ""), AvailLangLoaded[i], AvailLangLoaded[i]
-                           );
+               wc_printf("<option %s value=%s>%s</option>\n",
+                       ((WC->selected_language == i) ? "selected" : ""),
+                       AvailLangLoaded[i],
+                       AvailLangLoaded[i]
+               );
        }
 
        wc_printf("</select>\n");
@@ -235,7 +249,7 @@ void tmplput_offer_languages(StrBuf * Target, WCTemplputParams * TP) {
 void set_selected_language(const char *lang) {
 #ifdef HAVE_USELOCALE
        int i;
-       for (i = 0; i < nLocalesLoaded; ++i) {
+       for (i = 0; i<nLocalesLoaded; ++i) {
                if (!strcasecmp(lang, AvailLangLoaded[i])) {
                        WC->selected_language = i;
                        break;
@@ -250,16 +264,17 @@ void set_selected_language(const char *lang) {
 void go_selected_language(void) {
 #ifdef HAVE_USELOCALE
        wcsession *WCC = WC;
-       if (WCC->selected_language < 0) {
+       if (WCC->selected_language < 0)
+       {
                httplang_to_locale(WCC->Hdr->HR.browser_language, WCC);
                if (WCC->selected_language < 0)
                        return;
        }
        uselocale(wc_locales[WCC->selected_language]);  /* switch locales */
-       textdomain(textdomain(NULL));   /* clear the cache */
+       textdomain(textdomain(NULL));                   /* clear the cache */
 #else
        char *language;
-
+       
        language = getenv("LANG");
        setlocale(LC_MESSAGES, language);
 #endif
@@ -270,13 +285,13 @@ void go_selected_language(void) {
  */
 void stop_selected_language(void) {
 #ifdef HAVE_USELOCALE
-       uselocale(LC_GLOBAL_LOCALE);    /* switch locales */
-       textdomain(textdomain(NULL));   /* clear the cache */
+       uselocale(LC_GLOBAL_LOCALE);                    /* switch locales */
+       textdomain(textdomain(NULL));                   /* clear the cache */
 #endif
 }
 
 #ifdef HAVE_USELOCALE
-locale_t Empty_Locale;
+       locale_t Empty_Locale;
 #endif
 
 /*
@@ -288,22 +303,21 @@ void initialize_locales(void) {
        char buf[32];
        char *language = NULL;
 
-       nLocales = 0;
+       nLocales = 0; 
        while (!IsEmptyStr(AvailLang[nLocales]))
                nLocales++;
 
        language = getenv("WEBCIT_LANG");
        if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
                syslog(LOG_INFO, "Nailing locale to %s", language);
-       }
-       else
-               language = NULL;
+       }
+       else language = NULL;
 
-       AvailLangLoaded = malloc(sizeof(char *) * nLocales);
-       memset(AvailLangLoaded, 0, sizeof(char *) * nLocales);
+       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);
+       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
@@ -313,15 +327,17 @@ void initialize_locales(void) {
                        continue;
                if (i == 0) {
                        sprintf(buf, "C");      /* locale 0 (C) is ascii, not utf-8 */
-
+                       
                }
                else {
                        sprintf(buf, "%s.UTF8", AvailLang[i]);
                }
 #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)
-                   );
+               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[nLocalesLoaded] == NULL) {
                        syslog(LOG_NOTICE, "locale for %s disabled: %s", buf, strerror(errno));
                }
@@ -347,7 +363,11 @@ void initialize_locales(void) {
        if ((language != NULL) && (nLocalesLoaded == 0)) {
                syslog(LOG_WARNING, "Your selected locale [%s] isn't available on your system. falling back to C", language);
 #ifdef HAVE_USELOCALE
-               wc_locales[0] = newlocale((LC_MESSAGES_MASK | LC_TIME_MASK), AvailLang[0], Empty_Locale);
+               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);
@@ -360,12 +380,15 @@ void initialize_locales(void) {
        setlocale(LC_ALL, "");
        syslog(LOG_DEBUG, "Text domain: %s", textdomain("webcit"));
        syslog(LOG_DEBUG, "Text domain Charset: %s", bind_textdomain_codeset("webcit", "UTF8"));
-       syslog(LOG_DEBUG, "Message catalog directory: %s", bindtextdomain(textdomain(NULL), LOCALEDIR "/locale"));
+       syslog(LOG_DEBUG, "Message catalog directory: %s", bindtextdomain(textdomain(NULL), LOCALEDIR"/locale"));
 #endif
 }
 
 
-void ServerShutdownModule_GETTEXT(void) {
+void 
+ServerShutdownModule_GETTEXT
+(void)
+{
 #ifdef HAVE_USELOCALE
        int i;
        for (i = 0; i < nLocalesLoaded; ++i) {
@@ -378,19 +401,22 @@ void ServerShutdownModule_GETTEXT(void) {
        free(AvailLangLoaded);
 }
 
-#else                          /* ENABLE_NLS */
+#else  /* ENABLE_NLS */
 const char *AvailLang[] = {
        "C",
        ""
 };
 
 /* dummy for non NLS enabled systems */
-void
- ServerShutdownModule_GETTEXT(void) {
+void 
+ServerShutdownModule_GETTEXT
+(void)
+{
 }
 
 
-void tmplput_offer_languages(StrBuf * Target, WCTemplputParams * TP) {
+void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
+{
        wc_printf("English (US)");
 }
 
@@ -409,9 +435,10 @@ void stop_selected_language(void) {
 void initialize_locales(void) {
 }
 
-#endif                         /* ENABLE_NLS */
+#endif /* ENABLE_NLS */
 
-void TmplGettext(StrBuf * Target, WCTemplputParams * TP) {
+void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
+{
        const char *Text = _(TP->Tokens->Params[0]->Start);
 
        StrBufAppendTemplateStr(Target, TP, Text, 1);
@@ -435,39 +462,54 @@ const char *get_selected_language(void) {
 }
 
 
-void Header_HandleAcceptLanguage(StrBuf * Line, ParsedHttpHdrs * hdr) {
+void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
        hdr->HR.browser_language = Line;
 }
 
 
-void InitModule_GETTEXT(void) {
+void 
+InitModule_GETTEXT
+(void)
+{
        initialize_locales();
-
-       RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"), Header_HandleAcceptLanguage);
-
-       RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, NULL, CTX_NONE);
+       
+       RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"), 
+                             Header_HandleAcceptLanguage);
+                             
+       RegisterNamespace("LANG:SELECT", 0, 0, 
+                         tmplput_offer_languages, NULL, CTX_NONE);
 }
 
 
-void SessionNewModule_GETTEXT(wcsession * sess) {
+void
+SessionNewModule_GETTEXT
+(wcsession *sess)
+{
 #ifdef ENABLE_NLS
-       if ((sess != NULL)
-           && (!sess->Hdr->HR.Static)
-           && (sess->Hdr->HR.browser_language != NULL)
-           ) {
+       if (    (sess != NULL)
+               && (!sess->Hdr->HR.Static)
+               && (sess->Hdr->HR.browser_language != NULL)
+       ) {
                httplang_to_locale(sess->Hdr->HR.browser_language, sess);
        }
 #endif
 }
 
-void SessionAttachModule_GETTEXT(wcsession * sess) {
+void
+SessionAttachModule_GETTEXT
+(wcsession *sess)
+{
 #ifdef ENABLE_NLS
-       go_selected_language(); /* set locale */
+       go_selected_language();                                 /* set locale */
 #endif
 }
 
-void SessionDestroyModule_GETTEXT(wcsession * sess) {
+void 
+SessionDestroyModule_GETTEXT
+(wcsession *sess)
+{
 #ifdef ENABLE_NLS
-       stop_selected_language();       /* unset locale */
+       stop_selected_language();                               /* unset locale */
 #endif
 }