X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit%2Fgettext.c;h=930d9df38abb4e33b425b54e2fdd2b048ab6c618;hb=808f3be91dd6b6677e380695e2f16e6473141a7e;hp=549dfa0d89742d4444ef3b0365f8d6f2c57323db;hpb=0cb097cb436138481a62845f45118ee7979ae75d;p=citadel.git diff --git a/webcit/gettext.c b/webcit/gettext.c index 549dfa0d8..930d9df38 100644 --- a/webcit/gettext.c +++ b/webcit/gettext.c @@ -51,7 +51,7 @@ typedef struct _lang_pref{ * \param LocaleString the string from the browser http headers */ -void httplang_to_locale(char *LocaleString) +void httplang_to_locale(StrBuf *LocaleString) { LangStruct wanted_locales[SEARCH_LANG]; LangStruct *ls; @@ -63,70 +63,79 @@ void httplang_to_locale(char *LocaleString) int av; int nBest; int nParts; - char search[1024]; + StrBuf *Buf = NULL; + StrBuf *SBuf = NULL; - safestrncpy(search, LocaleString, sizeof search); - nParts=num_tokens(search,','); + nParts=StrBufNum_tokens(LocaleString,','); for (i=0; ((ipriority=atol(&sbuf[0]); - } - else { - ls->priority=1000; - } - /** get the locale part */ - extract_token(&sbuf[0],&buf[0],0,';',16); - /** get the lang part, which should be allways there */ - extract_token(&ls->lang[0],&sbuf[0],0,'-',16); - /** get the area code if any. */ - if (num_tokens(&sbuf[0],'-')>1) { - extract_token(&ls->region[0],&sbuf[0],1,'-',16); - } - 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; jregion[j]); - ls->region[j]=(char)chars;/** \todo ?! */ - } - sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]); + if (Buf == NULL) { + Buf = NewStrBuf(); + SBuf = NewStrBuf(); + } + else { + FlushStrBuf(Buf); + FlushStrBuf(SBuf); + } + + ls=&wanted_locales[i]; + + StrBufExtract_token(Buf,LocaleString, i,','); + /** 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; kpriority=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, '-', 16); + /** get the area code if any. */ + if (StrBufNum_tokens(SBuf,'-') > 1) { + extract_token(&ls->region[0],ChrPtr(SBuf),1,'-',16); + } + 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; jregion[j]); + ls->region[j]=(char)chars;/** \todo ?! */ + } + sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]); - /** check if we have this lang */ - ls->availability=1; - ls->selectedlang=-1; - for (j=0; jlang[0], AvailLangLoaded[j]); - if ((result<0)&&(resultavailability)){ - 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; - } + /** check if we have this lang */ + ls->availability=1; + ls->selectedlang=-1; + for (j=0; jlang[0], AvailLangLoaded[j]); + if ((result<0)&&(resultavailability)){ + 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; @@ -149,52 +158,17 @@ void httplang_to_locale(char *LocaleString) } WC->selected_language=nBest; lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]); + FreeStrBuf(&Buf); + FreeStrBuf(&SBuf); } -/* TODO: we skip the language weighting 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(char *LocaleString) -//{ -// char selected_locale[16]; -// int i, j; -// char lang[64]; -// int num_accept = 0; -// -// lprintf(9, "languageAccept: %s\n", LocaleString); -// -// strcpy(selected_locale, "C"); -// num_accept = num_tokens(LocaleString, ','); -// -// for (i=num_accept-1; 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; jselected_language < 0) return; uselocale(wc_locales[WCC->selected_language]); /** switch locales */ textdomain(textdomain(NULL)); /** clear the cache */ @@ -293,7 +267,7 @@ void initialize_locales(void) { for (i = 0; i < NUM_LANGS; ++i) { if (i == 0) { - sprintf(buf, "%s", AvailLang[i]); // locale 0 (C) is ascii, not utf-8 + sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */ } else { sprintf(buf, "%s.UTF8", AvailLang[i]); @@ -319,6 +293,7 @@ void initialize_locales(void) { } } + void ShutdownLocale(void) { int i; @@ -327,13 +302,13 @@ void ShutdownLocale(void) if (Empty_Locale != wc_locales[i]) freelocale(wc_locales[i]); } - freelocale(Empty_Locale); #endif } #else /* ENABLE_NLS */ /** \brief dummy for non NLS enabled systems */ -void offer_languages(void) { +void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP) +{ wprintf("English (US)"); } @@ -355,3 +330,31 @@ void preset_locale(void) #endif /* ENABLE_NLS */ +void TmplGettext(StrBuf *Target, WCTemplputParams *TP) +{ + StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0); +} + + +/* + * Returns the language currently in use. + * This function returns a static string, so don't do anything stupid please. + */ +const char *get_selected_language(void) { +#ifdef ENABLE_NLS +#ifdef HAVE_USELOCALE + return AvailLang[WC->selected_language]; +#else + return "en" +#endif +#else + return "en" +#endif +} + +void +InitModule_GETTEXT +(void) +{ + RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE); +}