2 * Copyright (c) 1996-2012 by the citadel.org team
4 * This program is open source software. You can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
14 #include "webserver.h"
15 #define SEARCH_LANG 20 /* how many langs should we parse? */
18 /* actual supported locales */
19 const char *AvailLang[] = {
46 const char **AvailLangLoaded;
47 long nLocalesLoaded = 0;
50 locale_t *wc_locales; /* here we keep the parsed stuff */
53 /* Keep information about one locale */
54 typedef struct _lang_pref {
55 char lang[16]; /* the language locale string */
56 char region[16]; /* the region locale string */
57 long priority; /* which priority does it have */
58 int availability; /* do we know it? */
59 int selectedlang; /* is this the selected language? */
62 /* parse browser locale header
64 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
66 * 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
68 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
69 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
70 * 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
73 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
75 LangStruct wanted_locales[SEARCH_LANG];
80 /* size_t len = strlen(LocaleString); */
88 nParts = StrBufNum_tokens(LocaleString, ',');
89 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
103 ls = &wanted_locales[i];
105 StrBufExtract_token(Buf, LocaleString, i, ',');
106 /* we are searching, if this list item has something like ;q=n*/
107 if (StrBufNum_tokens(Buf, '=') > 1) {
109 StrBufExtract_token(SBuf, Buf, 1, '=');
110 sbuflen = StrLength(SBuf);
111 for (k = 0; k < sbuflen; k++) {
112 if (ChrPtr(SBuf)[k] == '.') {
113 StrBufPeek(SBuf, NULL, k, '0');
116 ls->priority = StrTol(SBuf);
122 /* get the locale part */
123 StrBufExtract_token(SBuf, Buf, 0, ';');
125 /* get the lang part, which should be allways there */
126 extract_token(&ls->lang[0],
131 /* get the area code if any. */
132 if (StrBufNum_tokens(SBuf, '-') > 1) {
133 extract_token(&ls->region[0],
139 else { /* no ara code? use lang code */
140 blen=strlen(&ls->lang[0]);
141 memcpy(&ls->region[0], ls->lang, blen);
142 ls->region[blen] = '\0';
145 /* area codes are uppercase */
146 blen = strlen(&ls->region[0]);
147 for (j = 0; j < blen; j++)
150 chars = toupper(ls->region[j]);
151 ls->region[j] = (char)chars; /* todo ? */
159 /* check if we have this lang */
160 ls->availability = 1;
161 ls->selectedlang = -1;
162 for (j = 0; j < nLocalesLoaded; j++) {
164 /* match against the LANG part */
165 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
166 if ((result < 0) && (result < ls->availability)){
167 ls->availability = result;
168 ls->selectedlang = j;
170 /* match against lang and locale */
171 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
172 ls->availability = 0;
173 ls->selectedlang = j;
182 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
183 ls = &wanted_locales[i];
184 if ( (ls->availability <= 0)
185 && (av < ls->availability)
186 && (prio < ls->priority)
187 && (ls->selectedlang != -1)
189 nBest = ls->selectedlang;
190 av = ls->availability;
198 sess->selected_language = nBest;
199 syslog(9, "language found: %s", AvailLangLoaded[WC->selected_language]);
206 * show the language chooser on the login dialog
207 * depending on the browser locale change the sequence of the
210 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
213 #ifndef HAVE_USELOCALE
214 char *Lang = getenv("LANG");
220 if (nLocalesLoaded == 1) {
221 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
225 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\" onChange=\"switch_to_lang($('lname').value);\">\n");
227 for (i=0; i < nLocalesLoaded; ++i) {
228 #ifndef HAVE_USELOCALE
229 if (strcmp(AvailLangLoaded[i], Lang) == 0)
231 wc_printf("<option %s value=%s>%s</option>\n",
232 ((WC->selected_language == i) ? "selected" : ""),
238 wc_printf("</select>\n");
242 * Set the selected language for this session.
244 void set_selected_language(const char *lang) {
245 #ifdef HAVE_USELOCALE
247 for (i = 0; i<nLocalesLoaded; ++i) {
248 if (!strcasecmp(lang, AvailLangLoaded[i])) {
249 WC->selected_language = i;
257 * Activate the selected language for this session.
259 void go_selected_language(void) {
260 #ifdef HAVE_USELOCALE
262 if (WCC->selected_language < 0) return;
263 uselocale(wc_locales[WCC->selected_language]); /* switch locales */
264 textdomain(textdomain(NULL)); /* clear the cache */
268 language = getenv("LANG");
269 setlocale(LC_MESSAGES, language);
274 * Deactivate the selected language for this session.
276 void stop_selected_language(void) {
277 #ifdef HAVE_USELOCALE
278 uselocale(LC_GLOBAL_LOCALE); /* switch locales */
279 textdomain(textdomain(NULL)); /* clear the cache */
283 #ifdef HAVE_USELOCALE
284 locale_t Empty_Locale;
288 * Create a locale_t for each available language
290 void initialize_locales(void) {
294 char *language = NULL;
297 while (!IsEmptyStr(AvailLang[nLocales]))
300 language = getenv("WEBCIT_LANG");
301 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
302 syslog(9, "Nailing locale to %s", language);
304 else language = NULL;
306 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
307 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
308 #ifdef HAVE_USELOCALE
309 wc_locales = malloc (sizeof(locale_t) * nLocales);
310 memset(wc_locales,0, sizeof(locale_t) * nLocales);
311 /* create default locale */
312 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
315 for (i = 0; i < nLocales; ++i) {
316 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
319 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
322 sprintf(buf, "%s.UTF8", AvailLang[i]);
324 #ifdef HAVE_USELOCALE
325 wc_locales[nLocalesLoaded] = newlocale(
326 (LC_MESSAGES_MASK|LC_TIME_MASK),
328 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
330 if (wc_locales[nLocalesLoaded] == NULL) {
331 syslog(1, "locale for %s disabled: %s", buf, strerror(errno));
334 syslog(3, "Found locale: %s", buf);
335 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
339 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
340 setenv("LANG", buf, 1);
341 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
342 setlocale(LC_MESSAGES, AvailLang[i]);
345 else if (nLocalesLoaded == 0) {
346 setenv("LANG", buf, 1);
347 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
352 if ((language != NULL) && (nLocalesLoaded == 0)) {
353 syslog(1, "Your selected locale [%s] isn't available on your system. falling back to C", language);
354 #ifdef HAVE_USELOCALE
355 wc_locales[0] = newlocale(
356 (LC_MESSAGES_MASK|LC_TIME_MASK),
361 setlocale(LC_MESSAGES, AvailLang[0]);
362 setenv("LANG", AvailLang[0], 1);
364 AvailLangLoaded[0] = AvailLang[0];
369 setlocale(LC_ALL, "");
370 syslog(9, "Text domain: %s", textdomain("webcit"));
371 syslog(9, "Text domain Charset: %s", bind_textdomain_codeset("webcit", "UTF8"));
372 syslog(9, "Message catalog directory: %s", bindtextdomain(textdomain(NULL), LOCALEDIR"/locale"));
378 ServerShutdownModule_GETTEXT
381 #ifdef HAVE_USELOCALE
383 for (i = 0; i < nLocalesLoaded; ++i) {
384 if (Empty_Locale != wc_locales[i]) {
385 freelocale(wc_locales[i]);
390 free(AvailLangLoaded);
393 #else /* ENABLE_NLS */
394 const char *AvailLang[] = {
399 /* dummy for non NLS enabled systems */
401 ServerShutdownModule_GETTEXT
407 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
409 wc_printf("English (US)");
412 /* dummy for non NLS enabled systems */
413 void set_selected_language(const char *lang) {
416 /* dummy for non NLS enabled systems */
417 void go_selected_language(void) {
420 /* dummy for non NLS enabled systems */
421 void stop_selected_language(void) {
424 void initialize_locales(void) {
427 #endif /* ENABLE_NLS */
430 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
432 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
437 * Returns the language currently in use.
438 * This function returns a static string, so don't do anything stupid please.
440 const char *get_selected_language(void) {
442 #ifdef HAVE_USELOCALE
443 return AvailLang[WC->selected_language];
453 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
455 hdr->HR.browser_language = Line;
463 initialize_locales();
465 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
466 Header_HandleAcceptLanguage);
468 RegisterNamespace("LANG:SELECT", 0, 0,
469 tmplput_offer_languages, NULL, CTX_NONE);
474 SessionNewModule_GETTEXT
479 && (!sess->Hdr->HR.Static)
480 && (sess->Hdr->HR.browser_language != NULL)
482 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
488 SessionAttachModule_GETTEXT
492 go_selected_language(); /* set locale */
497 SessionDestroyModule_GETTEXT
501 stop_selected_language(); /* unset locale */