2 * Copyright (c) 1996-2011 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 as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "webserver.h"
21 #define SEARCH_LANG 20 /* how many langs should we parse? */
24 /* actual supported locales */
25 const char *AvailLang[] = {
52 const char **AvailLangLoaded;
53 long nLocalesLoaded = 0;
56 locale_t *wc_locales; /**< here we keep the parsed stuff */
59 /* Keep information about one locale */
60 typedef struct _lang_pref{
61 char lang[16]; /**< the language locale string */
62 char region[16]; /**< the region locale string */
63 long priority; /**< which priority does it have */
64 int availability; /**< do we know it? */
65 int selectedlang; /**< is this the selected language? */
68 /* parse browser locale header
70 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
72 * 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
74 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
75 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
76 * 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
79 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
81 LangStruct wanted_locales[SEARCH_LANG];
86 /* size_t len = strlen(LocaleString); */
94 nParts = StrBufNum_tokens(LocaleString, ',');
95 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
109 ls = &wanted_locales[i];
111 StrBufExtract_token(Buf, LocaleString, i, ',');
112 /** we are searching, if this list item has something like ;q=n*/
113 if (StrBufNum_tokens(Buf, '=') > 1) {
115 StrBufExtract_token(SBuf, Buf, 1, '=');
116 sbuflen = StrLength(SBuf);
117 for (k = 0; k < sbuflen; k++)
118 if (ChrPtr(SBuf)[k] == '.')
119 StrBufPeek(SBuf, NULL, k, '0');
120 ls->priority = StrTol(SBuf);
126 /** get the locale part */
127 StrBufExtract_token(SBuf, Buf, 0, ';');
129 /** get the lang part, which should be allways there */
130 extract_token(&ls->lang[0],
135 /** get the area code if any. */
136 if (StrBufNum_tokens(SBuf, '-') > 1) {
137 extract_token(&ls->region[0],
142 else { /** no ara code? use lang code */
143 blen=strlen(&ls->lang[0]);
144 memcpy(&ls->region[0], ls->lang, blen);
145 ls->region[blen] = '\0';
148 /* area codes are uppercase */
149 blen = strlen(&ls->region[0]);
150 for (j = 0; j < blen; j++)
153 chars = toupper(ls->region[j]);
154 ls->region[j] = (char)chars;/** \todo ?! */
162 /** check if we have this lang */
163 ls->availability = 1;
164 ls->selectedlang = -1;
165 for (j = 0; j < nLocalesLoaded; j++) {
167 /** match against the LANG part */
168 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
169 if ((result < 0) && (result < ls->availability)){
170 ls->availability = result;
171 ls->selectedlang = j;
173 /** match against lang and locale */
174 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
175 ls->availability = 0;
176 ls->selectedlang = j;
185 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
186 ls = &wanted_locales[i];
187 if ((ls->availability <= 0) &&
188 (av < ls->availability) &&
189 (prio < ls->priority) &&
190 (ls->selectedlang != -1)) {
191 nBest = ls->selectedlang;
192 av = ls->availability;
197 /** fall back to C */
200 sess->selected_language = nBest;
201 syslog(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
207 * show the language chooser on the login dialog
208 * depending on the browser locale change the sequence of the
211 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
214 #ifndef HAVE_USELOCALE
215 char *Lang = getenv("LANG");
222 if (nLocalesLoaded == 1) {
223 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
227 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\" onChange=\"switch_to_lang($('lname').value);\">\n");
229 for (i=0; i < nLocalesLoaded; ++i) {
230 #ifndef HAVE_USELOCALE
231 if (strcmp(AvailLangLoaded[i], Lang) == 0)
233 wc_printf("<option %s value=%s>%s</option>\n",
234 ((WC->selected_language == i) ? "selected" : ""),
240 wc_printf("</select>\n");
244 * Set the selected language for this session.
246 void set_selected_language(const char *lang) {
247 #ifdef HAVE_USELOCALE
249 for (i = 0; i<nLocalesLoaded; ++i) {
250 if (!strcasecmp(lang, AvailLangLoaded[i])) {
251 WC->selected_language = i;
259 * Activate the selected language for this session.
261 void go_selected_language(void) {
262 #ifdef HAVE_USELOCALE
264 if (WCC->selected_language < 0) return;
265 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
266 textdomain(textdomain(NULL)); /** clear the cache */
270 language = getenv("LANG");
271 setlocale(LC_MESSAGES, language);
276 * Deactivate the selected language for this session.
278 void stop_selected_language(void) {
279 #ifdef HAVE_USELOCALE
280 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
281 textdomain(textdomain(NULL)); /** clear the cache */
285 #ifdef HAVE_USELOCALE
286 locale_t Empty_Locale;
290 * Create a locale_t for each available language
292 void initialize_locales(void) {
296 char *language = NULL;
299 while (!IsEmptyStr(AvailLang[nLocales]))
302 language = getenv("WEBCIT_LANG");
303 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
304 syslog(9, "Nailing locale to %s\n", language);
306 else language = NULL;
308 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
309 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
310 #ifdef HAVE_USELOCALE
311 wc_locales = malloc (sizeof(locale_t) * nLocales);
312 memset(wc_locales,0, sizeof(locale_t) * nLocales);
313 /* create default locale */
314 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
320 for (i = 0; i < nLocales; ++i) {
321 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
324 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
327 sprintf(buf, "%s.UTF8", AvailLang[i]);
329 #ifdef HAVE_USELOCALE
330 wc_locales[nLocalesLoaded] = newlocale(
331 (LC_MESSAGES_MASK|LC_TIME_MASK),
333 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
335 if (wc_locales[nLocalesLoaded] == NULL) {
336 syslog(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
342 syslog(3, "Found locale: %s\n", buf);
343 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
347 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
348 setenv("LANG", buf, 1);
349 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
350 setlocale(LC_MESSAGES, AvailLang[i]);
353 else if (nLocalesLoaded == 0) {
354 setenv("LANG", buf, 1);
355 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
360 if ((language != NULL) && (nLocalesLoaded == 0)) {
361 syslog(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
362 #ifdef HAVE_USELOCALE
363 wc_locales[0] = newlocale(
364 (LC_MESSAGES_MASK|LC_TIME_MASK),
368 setlocale(LC_MESSAGES, AvailLang[0]);
369 setenv("LANG", AvailLang[0], 1);
371 AvailLangLoaded[0] = AvailLang[0];
376 /*locale = */setlocale(LC_ALL, "");
378 syslog(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
379 syslog(9, "Text domain: %s\n", textdomain("webcit"));
380 syslog(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
387 ServerShutdownModule_GETTEXT
390 #ifdef HAVE_USELOCALE
392 for (i = 0; i < nLocalesLoaded; ++i) {
393 if (Empty_Locale != wc_locales[i])
394 freelocale(wc_locales[i]);
398 free(AvailLangLoaded);
401 #else /* ENABLE_NLS */
402 const char *AvailLang[] = {
405 /* dummy for non NLS enabled systems */
406 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
408 wc_printf("English (US)");
411 /* dummy for non NLS enabled systems */
412 void set_selected_language(const char *lang) {
415 /* dummy for non NLS enabled systems */
416 void go_selected_language(void) {
419 /* dummy for non NLS enabled systems */
420 void stop_selected_language(void) {
423 void initialize_locales(void) {
426 #endif /* ENABLE_NLS */
429 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
431 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
436 * Returns the language currently in use.
437 * This function returns a static string, so don't do anything stupid please.
439 const char *get_selected_language(void) {
441 #ifdef HAVE_USELOCALE
442 return AvailLang[WC->selected_language];
452 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
454 hdr->HR.browser_language = Line;
462 initialize_locales();
464 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
465 Header_HandleAcceptLanguage);
467 RegisterNamespace("LANG:SELECT", 0, 0,
468 tmplput_offer_languages, NULL, CTX_NONE);
473 SessionNewModule_GETTEXT
478 && (!sess->Hdr->HR.Static)
479 && (sess->Hdr->HR.browser_language != NULL)
481 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
487 SessionAttachModule_GETTEXT
491 go_selected_language(); /* set locale */
496 SessionDestroyModule_GETTEXT
500 stop_selected_language(); /* unset locale */