+ }
+ 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, '-',
+ sizeof(ls->lang));
+
+ /* get the area code if any. */
+ if (StrBufNum_tokens(SBuf, '-') > 1) {
+ extract_token(&ls->region[0],
+ ChrPtr(SBuf),
+ 1, '-',
+ sizeof(ls->region)
+ );
+ }
+ 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;
+ chars = toupper(ls->region[j]);
+ ls->region[j] = (char)chars; /* todo ? */
+ }
+ snprintf(&lbuf[0],
+ sizeof(lbuf),
+ "%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;
+ }
+ }
+ }
+
+ prio = 0;
+ av = -1000;
+ nBest = -1;
+ 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)
+ ) {
+ nBest = ls->selectedlang;
+ av = ls->availability;
+ prio = ls->priority;
+ }
+ }
+ if (nBest == -1) {
+ /* fall back to C */
+ nBest=0;
+ }
+ sess->selected_language = nBest;
+ syslog(9, "language found: %s", AvailLangLoaded[WC->selected_language]);
+ FreeStrBuf(&Buf);
+ FreeStrBuf(&SBuf);
+}
+
+
+/*
+ * 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, WCTemplputParams *TP)
+{
+ int i;
+#ifndef HAVE_USELOCALE
+ char *Lang = getenv("LANG");
+
+ if (Lang == NULL)
+ Lang = "C";
+#endif
+
+ if (nLocalesLoaded == 1) {
+ wc_printf("<p>%s</p>", AvailLangLoaded[0]);
+ return;
+ }
+
+ wc_printf("<select name=\"language\" id=\"lname\" size=\"1\" onChange=\"switch_to_lang($('lname').value);\">\n");
+
+ 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("</select>\n");
+}
+
+/*
+ * Set the selected language for this session.
+ */
+void set_selected_language(const char *lang) {
+#ifdef HAVE_USELOCALE
+ int i;
+ for (i = 0; i<nLocalesLoaded; ++i) {
+ if (!strcasecmp(lang, AvailLangLoaded[i])) {
+ WC->selected_language = i;
+ break;
+ }
+ }
+#endif
+}
+
+/*
+ * Activate the selected language for this session.
+ */
+void go_selected_language(void) {
+#ifdef HAVE_USELOCALE
+ 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
+}
+
+/*
+ * Deactivate the selected language for this session.
+ */
+void stop_selected_language(void) {
+#ifdef HAVE_USELOCALE
+ uselocale(LC_GLOBAL_LOCALE); /* switch locales */
+ textdomain(textdomain(NULL)); /* clear the cache */
+#endif
+}
+
+#ifdef HAVE_USELOCALE
+ locale_t Empty_Locale;
+#endif
+
+/*
+ * Create a locale_t for each available language
+ */
+void initialize_locales(void) {
+ int nLocales;
+ int i;
+ char buf[32];
+ char *language = NULL;
+
+#ifdef ENABLE_NLS
+ setlocale(LC_ALL, "");
+ syslog(9, "Text domain: %s", textdomain("webcit"));
+ syslog(9, "Message catalog directory: %s", bindtextdomain(textdomain(NULL), LOCALEDIR));
+ syslog(9, "Text domain Charset: %s", bind_textdomain_codeset("webcit","UTF8"));
+#endif
+
+ nLocales = 0;
+ while (!IsEmptyStr(AvailLang[nLocales]))
+ nLocales++;
+
+ language = getenv("WEBCIT_LANG");
+ if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
+ syslog(9, "Nailing locale to %s", 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 < 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 */
+ }
+ 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)
+ );
+ if (wc_locales[nLocalesLoaded] == NULL) {
+ syslog(1, "locale for %s disabled: %s (domain: %s, path: %s)",
+ buf,
+ strerror(errno),
+ textdomain(NULL),
+ bindtextdomain(textdomain(NULL), NULL)
+ );
+ }
+ else {
+ syslog(3, "Found locale: %s", 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)) {
+ syslog(1, "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
+ );
+#else
+ setlocale(LC_MESSAGES, AvailLang[0]);
+ setenv("LANG", AvailLang[0], 1);
+#endif
+ AvailLangLoaded[0] = AvailLang[0];
+ nLocalesLoaded = 1;
+ }
+}
+
+
+void
+ServerShutdownModule_GETTEXT
+(void)
+{
+#ifdef HAVE_USELOCALE
+ int i;
+ for (i = 0; i < nLocalesLoaded; ++i) {
+ if (Empty_Locale != wc_locales[i]) {
+ freelocale(wc_locales[i]);