From 0bfc1b9cb968626402becec77bea085623d54e2b Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Thu, 8 Dec 2005 22:24:24 +0000 Subject: [PATCH] * Detect the browser language, and use it as the default language selection. --- webcit/ChangeLog | 3 + webcit/context_loop.c | 8 ++- webcit/gettext.c | 150 +++++++++--------------------------------- webcit/webcit.h | 1 + 4 files changed, 39 insertions(+), 123 deletions(-) diff --git a/webcit/ChangeLog b/webcit/ChangeLog index 18011f39f..88805841e 100644 --- a/webcit/ChangeLog +++ b/webcit/ChangeLog @@ -1,5 +1,8 @@ $Id$ +Thu Dec 8 17:23:46 EST 2005 ajc +* Detect the browser language, and use it as the default language selection. + Thu Dec 8 11:48:34 EST 2005 ajc * Fix "locale leak" on newer Linux systems where gettext() seems to cache its translated messages. textdomain(textdomain(NULL)) clears the cache. diff --git a/webcit/context_loop.c b/webcit/context_loop.c index bde5ff2a6..c013fe8e7 100644 --- a/webcit/context_loop.c +++ b/webcit/context_loop.c @@ -251,6 +251,7 @@ void context_loop(int sock) char httpauth_pass[1024]; char accept_language[256]; char *ptr = NULL; + int session_is_new = 0; strcpy(httpauth_string, ""); strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER); @@ -294,11 +295,9 @@ void context_loop(int sock) if_modified_since = httpdate_to_timestamp(&buf[19]); } -#ifdef ENABLE_NLS if (!strncasecmp(buf, "Accept-Language: ", 17)) { safestrncpy(accept_language, &buf[17], sizeof accept_language); } -#endif /* * Read in the request @@ -414,11 +413,11 @@ void context_loop(int sock) strcpy(TheSession->httpauth_user, httpauth_user); strcpy(TheSession->httpauth_pass, httpauth_pass); pthread_mutex_init(&TheSession->SessionMutex, NULL); - pthread_mutex_lock(&SessionListMutex); TheSession->next = SessionList; SessionList = TheSession; pthread_mutex_unlock(&SessionListMutex); + session_is_new = 1; } /* @@ -435,6 +434,9 @@ void context_loop(int sock) TheSession->lastreq = time(NULL); /* log */ TheSession->gzip_ok = gzip_ok; #ifdef ENABLE_NLS + if (session_is_new) { + httplang_to_locale(accept_language); + } go_selected_language(); /* set locale */ #endif session_loop(req); /* do transaction */ diff --git a/webcit/gettext.c b/webcit/gettext.c index 1f632ca9b..e658bea40 100644 --- a/webcit/gettext.c +++ b/webcit/gettext.c @@ -5,7 +5,7 @@ #ifdef ENABLE_NLS #define NUM_LANGS 3 -static const char *AvailLang[NUM_LANGS] = { +char *AvailLang[NUM_LANGS] = { "en_US", "de_DE", "it_IT" @@ -23,141 +23,50 @@ typedef struct _lang_pref{ /* TODO: we skip the language weightening 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(const char *LocaleString) +void httplang_to_locale(char *LocaleString) { - char *locale = "C"; - LangStruct wanted_locales[20]; - int i = 0; - int j = 0; - size_t len = strlen(LocaleString); - int nFound = 0; - int nParts; - const int nAvail = 1; /* Number of members in AvailLang */ - char *search = (char *) malloc(len); - // locale_t my_Locale; - // locale_t my_Empty_Locale; - - memcpy(search, LocaleString, len); - search[len] = '\0'; - nParts=num_tokens(search,','); - for (i=0; ((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; j1) { - extract_token(&wanted_locales[i].region[0],&sbuf[0],1,'-',16); - } - else { /* no ara code? use lang code */ - blen=strlen(&wanted_locales[i].lang[0]); - memcpy(&wanted_locales[i].region[0], wanted_locales[i].lang,blen); - wanted_locales[i].region[blen]='\0'; - } /* area codes are uppercase */ - blen=strlen(&wanted_locales[i].region[0]); - for (j=0; j 0) { - for (i = 0; i <= nFound; i++) { - for (j = 0; j < nAvail; j++) { - int ret = strncasecmp(&wanted_locales[i].lang[0], - AvailLang[j], - strlen(&wanted_locales[i].lang[0])); - if (!ret) { - locale = (char *) AvailLang[j]; //wanted_locales[i]; - i = nFound + 1; - j = nAvail + 1; - continue; - } - + for (j=0; j0) - { - extract_token(prefers,search, 1,';',len); - extract_token(langs,search, 0,';',len); - } - else - { - free(prefers); - prefers=NULL; - memcpy(search, len); - search[len] = '\0'; - } - i = 0; - while ( !done && (nFound < 10)) { - if ((search[i] == ',') || (search[i] == ';') || (search[i] == '\0')) - { - if ((search[i] == ';') || (search[i] == '\0')) - done = 1; - search[i] = '\0'; - wanted_locales[nFound] = (char *) &search[j]; - j = i + 1; - nFound++; - } - - i++; - } -*/ - - - void offer_languages(void) { int i; wprintf("\n"); @@ -180,6 +89,7 @@ void set_selected_language(char *lang) { * Activate and deactivate the selected language for this session. */ void go_selected_language(void) { + if (WC->selected_language < 0) return; uselocale(wc_locales[WC->selected_language]); /* switch locales */ textdomain(textdomain(NULL)); /* clear the cache */ } diff --git a/webcit/webcit.h b/webcit/webcit.h index 1a66c76c1..d61fef65a 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -657,6 +657,7 @@ void offer_languages(void); void set_selected_language(char *); void go_selected_language(void); void stop_selected_language(void); +void httplang_to_locale(char *LocaleString); void embed_room_banner(char *, int); /* navbar types that can be passed to embed_room_banner */ -- 2.39.2