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[] = {
45 const char **AvailLangLoaded;
46 long nLocalesLoaded = 0;
49 locale_t *wc_locales; /* here we keep the parsed stuff */
52 /* Keep information about one locale */
53 typedef struct _lang_pref{
54 char lang[16]; /* the language locale string */
55 char region[16]; /* the region locale string */
56 long priority; /* which priority does it have */
57 int availability; /* do we know it? */
58 int selectedlang; /* is this the selected language? */
61 /* parse browser locale header
63 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
65 * 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
67 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
68 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
69 * 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
72 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
74 LangStruct wanted_locales[SEARCH_LANG];
79 /* size_t len = strlen(LocaleString); */
87 nParts = StrBufNum_tokens(LocaleString, ',');
88 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
102 ls = &wanted_locales[i];
104 StrBufExtract_token(Buf, LocaleString, i, ',');
105 /* we are searching, if this list item has something like ;q=n*/
106 if (StrBufNum_tokens(Buf, '=') > 1) {
108 StrBufExtract_token(SBuf, Buf, 1, '=');
109 sbuflen = StrLength(SBuf);
110 for (k = 0; k < sbuflen; k++) {
111 if (ChrPtr(SBuf)[k] == '.') {
112 StrBufPeek(SBuf, NULL, k, '0');
115 ls->priority = StrTol(SBuf);
121 /* get the locale part */
122 StrBufExtract_token(SBuf, Buf, 0, ';');
124 /* get the lang part, which should be allways there */
125 extract_token(&ls->lang[0],
130 /* get the area code if any. */
131 if (StrBufNum_tokens(SBuf, '-') > 1) {
132 extract_token(&ls->region[0],
138 else { /* no ara code? use lang code */
139 blen=strlen(&ls->lang[0]);
140 memcpy(&ls->region[0], ls->lang, blen);
141 ls->region[blen] = '\0';
144 /* area codes are uppercase */
145 blen = strlen(&ls->region[0]);
146 for (j = 0; j < blen; j++)
149 chars = toupper(ls->region[j]);
150 ls->region[j] = (char)chars; /* todo ? */
158 /* check if we have this lang */
159 ls->availability = 1;
160 ls->selectedlang = -1;
161 for (j = 0; j < nLocalesLoaded; j++) {
163 /* match against the LANG part */
164 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
165 if ((result < 0) && (result < ls->availability)){
166 ls->availability = result;
167 ls->selectedlang = j;
169 /* match against lang and locale */
170 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
171 ls->availability = 0;
172 ls->selectedlang = j;
181 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
182 ls = &wanted_locales[i];
183 if ( (ls->availability <= 0)
184 && (av < ls->availability)
185 && (prio < ls->priority)
186 && (ls->selectedlang != -1)
188 nBest = ls->selectedlang;
189 av = ls->availability;
197 sess->selected_language = nBest;
198 syslog(9, "language found: %s", AvailLangLoaded[WC->selected_language]);
205 * show the language chooser on the login dialog
206 * depending on the browser locale change the sequence of the
209 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
212 #ifndef HAVE_USELOCALE
213 char *Lang = getenv("LANG");
219 if (nLocalesLoaded == 1) {
220 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
224 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\" onChange=\"switch_to_lang($('lname').value);\">\n");
226 for (i=0; i < nLocalesLoaded; ++i) {
227 #ifndef HAVE_USELOCALE
228 if (strcmp(AvailLangLoaded[i], Lang) == 0)
230 wc_printf("<option %s value=%s>%s</option>\n",
231 ((WC->selected_language == i) ? "selected" : ""),
237 wc_printf("</select>\n");
241 * Set the selected language for this session.
243 void set_selected_language(const char *lang) {
244 #ifdef HAVE_USELOCALE
246 for (i = 0; i<nLocalesLoaded; ++i) {
247 if (!strcasecmp(lang, AvailLangLoaded[i])) {
248 WC->selected_language = i;
256 * Activate the selected language for this session.
258 void go_selected_language(void) {
259 #ifdef HAVE_USELOCALE
261 if (WCC->selected_language < 0) return;
262 uselocale(wc_locales[WCC->selected_language]); /* switch locales */
263 textdomain(textdomain(NULL)); /* clear the cache */
267 language = getenv("LANG");
268 setlocale(LC_MESSAGES, language);
273 * Deactivate the selected language for this session.
275 void stop_selected_language(void) {
276 #ifdef HAVE_USELOCALE
277 uselocale(LC_GLOBAL_LOCALE); /* switch locales */
278 textdomain(textdomain(NULL)); /* clear the cache */
282 #ifdef HAVE_USELOCALE
283 locale_t Empty_Locale;
287 * Create a locale_t for each available language
289 void initialize_locales(void) {
293 char *language = NULL;
296 while (!IsEmptyStr(AvailLang[nLocales]))
299 language = getenv("WEBCIT_LANG");
300 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
301 syslog(9, "Nailing locale to %s", language);
303 else language = NULL;
305 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
306 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
307 #ifdef HAVE_USELOCALE
308 wc_locales = malloc (sizeof(locale_t) * nLocales);
309 memset(wc_locales,0, sizeof(locale_t) * nLocales);
310 /* create default locale */
311 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
314 for (i = 0; i < nLocales; ++i) {
315 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
318 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
321 sprintf(buf, "%s.UTF8", AvailLang[i]);
323 #ifdef HAVE_USELOCALE
324 wc_locales[nLocalesLoaded] = newlocale(
325 (LC_MESSAGES_MASK|LC_TIME_MASK),
327 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
329 if (wc_locales[nLocalesLoaded] == NULL) {
330 syslog(1, "locale for %s disabled: %s", buf, strerror(errno));
333 syslog(3, "Found locale: %s", buf);
334 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
338 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
339 setenv("LANG", buf, 1);
340 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
341 setlocale(LC_MESSAGES, AvailLang[i]);
344 else if (nLocalesLoaded == 0) {
345 setenv("LANG", buf, 1);
346 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
351 if ((language != NULL) && (nLocalesLoaded == 0)) {
352 syslog(1, "Your selected locale [%s] isn't available on your system. falling back to C", language);
353 #ifdef HAVE_USELOCALE
354 wc_locales[0] = newlocale(
355 (LC_MESSAGES_MASK|LC_TIME_MASK),
360 setlocale(LC_MESSAGES, AvailLang[0]);
361 setenv("LANG", AvailLang[0], 1);
363 AvailLangLoaded[0] = AvailLang[0];
368 setlocale(LC_ALL, "");
369 syslog(9, "Message catalog directory: %s", bindtextdomain("webcit", LOCALEDIR"/locale"));
370 syslog(9, "Text domain: %s", textdomain("webcit"));
371 syslog(9, "Text domain Charset: %s", bind_textdomain_codeset("webcit","UTF8"));
377 ServerShutdownModule_GETTEXT
380 #ifdef HAVE_USELOCALE
382 for (i = 0; i < nLocalesLoaded; ++i) {
383 if (Empty_Locale != wc_locales[i]) {
384 freelocale(wc_locales[i]);
389 free(AvailLangLoaded);
392 #else /* ENABLE_NLS */
393 const char *AvailLang[] = {
398 /* dummy for non NLS enabled systems */
399 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
401 wc_printf("English (US)");
404 /* dummy for non NLS enabled systems */
405 void set_selected_language(const char *lang) {
408 /* dummy for non NLS enabled systems */
409 void go_selected_language(void) {
412 /* dummy for non NLS enabled systems */
413 void stop_selected_language(void) {
416 void initialize_locales(void) {
419 #endif /* ENABLE_NLS */
422 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
424 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
429 * Returns the language currently in use.
430 * This function returns a static string, so don't do anything stupid please.
432 const char *get_selected_language(void) {
434 #ifdef HAVE_USELOCALE
435 return AvailLang[WC->selected_language];
445 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
447 hdr->HR.browser_language = Line;
455 initialize_locales();
457 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
458 Header_HandleAcceptLanguage);
460 RegisterNamespace("LANG:SELECT", 0, 0,
461 tmplput_offer_languages, NULL, CTX_NONE);
466 SessionNewModule_GETTEXT
471 && (!sess->Hdr->HR.Static)
472 && (sess->Hdr->HR.browser_language != NULL)
474 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
480 SessionAttachModule_GETTEXT
484 go_selected_language(); /* set locale */
489 SessionDestroyModule_GETTEXT
493 stop_selected_language(); /* unset locale */