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[] = {
46 const char **AvailLangLoaded;
47 long nLocalesLoaded = 0;
50 locale_t *wc_locales; /**< here we keep the parsed stuff */
53 /* Keep information about one locale */
54 typedef struct _lang_pref{
55 char lang[16]; /**< the language locale string */
56 char region[16]; /**< the region locale string */
57 long priority; /**< which priority does it have */
58 int availability; /**< do we know it? */
59 int selectedlang; /**< is this the selected language? */
62 /* parse browser locale header
64 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
66 * 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
68 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
69 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
70 * 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
73 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
75 LangStruct wanted_locales[SEARCH_LANG];
80 /* size_t len = strlen(LocaleString); */
88 nParts = StrBufNum_tokens(LocaleString, ',');
89 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
103 ls = &wanted_locales[i];
105 StrBufExtract_token(Buf, LocaleString, i, ',');
106 /** we are searching, if this list item has something like ;q=n*/
107 if (StrBufNum_tokens(Buf, '=') > 1) {
109 StrBufExtract_token(SBuf, Buf, 1, '=');
110 sbuflen = StrLength(SBuf);
111 for (k = 0; k < sbuflen; k++)
112 if (ChrPtr(SBuf)[k] == '.')
113 StrBufPeek(SBuf, NULL, k, '0');
114 ls->priority = StrTol(SBuf);
120 /** get the locale part */
121 StrBufExtract_token(SBuf, Buf, 0, ';');
123 /** get the lang part, which should be allways there */
124 extract_token(&ls->lang[0],
129 /** get the area code if any. */
130 if (StrBufNum_tokens(SBuf, '-') > 1) {
131 extract_token(&ls->region[0],
136 else { /** no ara code? use lang code */
137 blen=strlen(&ls->lang[0]);
138 memcpy(&ls->region[0], ls->lang, blen);
139 ls->region[blen] = '\0';
142 /* area codes are uppercase */
143 blen = strlen(&ls->region[0]);
144 for (j = 0; j < blen; j++)
147 chars = toupper(ls->region[j]);
148 ls->region[j] = (char)chars;/** \todo ?! */
156 /** check if we have this lang */
157 ls->availability = 1;
158 ls->selectedlang = -1;
159 for (j = 0; j < nLocalesLoaded; j++) {
161 /** match against the LANG part */
162 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
163 if ((result < 0) && (result < ls->availability)){
164 ls->availability = result;
165 ls->selectedlang = j;
167 /** match against lang and locale */
168 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
169 ls->availability = 0;
170 ls->selectedlang = j;
179 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
180 ls = &wanted_locales[i];
181 if ((ls->availability <= 0) &&
182 (av < ls->availability) &&
183 (prio < ls->priority) &&
184 (ls->selectedlang != -1)) {
185 nBest = ls->selectedlang;
186 av = ls->availability;
191 /** fall back to C */
194 sess->selected_language = nBest;
195 syslog(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
201 * show the language chooser on the login dialog
202 * depending on the browser locale change the sequence of the
205 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
208 #ifndef HAVE_USELOCALE
209 char *Lang = getenv("LANG");
216 if (nLocalesLoaded == 1) {
217 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
221 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\" onChange=\"switch_to_lang($('lname').value);\">\n");
223 for (i=0; i < nLocalesLoaded; ++i) {
224 #ifndef HAVE_USELOCALE
225 if (strcmp(AvailLangLoaded[i], Lang) == 0)
227 wc_printf("<option %s value=%s>%s</option>\n",
228 ((WC->selected_language == i) ? "selected" : ""),
234 wc_printf("</select>\n");
238 * Set the selected language for this session.
240 void set_selected_language(const char *lang) {
241 #ifdef HAVE_USELOCALE
243 for (i = 0; i<nLocalesLoaded; ++i) {
244 if (!strcasecmp(lang, AvailLangLoaded[i])) {
245 WC->selected_language = i;
252 * Activate the selected language for this session.
254 void go_selected_language(void) {
255 #ifdef HAVE_USELOCALE
257 if (WCC->selected_language < 0) return;
258 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
259 textdomain(textdomain(NULL)); /** clear the cache */
263 language = getenv("LANG");
264 setlocale(LC_MESSAGES, language);
269 * Deactivate the selected language for this session.
271 void stop_selected_language(void) {
272 #ifdef HAVE_USELOCALE
273 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
274 textdomain(textdomain(NULL)); /** clear the cache */
278 #ifdef HAVE_USELOCALE
279 locale_t Empty_Locale;
283 * Create a locale_t for each available language
285 void initialize_locales(void) {
289 char *language = NULL;
294 while (!IsEmptyStr(AvailLang[nLocales]))
297 language = getenv("WEBCIT_LANG");
298 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
299 syslog(9, "Nailing locale to %s\n", language);
301 else language = NULL;
303 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
304 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
305 #ifdef HAVE_USELOCALE
306 wc_locales = malloc (sizeof(locale_t) * nLocales);
307 memset(wc_locales,0, sizeof(locale_t) * nLocales);
308 /* create default locale */
309 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
315 for (i = 0; i < nLocales; ++i) {
316 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
319 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
322 sprintf(buf, "%s.UTF8", AvailLang[i]);
324 #ifdef HAVE_USELOCALE
325 wc_locales[nLocalesLoaded] = newlocale(
326 (LC_MESSAGES_MASK|LC_TIME_MASK),
328 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
330 if (wc_locales[nLocalesLoaded] == NULL) {
331 syslog(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
337 syslog(3, "Found locale: %s\n", buf);
338 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
342 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
343 setenv("LANG", buf, 1);
344 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
345 setlocale(LC_MESSAGES, AvailLang[i]);
348 else if (nLocalesLoaded == 0) {
349 setenv("LANG", buf, 1);
350 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
355 if ((language != NULL) && (nLocalesLoaded == 0)) {
356 syslog(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
357 #ifdef HAVE_USELOCALE
358 wc_locales[0] = newlocale(
359 (LC_MESSAGES_MASK|LC_TIME_MASK),
363 setlocale(LC_MESSAGES, AvailLang[0]);
364 setenv("LANG", AvailLang[0], 1);
366 AvailLangLoaded[0] = AvailLang[0];
371 locale = setlocale(LC_ALL, "");
373 syslog(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
374 syslog(9, "Text domain: %s\n", textdomain("webcit"));
375 syslog(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
382 ServerShutdownModule_GETTEXT
385 #ifdef HAVE_USELOCALE
387 for (i = 0; i < nLocalesLoaded; ++i) {
388 if (Empty_Locale != wc_locales[i])
389 freelocale(wc_locales[i]);
393 free(AvailLangLoaded);
396 #else /* ENABLE_NLS */
397 const char *AvailLang[] = {
400 /* dummy for non NLS enabled systems */
401 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
403 wc_printf("English (US)");
406 /* dummy for non NLS enabled systems */
407 void set_selected_language(const char *lang) {
410 /* dummy for non NLS enabled systems */
411 void go_selected_language(void) {
414 /* dummy for non NLS enabled systems */
415 void stop_selected_language(void) {
418 void initialize_locales(void) {
421 #endif /* ENABLE_NLS */
424 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
426 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
431 * Returns the language currently in use.
432 * This function returns a static string, so don't do anything stupid please.
434 const char *get_selected_language(void) {
436 #ifdef HAVE_USELOCALE
437 return AvailLang[WC->selected_language];
447 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
449 hdr->HR.browser_language = Line;
457 initialize_locales();
459 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
460 Header_HandleAcceptLanguage);
462 RegisterNamespace("LANG:SELECT", 0, 0,
463 tmplput_offer_languages, NULL, CTX_NONE);
468 SessionNewModule_GETTEXT
472 if (!sess->Hdr->HR.Static &&
473 (sess->Hdr->HR.browser_language != NULL)) {
474 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
480 SessionAttachModule_GETTEXT
484 go_selected_language(); /* set locale */
489 SessionDestroyModule_GETTEXT
493 stop_selected_language(); /* unset locale */