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[] = {
43 const char **AvailLangLoaded;
44 long nLocalesLoaded = 0;
47 locale_t *wc_locales; /**< here we keep the parsed stuff */
50 /* Keep information about one locale */
51 typedef struct _lang_pref{
52 char lang[16]; /**< the language locale string */
53 char region[16]; /**< the region locale string */
54 long priority; /**< which priority does it have */
55 int availability; /**< do we know it? */
56 int selectedlang; /**< is this the selected language? */
59 /* parse browser locale header
61 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
63 * 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
65 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
66 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
67 * 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
70 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
72 LangStruct wanted_locales[SEARCH_LANG];
77 /* size_t len = strlen(LocaleString); */
85 nParts = StrBufNum_tokens(LocaleString, ',');
86 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
100 ls = &wanted_locales[i];
102 StrBufExtract_token(Buf, LocaleString, i, ',');
103 /** we are searching, if this list item has something like ;q=n*/
104 if (StrBufNum_tokens(Buf, '=') > 1) {
106 StrBufExtract_token(SBuf, Buf, 1, '=');
107 sbuflen = StrLength(SBuf);
108 for (k = 0; k < sbuflen; k++)
109 if (ChrPtr(SBuf)[k] == '.')
110 StrBufPeek(SBuf, NULL, k, '0');
111 ls->priority = StrTol(SBuf);
117 /** get the locale part */
118 StrBufExtract_token(SBuf, Buf, 0, ';');
120 /** get the lang part, which should be allways there */
121 extract_token(&ls->lang[0],
126 /** get the area code if any. */
127 if (StrBufNum_tokens(SBuf, '-') > 1) {
128 extract_token(&ls->region[0],
133 else { /** no ara code? use lang code */
134 blen=strlen(&ls->lang[0]);
135 memcpy(&ls->region[0], ls->lang, blen);
136 ls->region[blen] = '\0';
139 /* area codes are uppercase */
140 blen = strlen(&ls->region[0]);
141 for (j = 0; j < blen; j++)
144 chars = toupper(ls->region[j]);
145 ls->region[j] = (char)chars;/** \todo ?! */
153 /** check if we have this lang */
154 ls->availability = 1;
155 ls->selectedlang = -1;
156 for (j = 0; j < nLocalesLoaded; j++) {
158 /** match against the LANG part */
159 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
160 if ((result < 0) && (result < ls->availability)){
161 ls->availability = result;
162 ls->selectedlang = j;
164 /** match against lang and locale */
165 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
166 ls->availability = 0;
167 ls->selectedlang = j;
176 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
177 ls = &wanted_locales[i];
178 if ((ls->availability <= 0) &&
179 (av < ls->availability) &&
180 (prio < ls->priority) &&
181 (ls->selectedlang != -1)) {
182 nBest = ls->selectedlang;
183 av = ls->availability;
188 /** fall back to C */
191 sess->selected_language = nBest;
192 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
198 * show the language chooser on the login dialog
199 * depending on the browser locale change the sequence of the
202 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
205 #ifndef HAVE_USELOCALE
206 char *Lang = getenv("LANG");
213 if (nLocalesLoaded == 1) {
214 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
218 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
220 for (i=0; i < nLocalesLoaded; ++i) {
221 #ifndef HAVE_USELOCALE
222 if (strcmp(AvailLangLoaded[i], Lang) == 0)
224 wc_printf("<option %s value=%s>%s</option>\n",
225 ((WC->selected_language == i) ? "selected" : ""),
231 wc_printf("</select>\n");
235 * Set the selected language for this session.
237 void set_selected_language(const char *lang) {
238 #ifdef HAVE_USELOCALE
240 for (i = 0; i<nLocalesLoaded; ++i) {
241 if (!strcasecmp(lang, AvailLangLoaded[i])) {
242 WC->selected_language = i;
249 * Activate the selected language for this session.
251 void go_selected_language(void) {
252 #ifdef HAVE_USELOCALE
254 if (WCC->selected_language < 0) return;
255 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
256 textdomain(textdomain(NULL)); /** clear the cache */
260 language = getenv("LANG");
261 setlocale(LC_MESSAGES, language);
266 * Deactivate the selected language for this session.
268 void stop_selected_language(void) {
269 #ifdef HAVE_USELOCALE
270 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
271 textdomain(textdomain(NULL)); /** clear the cache */
275 #ifdef HAVE_USELOCALE
276 locale_t Empty_Locale;
280 * Create a locale_t for each available language
282 void initialize_locales(void) {
286 char *language = NULL;
291 while (!IsEmptyStr(AvailLang[nLocales]))
294 language = getenv("WEBCIT_LANG");
295 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
296 lprintf(9, "Nailing locale to %s\n", language);
298 else language = NULL;
300 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
301 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
302 #ifdef HAVE_USELOCALE
303 wc_locales = malloc (sizeof(locale_t) * nLocales);
304 memset(wc_locales,0, sizeof(locale_t) * nLocales);
305 /* create default locale */
306 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
312 for (i = 0; i < nLocales; ++i) {
313 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
316 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
319 sprintf(buf, "%s.UTF8", AvailLang[i]);
321 #ifdef HAVE_USELOCALE
322 wc_locales[nLocalesLoaded] = newlocale(
323 (LC_MESSAGES_MASK|LC_TIME_MASK),
325 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
327 if (wc_locales[nLocalesLoaded] == NULL) {
328 lprintf(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
334 lprintf(3, "Found locale: %s\n", buf);
335 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
339 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
340 setenv("LANG", buf, 1);
341 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
342 setlocale(LC_MESSAGES, AvailLang[i]);
345 else if (nLocalesLoaded == 0) {
346 setenv("LANG", buf, 1);
347 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
352 if ((language != NULL) && (nLocalesLoaded == 0)) {
353 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
354 #ifdef HAVE_USELOCALE
355 wc_locales[0] = newlocale(
356 (LC_MESSAGES_MASK|LC_TIME_MASK),
360 setlocale(LC_MESSAGES, AvailLang[0]);
361 setenv("LANG", AvailLang[0], 1);
363 AvailLangLoaded[0] = AvailLang[0];
368 locale = setlocale(LC_ALL, "");
370 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
371 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
372 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
379 ServerShutdownModule_GETTEXT
382 #ifdef HAVE_USELOCALE
384 for (i = 0; i < nLocalesLoaded; ++i) {
385 if (Empty_Locale != wc_locales[i])
386 freelocale(wc_locales[i]);
390 free(AvailLangLoaded);
393 #else /* ENABLE_NLS */
394 const char *AvailLang[] = {
397 /* dummy for non NLS enabled systems */
398 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
400 wc_printf("English (US)");
403 /* dummy for non NLS enabled systems */
404 void set_selected_language(const char *lang) {
407 /* dummy for non NLS enabled systems */
408 void go_selected_language(void) {
411 /* dummy for non NLS enabled systems */
412 void stop_selected_language(void) {
415 void initialize_locales(void) {
418 #endif /* ENABLE_NLS */
421 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
423 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
428 * Returns the language currently in use.
429 * This function returns a static string, so don't do anything stupid please.
431 const char *get_selected_language(void) {
433 #ifdef HAVE_USELOCALE
434 return AvailLang[WC->selected_language];
444 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
446 hdr->HR.browser_language = Line;
453 initialize_locales();
455 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
456 Header_HandleAcceptLanguage);
458 RegisterNamespace("LANG:SELECT", 0, 0,
459 tmplput_offer_languages, NULL, CTX_NONE);
464 SessionNewModule_GETTEXT
468 if (!sess->Hdr->HR.Static &&
469 (sess->Hdr->HR.browser_language != NULL)) {
470 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
476 SessionAttachModule_GETTEXT
480 go_selected_language(); /* set locale */
485 SessionDestroyModule_GETTEXT
489 stop_selected_language(); /* unset locale */