7 #define SEARCH_LANG 20 /* how many langs should we parse? */
10 /* actual supported locales */
11 const char *AvailLang[] = {
28 const char **AvailLangLoaded;
29 long nLocalesLoaded = 0;
32 locale_t *wc_locales; /**< here we keep the parsed stuff */
35 /** Keep information about one locale */
36 typedef struct _lang_pref{
37 char lang[16]; /**< the language locale string */
38 char region[16]; /**< the region locale string */
39 long priority; /**< which priority does it have */
40 int availability; /**< do we know it? */
41 int selectedlang; /**< is this the selected language? */
44 /* \brief parse browser locale header
45 * seems as most browsers just do a one after coma value even if more than 10 locales are available. Sample strings:
47 * Accept-Language: sq;q=1.0,de;q=0.9,as;q=0.8,ar;q=0.7,bn;q=0.6,zh-cn;q=0.5,kn;q=0.4,ch;q=0.3,fo;q=0.2,gn;q=0.1,ce;q=0.1,ie;q=0.1
49 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
50 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
51 * 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
52 * \param LocaleString the string from the browser http headers
55 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
57 LangStruct wanted_locales[SEARCH_LANG];
62 /* size_t len = strlen(LocaleString); */
70 nParts = StrBufNum_tokens(LocaleString, ',');
71 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
85 ls = &wanted_locales[i];
87 StrBufExtract_token(Buf, LocaleString, i, ',');
88 /** we are searching, if this list item has something like ;q=n*/
89 if (StrBufNum_tokens(Buf, '=') > 1) {
91 StrBufExtract_token(SBuf, Buf, 1, '=');
92 sbuflen = StrLength(SBuf);
93 for (k = 0; k < sbuflen; k++)
94 if (ChrPtr(SBuf)[k] == '.')
95 StrBufPeek(SBuf, NULL, k, '0');
96 ls->priority = StrTol(SBuf);
102 /** get the locale part */
103 StrBufExtract_token(SBuf, Buf, 0, ';');
105 /** get the lang part, which should be allways there */
106 extract_token(&ls->lang[0],
111 /** get the area code if any. */
112 if (StrBufNum_tokens(SBuf, '-') > 1) {
113 extract_token(&ls->region[0],
118 else { /** no ara code? use lang code */
119 blen=strlen(&ls->lang[0]);
120 memcpy(&ls->region[0], ls->lang, blen);
121 ls->region[blen] = '\0';
124 /** area codes are uppercase */
125 blen = strlen(&ls->region[0]);
126 for (j = 0; j < blen; j++)
129 chars = toupper(ls->region[j]);
130 ls->region[j] = (char)chars;/** \todo ?! */
138 /** check if we have this lang */
139 ls->availability = 1;
140 ls->selectedlang = -1;
141 for (j = 0; j < nLocalesLoaded; j++) {
143 /** match against the LANG part */
144 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
145 if ((result < 0) && (result < ls->availability)){
146 ls->availability = result;
147 ls->selectedlang = j;
149 /** match against lang and locale */
150 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
151 ls->availability = 0;
152 ls->selectedlang = j;
161 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
162 ls = &wanted_locales[i];
163 if ((ls->availability <= 0) &&
164 (av < ls->availability) &&
165 (prio < ls->priority) &&
166 (ls->selectedlang != -1)) {
167 nBest = ls->selectedlang;
168 av = ls->availability;
173 /** fall back to C */
176 sess->selected_language = nBest;
177 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
183 * \brief show the language chooser on the login dialog
184 * depending on the browser locale change the sequence of the
187 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
190 #ifndef HAVE_USELOCALE
191 char *Lang = getenv("LANG");
198 if (nLocalesLoaded == 1) {
199 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
203 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
205 for (i=0; i < nLocalesLoaded; ++i) {
206 #ifndef HAVE_USELOCALE
207 if (strcmp(AvailLangLoaded[i], Lang) == 0)
209 wc_printf("<option %s value=%s>%s</option>\n",
210 ((WC->selected_language == i) ? "selected" : ""),
216 wc_printf("</select>\n");
220 * \brief Set the selected language for this session.
221 * \param lang the locale to set.
223 void set_selected_language(const char *lang) {
224 #ifdef HAVE_USELOCALE
226 for (i = 0; i<nLocalesLoaded; ++i) {
227 if (!strcasecmp(lang, AvailLangLoaded[i])) {
228 WC->selected_language = i;
235 * \brief Activate the selected language for this session.
237 void go_selected_language(void) {
238 #ifdef HAVE_USELOCALE
240 if (WCC->selected_language < 0) return;
241 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
242 textdomain(textdomain(NULL)); /** clear the cache */
246 language = getenv("LANG");
247 setlocale(LC_MESSAGES, language);
252 * \brief Deactivate the selected language for this session.
254 void stop_selected_language(void) {
255 #ifdef HAVE_USELOCALE
256 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
257 textdomain(textdomain(NULL)); /** clear the cache */
261 #ifdef HAVE_USELOCALE
262 locale_t Empty_Locale;
266 * \brief Create a locale_t for each available language
268 void initialize_locales(void) {
272 char *language = NULL;
277 while (!IsEmptyStr(AvailLang[nLocales]))
280 language = getenv("WEBCIT_LANG");
281 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
282 lprintf(9, "Nailing locale to %s\n", language);
284 else language = NULL;
286 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
287 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
288 #ifdef HAVE_USELOCALE
289 wc_locales = malloc (sizeof(locale_t) * nLocales);
290 memset(wc_locales,0, sizeof(locale_t) * nLocales);
291 /* create default locale */
292 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
298 for (i = 0; i < nLocales; ++i) {
299 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
302 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
305 sprintf(buf, "%s.UTF8", AvailLang[i]);
307 #ifdef HAVE_USELOCALE
308 wc_locales[nLocalesLoaded] = newlocale(
309 (LC_MESSAGES_MASK|LC_TIME_MASK),
311 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
313 if (wc_locales[nLocalesLoaded] == NULL) {
314 lprintf(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
320 lprintf(3, "Found locale: %s\n", buf);
321 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
325 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
326 setenv("LANG", buf, 1);
327 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
328 setlocale(LC_MESSAGES, AvailLang[i]);
331 else if (nLocalesLoaded == 0) {
332 setenv("LANG", buf, 1);
333 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
338 if ((language != NULL) && (nLocalesLoaded == 0)) {
339 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
340 #ifdef HAVE_USELOCALE
341 wc_locales[0] = newlocale(
342 (LC_MESSAGES_MASK|LC_TIME_MASK),
346 setlocale(LC_MESSAGES, AvailLang[0]);
347 setenv("LANG", AvailLang[0], 1);
349 AvailLangLoaded[0] = AvailLang[0];
352 #ifndef HAVE_USELOCALE
358 locale = setlocale(LC_ALL, "");
360 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
361 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
362 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
369 ServerShutdownModule_GETTEXT
372 #ifdef HAVE_USELOCALE
374 for (i = 0; i < nLocalesLoaded; ++i) {
375 if (Empty_Locale != wc_locales[i])
376 freelocale(wc_locales[i]);
380 free(AvailLangLoaded);
383 #else /* ENABLE_NLS */
384 const char *AvailLang[] = {
387 /** \brief dummy for non NLS enabled systems */
388 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
390 wc_printf("English (US)");
393 /** \brief dummy for non NLS enabled systems */
394 void set_selected_language(char *lang) {
397 /** \brief dummy for non NLS enabled systems */
398 void go_selected_language(void) {
401 /** \brief dummy for non NLS enabled systems */
402 void stop_selected_language(void) {
405 #endif /* ENABLE_NLS */
408 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
410 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
415 * Returns the language currently in use.
416 * This function returns a static string, so don't do anything stupid please.
418 const char *get_selected_language(void) {
420 #ifdef HAVE_USELOCALE
421 return AvailLang[WC->selected_language];
431 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
433 hdr->HR.browser_language = Line;
440 initialize_locales();
442 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
443 Header_HandleAcceptLanguage);
445 RegisterNamespace("LANG:SELECT", 0, 0,
446 tmplput_offer_languages, NULL, CTX_NONE);
451 SessionNewModule_GETTEXT
455 if (!sess->Hdr->HR.Static &&
456 (sess->Hdr->HR.browser_language != NULL)) {
457 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
463 SessionAttachModule_GETTEXT
467 go_selected_language(); /* set locale */
472 SessionDestroyModule_GETTEXT
476 stop_selected_language(); /* unset locale */