X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit%2Fgettext.c;h=1bc40c8f40a5dbe18f08331d85dd8a8ccf46578f;hb=HEAD;hp=7713ce70dc1ce98a34c4936cd95e36073a7caf9a;hpb=2da9849cc6ca7c2ea6345ec53891e0a5dd91ab97;p=citadel.git diff --git a/webcit/gettext.c b/webcit/gettext.c index 7713ce70d..8c749cb27 100644 --- a/webcit/gettext.c +++ b/webcit/gettext.c @@ -1,41 +1,48 @@ /* - * Copyright (c) 1996-2010 by the citadel.org team + * Copyright (c) 1996-2012 by the citadel.org team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. + * This program is open source software. You can redistribute it and/or + * modify it under the terms of the GNU General Public License version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #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[] = { - "C", "en_US", + "ar_AE", + "bg_BG", + "cs_CZ", + "en_US", + "da_DK", "de_DE", - "it_IT", - "es_ES", + "el_GR", "en_GB", - "da_DK", + "es_ES", + "et_EE", + "fi_FI", "fr_FR", + "hu_HU", + "it_IT", + "ko_KO", "nl_NL", + "pl_PL", "pt_BR", - "hu_HU", - "et_EE", "ru_RU", + "zh_CN", + "he_IL", + "kk_KK", + "ro_RO", + "sl_SL", + "tr_TR", "" }; @@ -43,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? */ +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? */ } LangStruct; /* parse browser locale header @@ -71,6 +78,7 @@ void httplang_to_locale(StrBuf *LocaleString, wcsession *sess) LangStruct wanted_locales[SEARCH_LANG]; LangStruct *ls; + long len; int i = 0; int j = 0; /* size_t len = strlen(LocaleString); */ @@ -99,39 +107,42 @@ 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, '='); sbuflen = StrLength(SBuf); - for (k = 0; k < sbuflen; k++) - if (ChrPtr(SBuf)[k] == '.') + for (k = 0; k < sbuflen; k++) { + if (ChrPtr(SBuf)[k] == '.') { StrBufPeek(SBuf, NULL, k, '0'); + } + } ls->priority = StrTol(SBuf); } else { ls->priority = 1000; } - /** get the locale part */ + /* get the locale part */ StrBufExtract_token(SBuf, Buf, 0, ';'); - /** get the lang part, which should be allways there */ - extract_token(&ls->lang[0], + /* get the lang part, which should be allways there */ + extract_token(ls->lang, ChrPtr(SBuf), 0, '-', sizeof(ls->lang)); - /** get the area code if any. */ + /* get the area code if any. */ if (StrBufNum_tokens(SBuf, '-') > 1) { - extract_token(&ls->region[0], + extract_token(ls->region, ChrPtr(SBuf), 1, '-', - sizeof(ls->region)); + sizeof(ls->region) + ); } - else { /** no ara code? use lang code */ - blen=strlen(&ls->lang[0]); - memcpy(&ls->region[0], ls->lang, blen); + else { /* no ara code? use lang code */ + blen = strlen(ls->lang); + memcpy(ls->region, ls->lang, blen); ls->region[blen] = '\0'; } @@ -141,27 +152,28 @@ void httplang_to_locale(StrBuf *LocaleString, wcsession *sess) { int chars; chars = toupper(ls->region[j]); - ls->region[j] = (char)chars;/** \todo ?! */ + ls->region[j] = (char)chars; /* todo ? */ } - snprintf(&lbuf[0], + snprintf(lbuf, sizeof(lbuf), "%s_%s", - &ls->lang[0], - &ls->region[0]); + ls->lang, + ls->region); - /** check if we have this lang */ + /* check if we have this lang */ ls->availability = 1; ls->selectedlang = -1; + len = strlen(ls->lang); 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)){ + /* match against the LANG part */ + result = strncasecmp(ls->lang, AvailLangLoaded[j], len); + if ((result == 0) && (result < ls->availability)){ ls->availability = result; ls->selectedlang = j; } - /** match against lang and locale */ - if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){ + /* match against lang and locale */ + if (0 == strcasecmp(lbuf, AvailLangLoaded[j])){ ls->availability = 0; ls->selectedlang = j; j = nLocalesLoaded; @@ -174,25 +186,27 @@ void httplang_to_locale(StrBuf *LocaleString, wcsession *sess) nBest = -1; for (i = 0; ((i < nParts) && (iavailability <= 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; } } if (nBest == -1) { - /** fall back to C */ + /* fall back to C */ nBest=0; } sess->selected_language = nBest; - lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]); + syslog(LOG_DEBUG, "language found: %s", AvailLangLoaded[sess->selected_language]); FreeStrBuf(&Buf); FreeStrBuf(&SBuf); } + /* * show the language chooser on the login dialog * depending on the browser locale change the sequence of the @@ -208,13 +222,12 @@ void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP) Lang = "C"; #endif - if (nLocalesLoaded == 1) { wc_printf("

%s

", AvailLangLoaded[0]); return; } - wc_printf("\n"); for (i=0; i < nLocalesLoaded; ++i) { #ifndef HAVE_USELOCALE @@ -239,6 +252,7 @@ void set_selected_language(const char *lang) { for (i = 0; iselected_language = i; + break; } } #endif @@ -250,9 +264,14 @@ void set_selected_language(const char *lang) { 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 */ + 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 */ #else char *language; @@ -266,8 +285,8 @@ 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 } @@ -283,8 +302,6 @@ void initialize_locales(void) { int i; char buf[32]; char *language = NULL; - char *locale; - nLocales = 0; while (!IsEmptyStr(AvailLang[nLocales])) @@ -292,7 +309,7 @@ void initialize_locales(void) { language = getenv("WEBCIT_LANG"); if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) { - lprintf(9, "Nailing locale to %s\n", language); + syslog(LOG_INFO, "Nailing locale to %s", language); } else language = NULL; @@ -305,14 +322,12 @@ void initialize_locales(void) { 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 */ + sprintf(buf, "C"); /* locale 0 (C) is ascii, not utf-8 */ + } else { sprintf(buf, "%s.UTF8", AvailLang[i]); @@ -324,13 +339,10 @@ void initialize_locales(void) { (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale) ); if (wc_locales[nLocalesLoaded] == NULL) { - lprintf(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n", - buf, - strerror(errno) - ); + syslog(LOG_NOTICE, "locale for %s disabled: %s", buf, strerror(errno)); } else { - lprintf(3, "Found locale: %s\n", buf); + syslog(LOG_INFO, "Found locale: %s - %s", buf, AvailLang[i]); AvailLangLoaded[nLocalesLoaded] = AvailLang[i]; nLocalesLoaded++; } @@ -349,12 +361,13 @@ void initialize_locales(void) { #endif } if ((language != NULL) && (nLocalesLoaded == 0)) { - lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language); + 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); + Empty_Locale + ); #else setlocale(LC_MESSAGES, AvailLang[0]); setenv("LANG", AvailLang[0], 1); @@ -364,12 +377,10 @@ void initialize_locales(void) { } #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")); - + 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")); #endif } @@ -381,8 +392,9 @@ ServerShutdownModule_GETTEXT #ifdef HAVE_USELOCALE int i; for (i = 0; i < nLocalesLoaded; ++i) { - if (Empty_Locale != wc_locales[i]) + if (Empty_Locale != wc_locales[i]) { freelocale(wc_locales[i]); + } } free(wc_locales); #endif @@ -391,9 +403,18 @@ ServerShutdownModule_GETTEXT #else /* ENABLE_NLS */ const char *AvailLang[] = { - "C", ""}; + "C", + "" +}; /* dummy for non NLS enabled systems */ +void +ServerShutdownModule_GETTEXT +(void) +{ +} + + void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP) { wc_printf("English (US)"); @@ -416,10 +437,11 @@ void initialize_locales(void) { #endif /* ENABLE_NLS */ - void TmplGettext(StrBuf *Target, WCTemplputParams *TP) { - StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0); + const char *Text = _(TP->Tokens->Params[0]->Start); + + StrBufAppendTemplateStr(Target, TP, Text, 1); } @@ -430,7 +452,7 @@ void TmplGettext(StrBuf *Target, WCTemplputParams *TP) const char *get_selected_language(void) { #ifdef ENABLE_NLS #ifdef HAVE_USELOCALE - return AvailLang[WC->selected_language]; + return AvailLangLoaded[WC->selected_language]; #else return "en"; #endif @@ -445,6 +467,7 @@ void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr) hdr->HR.browser_language = Line; } + void InitModule_GETTEXT (void) @@ -464,8 +487,10 @@ SessionNewModule_GETTEXT (wcsession *sess) { #ifdef ENABLE_NLS - if (!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