4 * Copyright (c) 1996-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "webserver.h"
23 #define SEARCH_LANG 20 /* how many langs should we parse? */
26 /* actual supported locales */
27 const char *AvailLang[] = {
44 const char **AvailLangLoaded;
45 long nLocalesLoaded = 0;
48 locale_t *wc_locales; /**< here we keep the parsed stuff */
51 /* Keep information about one locale */
52 typedef struct _lang_pref{
53 char lang[16]; /**< the language locale string */
54 char region[16]; /**< the region locale string */
55 long priority; /**< which priority does it have */
56 int availability; /**< do we know it? */
57 int selectedlang; /**< is this the selected language? */
60 /* parse browser locale header
62 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
64 * 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
66 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
67 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
68 * 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
71 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
73 LangStruct wanted_locales[SEARCH_LANG];
78 /* size_t len = strlen(LocaleString); */
86 nParts = StrBufNum_tokens(LocaleString, ',');
87 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
101 ls = &wanted_locales[i];
103 StrBufExtract_token(Buf, LocaleString, i, ',');
104 /** we are searching, if this list item has something like ;q=n*/
105 if (StrBufNum_tokens(Buf, '=') > 1) {
107 StrBufExtract_token(SBuf, Buf, 1, '=');
108 sbuflen = StrLength(SBuf);
109 for (k = 0; k < sbuflen; k++)
110 if (ChrPtr(SBuf)[k] == '.')
111 StrBufPeek(SBuf, NULL, k, '0');
112 ls->priority = StrTol(SBuf);
118 /** get the locale part */
119 StrBufExtract_token(SBuf, Buf, 0, ';');
121 /** get the lang part, which should be allways there */
122 extract_token(&ls->lang[0],
127 /** get the area code if any. */
128 if (StrBufNum_tokens(SBuf, '-') > 1) {
129 extract_token(&ls->region[0],
134 else { /** no ara code? use lang code */
135 blen=strlen(&ls->lang[0]);
136 memcpy(&ls->region[0], ls->lang, blen);
137 ls->region[blen] = '\0';
140 /* area codes are uppercase */
141 blen = strlen(&ls->region[0]);
142 for (j = 0; j < blen; j++)
145 chars = toupper(ls->region[j]);
146 ls->region[j] = (char)chars;/** \todo ?! */
154 /** check if we have this lang */
155 ls->availability = 1;
156 ls->selectedlang = -1;
157 for (j = 0; j < nLocalesLoaded; j++) {
159 /** match against the LANG part */
160 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
161 if ((result < 0) && (result < ls->availability)){
162 ls->availability = result;
163 ls->selectedlang = j;
165 /** match against lang and locale */
166 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
167 ls->availability = 0;
168 ls->selectedlang = j;
177 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
178 ls = &wanted_locales[i];
179 if ((ls->availability <= 0) &&
180 (av < ls->availability) &&
181 (prio < ls->priority) &&
182 (ls->selectedlang != -1)) {
183 nBest = ls->selectedlang;
184 av = ls->availability;
189 /** fall back to C */
192 sess->selected_language = nBest;
193 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
199 * show the language chooser on the login dialog
200 * depending on the browser locale change the sequence of the
203 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
206 #ifndef HAVE_USELOCALE
207 char *Lang = getenv("LANG");
214 if (nLocalesLoaded == 1) {
215 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
219 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
221 for (i=0; i < nLocalesLoaded; ++i) {
222 #ifndef HAVE_USELOCALE
223 if (strcmp(AvailLangLoaded[i], Lang) == 0)
225 wc_printf("<option %s value=%s>%s</option>\n",
226 ((WC->selected_language == i) ? "selected" : ""),
232 wc_printf("</select>\n");
236 * Set the selected language for this session.
238 void set_selected_language(const char *lang) {
239 #ifdef HAVE_USELOCALE
241 for (i = 0; i<nLocalesLoaded; ++i) {
242 if (!strcasecmp(lang, AvailLangLoaded[i])) {
243 WC->selected_language = i;
250 * Activate the selected language for this session.
252 void go_selected_language(void) {
253 #ifdef HAVE_USELOCALE
255 if (WCC->selected_language < 0) return;
256 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
257 textdomain(textdomain(NULL)); /** clear the cache */
261 language = getenv("LANG");
262 setlocale(LC_MESSAGES, language);
267 * Deactivate the selected language for this session.
269 void stop_selected_language(void) {
270 #ifdef HAVE_USELOCALE
271 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
272 textdomain(textdomain(NULL)); /** clear the cache */
276 #ifdef HAVE_USELOCALE
277 locale_t Empty_Locale;
281 * Create a locale_t for each available language
283 void initialize_locales(void) {
287 char *language = NULL;
292 while (!IsEmptyStr(AvailLang[nLocales]))
295 language = getenv("WEBCIT_LANG");
296 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
297 lprintf(9, "Nailing locale to %s\n", language);
299 else language = NULL;
301 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
302 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
303 #ifdef HAVE_USELOCALE
304 wc_locales = malloc (sizeof(locale_t) * nLocales);
305 memset(wc_locales,0, sizeof(locale_t) * nLocales);
306 /* create default locale */
307 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
313 for (i = 0; i < nLocales; ++i) {
314 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
317 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
320 sprintf(buf, "%s.UTF8", AvailLang[i]);
322 #ifdef HAVE_USELOCALE
323 wc_locales[nLocalesLoaded] = newlocale(
324 (LC_MESSAGES_MASK|LC_TIME_MASK),
326 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
328 if (wc_locales[nLocalesLoaded] == NULL) {
329 lprintf(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
335 lprintf(3, "Found locale: %s\n", buf);
336 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
340 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
341 setenv("LANG", buf, 1);
342 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
343 setlocale(LC_MESSAGES, AvailLang[i]);
346 else if (nLocalesLoaded == 0) {
347 setenv("LANG", buf, 1);
348 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
353 if ((language != NULL) && (nLocalesLoaded == 0)) {
354 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
355 #ifdef HAVE_USELOCALE
356 wc_locales[0] = newlocale(
357 (LC_MESSAGES_MASK|LC_TIME_MASK),
361 setlocale(LC_MESSAGES, AvailLang[0]);
362 setenv("LANG", AvailLang[0], 1);
364 AvailLangLoaded[0] = AvailLang[0];
367 #ifndef HAVE_USELOCALE
373 locale = setlocale(LC_ALL, "");
375 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
376 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
377 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
384 ServerShutdownModule_GETTEXT
387 #ifdef HAVE_USELOCALE
389 for (i = 0; i < nLocalesLoaded; ++i) {
390 if (Empty_Locale != wc_locales[i])
391 freelocale(wc_locales[i]);
395 free(AvailLangLoaded);
398 #else /* ENABLE_NLS */
399 const char *AvailLang[] = {
402 /* dummy for non NLS enabled systems */
403 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
405 wc_printf("English (US)");
408 /* dummy for non NLS enabled systems */
409 void set_selected_language(const char *lang) {
412 /* dummy for non NLS enabled systems */
413 void go_selected_language(void) {
416 /* dummy for non NLS enabled systems */
417 void stop_selected_language(void) {
420 void initialize_locales(void) {
423 #endif /* ENABLE_NLS */
426 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
428 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
433 * Returns the language currently in use.
434 * This function returns a static string, so don't do anything stupid please.
436 const char *get_selected_language(void) {
438 #ifdef HAVE_USELOCALE
439 return AvailLang[WC->selected_language];
449 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
451 hdr->HR.browser_language = Line;
458 initialize_locales();
460 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
461 Header_HandleAcceptLanguage);
463 RegisterNamespace("LANG:SELECT", 0, 0,
464 tmplput_offer_languages, NULL, CTX_NONE);
469 SessionNewModule_GETTEXT
473 if (!sess->Hdr->HR.Static &&
474 (sess->Hdr->HR.browser_language != NULL)) {
475 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
481 SessionAttachModule_GETTEXT
485 go_selected_language(); /* set locale */
490 SessionDestroyModule_GETTEXT
494 stop_selected_language(); /* unset locale */