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 #include "language_list.h"
20 const char **AvailLangLoaded;
21 long nLocalesLoaded = 0;
24 locale_t *wc_locales; /* here we keep the parsed stuff */
27 /* Keep information about one locale */
28 typedef struct _lang_pref{
29 char lang[16]; /* the language locale string */
30 char region[16]; /* the region locale string */
31 long priority; /* which priority does it have */
32 int availability; /* do we know it? */
33 int selectedlang; /* is this the selected language? */
36 /* parse browser locale header
38 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
40 * 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
42 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
43 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
44 * 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
47 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
49 LangStruct wanted_locales[SEARCH_LANG];
54 /* size_t len = strlen(LocaleString); */
62 nParts = StrBufNum_tokens(LocaleString, ',');
63 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
77 ls = &wanted_locales[i];
79 StrBufExtract_token(Buf, LocaleString, i, ',');
80 /* we are searching, if this list item has something like ;q=n*/
81 if (StrBufNum_tokens(Buf, '=') > 1) {
83 StrBufExtract_token(SBuf, Buf, 1, '=');
84 sbuflen = StrLength(SBuf);
85 for (k = 0; k < sbuflen; k++) {
86 if (ChrPtr(SBuf)[k] == '.') {
87 StrBufPeek(SBuf, NULL, k, '0');
90 ls->priority = StrTol(SBuf);
96 /* get the locale part */
97 StrBufExtract_token(SBuf, Buf, 0, ';');
99 /* get the lang part, which should be allways there */
100 extract_token(&ls->lang[0],
105 /* get the area code if any. */
106 if (StrBufNum_tokens(SBuf, '-') > 1) {
107 extract_token(&ls->region[0],
113 else { /* no ara code? use lang code */
114 blen=strlen(&ls->lang[0]);
115 memcpy(&ls->region[0], ls->lang, blen);
116 ls->region[blen] = '\0';
119 /* area codes are uppercase */
120 blen = strlen(&ls->region[0]);
121 for (j = 0; j < blen; j++)
124 chars = toupper(ls->region[j]);
125 ls->region[j] = (char)chars; /* todo ? */
133 /* check if we have this lang */
134 ls->availability = 1;
135 ls->selectedlang = -1;
136 for (j = 0; j < nLocalesLoaded; j++) {
138 /* match against the LANG part */
139 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
140 if ((result < 0) && (result < ls->availability)){
141 ls->availability = result;
142 ls->selectedlang = j;
144 /* match against lang and locale */
145 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
146 ls->availability = 0;
147 ls->selectedlang = j;
156 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
157 ls = &wanted_locales[i];
158 if ( (ls->availability <= 0)
159 && (av < ls->availability)
160 && (prio < ls->priority)
161 && (ls->selectedlang != -1)
163 nBest = ls->selectedlang;
164 av = ls->availability;
172 sess->selected_language = nBest;
173 syslog(9, "language found: %s", AvailLangLoaded[WC->selected_language]);
180 * show the language chooser on the login dialog
181 * depending on the browser locale change the sequence of the
184 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
186 #ifdef HAVE_USELOCALE
189 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\" onChange=\"switch_to_lang($('lname').value);\">\n");
191 for (i=0; i < nLocalesLoaded; ++i) {
192 wc_printf("<option %s value=%s>%s</option>\n",
193 ((WC->selected_language == i) ? "selected" : ""),
199 wc_printf("</select>\n");
201 wc_printf("%s", (getenv("LANG") ? getenv("LANG") : "C"));
206 * Set the selected language for this session.
208 void set_selected_language(const char *lang) {
209 #ifdef HAVE_USELOCALE
211 for (i = 0; i<nLocalesLoaded; ++i) {
212 if (!strcasecmp(lang, AvailLangLoaded[i])) {
213 WC->selected_language = i;
221 * Activate the selected language for this session.
223 void go_selected_language(void) {
224 #ifdef HAVE_USELOCALE
226 if (WCC->selected_language < 0) return;
227 uselocale(wc_locales[WCC->selected_language]); /* switch locales */
228 textdomain(textdomain(NULL)); /* clear the cache */
232 language = getenv("LANG");
233 setlocale(LC_MESSAGES, language);
238 * Deactivate the selected language for this session.
240 void stop_selected_language(void) {
241 #ifdef HAVE_USELOCALE
242 uselocale(LC_GLOBAL_LOCALE); /* switch locales */
243 textdomain(textdomain(NULL)); /* clear the cache */
249 * Create a locale_t for each available language
251 void initialize_locales(void) {
254 char *language = NULL;
257 setlocale(LC_ALL, "");
258 syslog(9, "Text domain: %s", textdomain("webcit"));
259 syslog(9, "Message catalog directory: %s", bindtextdomain(textdomain(NULL), LOCALEDIR"/locale"));
260 syslog(9, "Text domain Charset: %s", bind_textdomain_codeset("webcit","UTF8"));
264 while (!IsEmptyStr(AvailLang[nLocales]))
267 language = getenv("WEBCIT_LANG");
268 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
269 syslog(9, "Nailing locale to %s", language);
271 else language = NULL;
273 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
274 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
275 #ifdef HAVE_USELOCALE
276 wc_locales = malloc (sizeof(locale_t) * nLocales);
277 memset(wc_locales,0, sizeof(locale_t) * nLocales);
278 wc_locales[0] = newlocale(LC_ALL_MASK, NULL, NULL);
281 for (i = 1; i < nLocales; ++i) {
282 #ifdef HAVE_USELOCALE
283 wc_locales[nLocalesLoaded] = newlocale(
284 (LC_MESSAGES_MASK|LC_TIME_MASK),
288 if (wc_locales[nLocalesLoaded] == NULL) {
289 syslog(1, "locale for %s disabled: %s", AvailLang[i], strerror(errno));
292 syslog(3, "Found locale: %s", AvailLang[i]);
293 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
297 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
298 setenv("LANG", AvailLang[i], 1);
299 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
300 setlocale(LC_MESSAGES, AvailLang[i]);
303 else if (nLocalesLoaded == 0) {
304 setenv("LANG", AvailLang[i], 1);
305 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
310 if ((language != NULL) && (nLocalesLoaded == 0)) {
311 syslog(1, "Your selected locale [%s] isn't available on your system. falling back to C", language);
312 #ifndef HAVE_USELOCALE
313 setlocale(LC_MESSAGES, AvailLang[0]);
314 setenv("LANG", AvailLang[0], 1);
316 AvailLangLoaded[0] = AvailLang[0];
323 ServerShutdownModule_GETTEXT
326 #ifdef HAVE_USELOCALE
328 for (i = 0; i < nLocalesLoaded; ++i) {
329 freelocale(wc_locales[i]);
333 free(AvailLangLoaded);
336 #else /* ENABLE_NLS */
337 const char *AvailLang[] = {
342 /* dummy for non NLS enabled systems */
343 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
345 wc_printf("English (US)");
348 /* dummy for non NLS enabled systems */
349 void set_selected_language(const char *lang) {
352 /* dummy for non NLS enabled systems */
353 void go_selected_language(void) {
356 /* dummy for non NLS enabled systems */
357 void stop_selected_language(void) {
360 void initialize_locales(void) {
363 #endif /* ENABLE_NLS */
366 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
368 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
373 * Returns the language currently in use.
374 * This function returns a static string, so don't do anything stupid please.
376 const char *get_selected_language(void) {
378 #ifdef HAVE_USELOCALE
379 return AvailLang[WC->selected_language];
389 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
391 hdr->HR.browser_language = Line;
399 initialize_locales();
401 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
402 Header_HandleAcceptLanguage);
404 RegisterNamespace("LANG:SELECT", 0, 0,
405 tmplput_offer_languages, NULL, CTX_NONE);
410 SessionNewModule_GETTEXT
415 && (!sess->Hdr->HR.Static)
416 && (sess->Hdr->HR.browser_language != NULL)
418 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
424 SessionAttachModule_GETTEXT
428 go_selected_language(); /* set locale */
433 SessionDestroyModule_GETTEXT
437 stop_selected_language(); /* unset locale */