2 * Copyright (c) 1996-2010 by the citadel.org team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (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[] = {
42 const char **AvailLangLoaded;
43 long nLocalesLoaded = 0;
46 locale_t *wc_locales; /**< here we keep the parsed stuff */
49 /* Keep information about one locale */
50 typedef struct _lang_pref{
51 char lang[16]; /**< the language locale string */
52 char region[16]; /**< the region locale string */
53 long priority; /**< which priority does it have */
54 int availability; /**< do we know it? */
55 int selectedlang; /**< is this the selected language? */
58 /* parse browser locale header
60 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
62 * 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
64 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
65 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
66 * 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
69 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
71 LangStruct wanted_locales[SEARCH_LANG];
76 /* size_t len = strlen(LocaleString); */
84 nParts = StrBufNum_tokens(LocaleString, ',');
85 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
99 ls = &wanted_locales[i];
101 StrBufExtract_token(Buf, LocaleString, i, ',');
102 /** we are searching, if this list item has something like ;q=n*/
103 if (StrBufNum_tokens(Buf, '=') > 1) {
105 StrBufExtract_token(SBuf, Buf, 1, '=');
106 sbuflen = StrLength(SBuf);
107 for (k = 0; k < sbuflen; k++)
108 if (ChrPtr(SBuf)[k] == '.')
109 StrBufPeek(SBuf, NULL, k, '0');
110 ls->priority = StrTol(SBuf);
116 /** get the locale part */
117 StrBufExtract_token(SBuf, Buf, 0, ';');
119 /** get the lang part, which should be allways there */
120 extract_token(&ls->lang[0],
125 /** get the area code if any. */
126 if (StrBufNum_tokens(SBuf, '-') > 1) {
127 extract_token(&ls->region[0],
132 else { /** no ara code? use lang code */
133 blen=strlen(&ls->lang[0]);
134 memcpy(&ls->region[0], ls->lang, blen);
135 ls->region[blen] = '\0';
138 /* area codes are uppercase */
139 blen = strlen(&ls->region[0]);
140 for (j = 0; j < blen; j++)
143 chars = toupper(ls->region[j]);
144 ls->region[j] = (char)chars;/** \todo ?! */
152 /** check if we have this lang */
153 ls->availability = 1;
154 ls->selectedlang = -1;
155 for (j = 0; j < nLocalesLoaded; j++) {
157 /** match against the LANG part */
158 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
159 if ((result < 0) && (result < ls->availability)){
160 ls->availability = result;
161 ls->selectedlang = j;
163 /** match against lang and locale */
164 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
165 ls->availability = 0;
166 ls->selectedlang = j;
175 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
176 ls = &wanted_locales[i];
177 if ((ls->availability <= 0) &&
178 (av < ls->availability) &&
179 (prio < ls->priority) &&
180 (ls->selectedlang != -1)) {
181 nBest = ls->selectedlang;
182 av = ls->availability;
187 /** fall back to C */
190 sess->selected_language = nBest;
191 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
197 * show the language chooser on the login dialog
198 * depending on the browser locale change the sequence of the
201 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
204 #ifndef HAVE_USELOCALE
205 char *Lang = getenv("LANG");
212 if (nLocalesLoaded == 1) {
213 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
217 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
219 for (i=0; i < nLocalesLoaded; ++i) {
220 #ifndef HAVE_USELOCALE
221 if (strcmp(AvailLangLoaded[i], Lang) == 0)
223 wc_printf("<option %s value=%s>%s</option>\n",
224 ((WC->selected_language == i) ? "selected" : ""),
230 wc_printf("</select>\n");
234 * Set the selected language for this session.
236 void set_selected_language(const char *lang) {
237 #ifdef HAVE_USELOCALE
239 for (i = 0; i<nLocalesLoaded; ++i) {
240 if (!strcasecmp(lang, AvailLangLoaded[i])) {
241 WC->selected_language = i;
248 * Activate the selected language for this session.
250 void go_selected_language(void) {
251 #ifdef HAVE_USELOCALE
253 if (WCC->selected_language < 0) return;
254 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
255 textdomain(textdomain(NULL)); /** clear the cache */
259 language = getenv("LANG");
260 setlocale(LC_MESSAGES, language);
265 * Deactivate the selected language for this session.
267 void stop_selected_language(void) {
268 #ifdef HAVE_USELOCALE
269 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
270 textdomain(textdomain(NULL)); /** clear the cache */
274 #ifdef HAVE_USELOCALE
275 locale_t Empty_Locale;
279 * Create a locale_t for each available language
281 void initialize_locales(void) {
285 char *language = NULL;
290 while (!IsEmptyStr(AvailLang[nLocales]))
293 language = getenv("WEBCIT_LANG");
294 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
295 lprintf(9, "Nailing locale to %s\n", language);
297 else language = NULL;
299 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
300 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
301 #ifdef HAVE_USELOCALE
302 wc_locales = malloc (sizeof(locale_t) * nLocales);
303 memset(wc_locales,0, sizeof(locale_t) * nLocales);
304 /* create default locale */
305 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
311 for (i = 0; i < nLocales; ++i) {
312 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
315 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
318 sprintf(buf, "%s.UTF8", AvailLang[i]);
320 #ifdef HAVE_USELOCALE
321 wc_locales[nLocalesLoaded] = newlocale(
322 (LC_MESSAGES_MASK|LC_TIME_MASK),
324 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
326 if (wc_locales[nLocalesLoaded] == NULL) {
327 lprintf(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
333 lprintf(3, "Found locale: %s\n", 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 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
353 #ifdef HAVE_USELOCALE
354 wc_locales[0] = newlocale(
355 (LC_MESSAGES_MASK|LC_TIME_MASK),
359 setlocale(LC_MESSAGES, AvailLang[0]);
360 setenv("LANG", AvailLang[0], 1);
362 AvailLangLoaded[0] = AvailLang[0];
367 locale = setlocale(LC_ALL, "");
369 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
370 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
371 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
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]);
389 free(AvailLangLoaded);
392 #else /* ENABLE_NLS */
393 const char *AvailLang[] = {
396 /* dummy for non NLS enabled systems */
397 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
399 wc_printf("English (US)");
402 /* dummy for non NLS enabled systems */
403 void set_selected_language(const char *lang) {
406 /* dummy for non NLS enabled systems */
407 void go_selected_language(void) {
410 /* dummy for non NLS enabled systems */
411 void stop_selected_language(void) {
414 void initialize_locales(void) {
417 #endif /* ENABLE_NLS */
420 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
422 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
427 * Returns the language currently in use.
428 * This function returns a static string, so don't do anything stupid please.
430 const char *get_selected_language(void) {
432 #ifdef HAVE_USELOCALE
433 return AvailLang[WC->selected_language];
443 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
445 hdr->HR.browser_language = Line;
452 initialize_locales();
454 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
455 Header_HandleAcceptLanguage);
457 RegisterNamespace("LANG:SELECT", 0, 0,
458 tmplput_offer_languages, NULL, CTX_NONE);
463 SessionNewModule_GETTEXT
467 if (!sess->Hdr->HR.Static &&
468 (sess->Hdr->HR.browser_language != NULL)) {
469 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
475 SessionAttachModule_GETTEXT
479 go_selected_language(); /* set locale */
484 SessionDestroyModule_GETTEXT
488 stop_selected_language(); /* unset locale */