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[] = {
51 const char **AvailLangLoaded;
52 long nLocalesLoaded = 0;
55 locale_t *wc_locales; /**< here we keep the parsed stuff */
58 /* Keep information about one locale */
59 typedef struct _lang_pref{
60 char lang[16]; /**< the language locale string */
61 char region[16]; /**< the region locale string */
62 long priority; /**< which priority does it have */
63 int availability; /**< do we know it? */
64 int selectedlang; /**< is this the selected language? */
67 /* parse browser locale header
69 * seems as most browsers just do a one after comma value even if more than 10 locales are available. Sample strings:
71 * 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
73 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
74 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
75 * 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
78 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
80 LangStruct wanted_locales[SEARCH_LANG];
85 /* size_t len = strlen(LocaleString); */
93 nParts = StrBufNum_tokens(LocaleString, ',');
94 for (i=0; ((i<nParts) && (i < SEARCH_LANG)); i++)
108 ls = &wanted_locales[i];
110 StrBufExtract_token(Buf, LocaleString, i, ',');
111 /** we are searching, if this list item has something like ;q=n*/
112 if (StrBufNum_tokens(Buf, '=') > 1) {
114 StrBufExtract_token(SBuf, Buf, 1, '=');
115 sbuflen = StrLength(SBuf);
116 for (k = 0; k < sbuflen; k++)
117 if (ChrPtr(SBuf)[k] == '.')
118 StrBufPeek(SBuf, NULL, k, '0');
119 ls->priority = StrTol(SBuf);
125 /** get the locale part */
126 StrBufExtract_token(SBuf, Buf, 0, ';');
128 /** get the lang part, which should be allways there */
129 extract_token(&ls->lang[0],
134 /** get the area code if any. */
135 if (StrBufNum_tokens(SBuf, '-') > 1) {
136 extract_token(&ls->region[0],
141 else { /** no ara code? use lang code */
142 blen=strlen(&ls->lang[0]);
143 memcpy(&ls->region[0], ls->lang, blen);
144 ls->region[blen] = '\0';
147 /* area codes are uppercase */
148 blen = strlen(&ls->region[0]);
149 for (j = 0; j < blen; j++)
152 chars = toupper(ls->region[j]);
153 ls->region[j] = (char)chars;/** \todo ?! */
161 /** check if we have this lang */
162 ls->availability = 1;
163 ls->selectedlang = -1;
164 for (j = 0; j < nLocalesLoaded; j++) {
166 /** match against the LANG part */
167 result = strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
168 if ((result < 0) && (result < ls->availability)){
169 ls->availability = result;
170 ls->selectedlang = j;
172 /** match against lang and locale */
173 if (0 == strcasecmp(&lbuf[0], AvailLangLoaded[j])){
174 ls->availability = 0;
175 ls->selectedlang = j;
184 for (i = 0; ((i < nParts) && (i<SEARCH_LANG)); i++) {
185 ls = &wanted_locales[i];
186 if ((ls->availability <= 0) &&
187 (av < ls->availability) &&
188 (prio < ls->priority) &&
189 (ls->selectedlang != -1)) {
190 nBest = ls->selectedlang;
191 av = ls->availability;
196 /** fall back to C */
199 sess->selected_language = nBest;
200 syslog(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
206 * show the language chooser on the login dialog
207 * depending on the browser locale change the sequence of the
210 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
213 #ifndef HAVE_USELOCALE
214 char *Lang = getenv("LANG");
221 if (nLocalesLoaded == 1) {
222 wc_printf("<p>%s</p>", AvailLangLoaded[0]);
226 wc_printf("<select name=\"language\" id=\"lname\" size=\"1\" onChange=\"switch_to_lang($('lname').value);\">\n");
228 for (i=0; i < nLocalesLoaded; ++i) {
229 #ifndef HAVE_USELOCALE
230 if (strcmp(AvailLangLoaded[i], Lang) == 0)
232 wc_printf("<option %s value=%s>%s</option>\n",
233 ((WC->selected_language == i) ? "selected" : ""),
239 wc_printf("</select>\n");
243 * Set the selected language for this session.
245 void set_selected_language(const char *lang) {
246 #ifdef HAVE_USELOCALE
248 for (i = 0; i<nLocalesLoaded; ++i) {
249 if (!strcasecmp(lang, AvailLangLoaded[i])) {
250 WC->selected_language = i;
257 * Activate the selected language for this session.
259 void go_selected_language(void) {
260 #ifdef HAVE_USELOCALE
262 if (WCC->selected_language < 0) return;
263 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
264 textdomain(textdomain(NULL)); /** clear the cache */
268 language = getenv("LANG");
269 setlocale(LC_MESSAGES, language);
274 * Deactivate the selected language for this session.
276 void stop_selected_language(void) {
277 #ifdef HAVE_USELOCALE
278 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
279 textdomain(textdomain(NULL)); /** clear the cache */
283 #ifdef HAVE_USELOCALE
284 locale_t Empty_Locale;
288 * Create a locale_t for each available language
290 void initialize_locales(void) {
294 char *language = NULL;
297 while (!IsEmptyStr(AvailLang[nLocales]))
300 language = getenv("WEBCIT_LANG");
301 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
302 syslog(9, "Nailing locale to %s\n", language);
304 else language = NULL;
306 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
307 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
308 #ifdef HAVE_USELOCALE
309 wc_locales = malloc (sizeof(locale_t) * nLocales);
310 memset(wc_locales,0, sizeof(locale_t) * nLocales);
311 /* create default locale */
312 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
318 for (i = 0; i < nLocales; ++i) {
319 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
322 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
325 sprintf(buf, "%s.UTF8", AvailLang[i]);
327 #ifdef HAVE_USELOCALE
328 wc_locales[nLocalesLoaded] = newlocale(
329 (LC_MESSAGES_MASK|LC_TIME_MASK),
331 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
333 if (wc_locales[nLocalesLoaded] == NULL) {
334 syslog(1, "locale for "LOCALEDIR"locale/%s: %s; disabled\n",
340 syslog(3, "Found locale: %s\n", buf);
341 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
345 if ((language != NULL) && (strcmp(language, AvailLang[i]) == 0)) {
346 setenv("LANG", buf, 1);
347 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
348 setlocale(LC_MESSAGES, AvailLang[i]);
351 else if (nLocalesLoaded == 0) {
352 setenv("LANG", buf, 1);
353 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
358 if ((language != NULL) && (nLocalesLoaded == 0)) {
359 syslog(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
360 #ifdef HAVE_USELOCALE
361 wc_locales[0] = newlocale(
362 (LC_MESSAGES_MASK|LC_TIME_MASK),
366 setlocale(LC_MESSAGES, AvailLang[0]);
367 setenv("LANG", AvailLang[0], 1);
369 AvailLangLoaded[0] = AvailLang[0];
374 /*locale = */setlocale(LC_ALL, "");
376 syslog(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
377 syslog(9, "Text domain: %s\n", textdomain("webcit"));
378 syslog(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
385 ServerShutdownModule_GETTEXT
388 #ifdef HAVE_USELOCALE
390 for (i = 0; i < nLocalesLoaded; ++i) {
391 if (Empty_Locale != wc_locales[i])
392 freelocale(wc_locales[i]);
396 free(AvailLangLoaded);
399 #else /* ENABLE_NLS */
400 const char *AvailLang[] = {
403 /* dummy for non NLS enabled systems */
404 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
406 wc_printf("English (US)");
409 /* dummy for non NLS enabled systems */
410 void set_selected_language(const char *lang) {
413 /* dummy for non NLS enabled systems */
414 void go_selected_language(void) {
417 /* dummy for non NLS enabled systems */
418 void stop_selected_language(void) {
421 void initialize_locales(void) {
424 #endif /* ENABLE_NLS */
427 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
429 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
434 * Returns the language currently in use.
435 * This function returns a static string, so don't do anything stupid please.
437 const char *get_selected_language(void) {
439 #ifdef HAVE_USELOCALE
440 return AvailLang[WC->selected_language];
450 void Header_HandleAcceptLanguage(StrBuf *Line, ParsedHttpHdrs *hdr)
452 hdr->HR.browser_language = Line;
460 initialize_locales();
462 RegisterHeaderHandler(HKEY("ACCEPT-LANGUAGE"),
463 Header_HandleAcceptLanguage);
465 RegisterNamespace("LANG:SELECT", 0, 0,
466 tmplput_offer_languages, NULL, CTX_NONE);
471 SessionNewModule_GETTEXT
476 && (!sess->Hdr->HR.Static)
477 && (sess->Hdr->HR.browser_language != NULL)
479 httplang_to_locale(sess->Hdr->HR.browser_language, sess);
485 SessionAttachModule_GETTEXT
489 go_selected_language(); /* set locale */
494 SessionDestroyModule_GETTEXT
498 stop_selected_language(); /* unset locale */