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[] = {
47 const char **AvailLangLoaded;
48 long nLocalesLoaded = 0;
51 locale_t *wc_locales; /**< here we keep the parsed stuff */
54 /* Keep information about one locale */
55 typedef struct _lang_pref{
56 char lang[16]; /**< the language locale string */
57 char region[16]; /**< the region locale string */
58 long priority; /**< which priority does it have */
59 int availability; /**< do we know it? */
60 int selectedlang; /**< is this the selected language? */
63 /* parse browser locale header
65 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
67 * 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
69 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
70 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
71 * 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
74 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
76 LangStruct wanted_locales[SEARCH_LANG];
81 /* size_t len = strlen(LocaleString); */
89 nParts = StrBufNum_tokens(LocaleString, ',');
90 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
104 ls = &wanted_locales[i];
106 StrBufExtract_token(Buf, LocaleString, i, ',');
107 /** we are searching, if this list item has something like ;q=n*/
108 if (StrBufNum_tokens(Buf, '=') > 1) {
110 StrBufExtract_token(SBuf, Buf, 1, '=');
111 sbuflen = StrLength(SBuf);
112 for (k = 0; k < sbuflen; k++)
113 if (ChrPtr(SBuf)[k] == '.')
114 StrBufPeek(SBuf, NULL, k, '0');
115 ls->priority = StrTol(SBuf);
121 /** get the locale part */
122 StrBufExtract_token(SBuf, Buf, 0, ';');
124 /** get the lang part, which should be allways there */
125 extract_token(&ls->lang[0],
130 /** get the area code if any. */
131 if (StrBufNum_tokens(SBuf, '-') > 1) {
132 extract_token(&ls->region[0],
137 else { /** no ara code? use lang code */
138 blen=strlen(&ls->lang[0]);
139 memcpy(&ls->region[0], ls->lang, blen);
140 ls->region[blen] = '\0';
143 /* area codes are uppercase */
144 blen = strlen(&ls->region[0]);
145 for (j = 0; j < blen; j++)
148 chars = toupper(ls->region[j]);
149 ls->region[j] = (char)chars;/** \todo ?! */
157 /** check if we have this lang */
158 ls->availability = 1;
159 ls->selectedlang = -1;
160 for (j = 0; j < nLocalesLoaded; j++) {
162 /** match against the LANG part */
163 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
164 if ((result < 0) && (result < ls->availability)){
165 ls->availability = result;
166 ls->selectedlang = j;
168 /** match against lang and locale */
169 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
170 ls->availability = 0;
171 ls->selectedlang = j;
180 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
181 ls = &wanted_locales[i];
182 if ((ls->availability <= 0) &&
183 (av < ls->availability) &&
184 (prio < ls->priority) &&
185 (ls->selectedlang != -1)) {
186 nBest = ls->selectedlang;
187 av = ls->availability;
192 /** fall back to C */
195 sess->selected_language = nBest;
196 syslog(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
202 * show the language chooser on the login dialog
203 * depending on the browser locale change the sequence of the
206 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
209 #ifndef HAVE_USELOCALE
210 char *Lang = getenv("LANG");
217 if (nLocalesLoaded == 1) {
218 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
222 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\" onChange=\"switch_to_lang($('lname').value);\">\n");
224 for (i=0; i < nLocalesLoaded; ++i) {
225 #ifndef HAVE_USELOCALE
226 if (strcmp(AvailLangLoaded[i], Lang) == 0)
228 wc_printf("<option %s value=%s>%s</option>\n",
229 ((WC->selected_language == i) ? "selected" : ""),
235 wc_printf("</select>\n");
239 * Set the selected language for this session.
241 void set_selected_language(const char *lang) {
242 #ifdef HAVE_USELOCALE
244 for (i = 0; i<nLocalesLoaded; ++i) {
245 if (!strcasecmp(lang, AvailLangLoaded[i])) {
246 WC->selected_language = i;
253 * Activate the selected language for this session.
255 void go_selected_language(void) {
256 #ifdef HAVE_USELOCALE
258 if (WCC->selected_language < 0) return;
259 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
260 textdomain(textdomain(NULL)); /** clear the cache */
264 language = getenv("LANG");
265 setlocale(LC_MESSAGES, language);
270 * Deactivate the selected language for this session.
272 void stop_selected_language(void) {
273 #ifdef HAVE_USELOCALE
274 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
275 textdomain(textdomain(NULL)); /** clear the cache */
279 #ifdef HAVE_USELOCALE
280 locale_t Empty_Locale;
284 * Create a locale_t for each available language
286 void initialize_locales(void) {
290 char *language = NULL;
293 while (!IsEmptyStr(AvailLang[nLocales]))
296 language = getenv("WEBCIT_LANG");
297 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
298 syslog(9, "Nailing locale to %s\n", language);
300 else language = NULL;
302 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
303 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
304 #ifdef HAVE_USELOCALE
305 wc_locales = malloc (sizeof(locale_t) * nLocales);
306 memset(wc_locales,0, sizeof(locale_t) * nLocales);
307 /* create default locale */
308 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
314 for (i = 0; i < nLocales; ++i) {
315 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
318 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
321 sprintf(buf, "%s.UTF8", AvailLang[i]);
323 #ifdef HAVE_USELOCALE
324 wc_locales[nLocalesLoaded] = newlocale(
325 (LC_MESSAGES_MASK|LC_TIME_MASK),
327 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
329 if (wc_locales[nLocalesLoaded] == NULL) {
330 syslog(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
336 syslog(3, "Found locale: %s\n", buf);
337 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
341 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
342 setenv("LANG", buf, 1);
343 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
344 setlocale(LC_MESSAGES, AvailLang[i]);
347 else if (nLocalesLoaded == 0) {
348 setenv("LANG", buf, 1);
349 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
354 if ((language != NULL) && (nLocalesLoaded == 0)) {
355 syslog(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
356 #ifdef HAVE_USELOCALE
357 wc_locales[0] = newlocale(
358 (LC_MESSAGES_MASK|LC_TIME_MASK),
362 setlocale(LC_MESSAGES, AvailLang[0]);
363 setenv("LANG", AvailLang[0], 1);
365 AvailLangLoaded[0] = AvailLang[0];
370 /*locale = */setlocale(LC_ALL, "");
372 syslog(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
373 syslog(9, "Text domain: %s\n", textdomain("webcit"));
374 syslog(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
381 ServerShutdownModule_GETTEXT
384 #ifdef HAVE_USELOCALE
386 for (i = 0; i < nLocalesLoaded; ++i) {
387 if (Empty_Locale != wc_locales[i])
388 freelocale(wc_locales[i]);
392 free(AvailLangLoaded);
395 #else /* ENABLE_NLS */
396 const char *AvailLang[] = {
399 /* dummy for non NLS enabled systems */
400 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
402 wc_printf("English (US)");
405 /* dummy for non NLS enabled systems */
406 void set_selected_language(const char *lang) {
409 /* dummy for non NLS enabled systems */
410 void go_selected_language(void) {
413 /* dummy for non NLS enabled systems */
414 void stop_selected_language(void) {
417 void initialize_locales(void) {
420 #endif /* ENABLE_NLS */
423 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
425 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
430 * Returns the language currently in use.
431 * This function returns a static string, so don't do anything stupid please.
433 const char *get_selected_language(void) {
435 #ifdef HAVE_USELOCALE
436 return AvailLang[WC->selected_language];
446 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
448 hdr->HR.browser_language = Line;
456 initialize_locales();
458 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
459 Header_HandleAcceptLanguage);
461 RegisterNamespace("LANG:SELECT", 0, 0,
462 tmplput_offer_languages, NULL, CTX_NONE);
467 SessionNewModule_GETTEXT
472 && (!sess->Hdr->HR.Static)
473 && (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 */