7 #define SEARCH_LANG 20 /* how many langs should we parse? */
10 /* actual supported locales */
11 const char *AvailLang[] = {
27 const char **AvailLangLoaded;
28 long nLocalesLoaded = 0;
31 locale_t *wc_locales; /**< here we keep the parsed stuff */
34 /** Keep information about one locale */
35 typedef struct _lang_pref{
36 char lang[16]; /**< the language locale string */
37 char region[16]; /**< the region locale string */
38 long priority; /**< which priority does it have */
39 int availability; /**< do we know it? */
40 int selectedlang; /**< is this the selected language? */
43 /* \brief parse browser locale header
44 * seems as most browsers just do a one after coma value even if more than 10 locales are available. Sample strings:
46 * 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
48 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
49 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
50 * 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
51 * \param LocaleString the string from the browser http headers
54 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
56 LangStruct wanted_locales[SEARCH_LANG];
61 /* size_t len = strlen(LocaleString); */
69 nParts=StrBufNum_tokens(LocaleString,',');
70 for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
84 ls=&wanted_locales[i];
86 StrBufExtract_token(Buf,LocaleString, i,',');
87 /** we are searching, if this list item has something like ;q=n*/
88 if (StrBufNum_tokens(Buf,'=')>1) {
90 StrBufExtract_token(SBuf,Buf, 1,'=');
91 sbuflen=StrLength(SBuf);
92 for (k=0; k<sbuflen; k++)
93 if (ChrPtr(SBuf)[k]=='.')
94 StrBufPeek(SBuf, NULL, k, '0');
95 ls->priority=StrTol(SBuf);
100 /** get the locale part */
101 StrBufExtract_token(SBuf ,Buf, 0, ';');
102 /** get the lang part, which should be allways there */
103 extract_token(&ls->lang[0], ChrPtr(SBuf), 0, '-', 16);
104 /** get the area code if any. */
105 if (StrBufNum_tokens(SBuf,'-') > 1) {
106 extract_token(&ls->region[0],ChrPtr(SBuf),1,'-',16);
108 else { /** no ara code? use lang code */
109 blen=strlen(&ls->lang[0]);
110 memcpy(&ls->region[0], ls->lang,blen);
111 ls->region[blen]='\0';
112 } /** area codes are uppercase */
113 blen=strlen(&ls->region[0]);
114 for (j=0; j<blen; j++)
116 int chars=toupper(ls->region[j]);
117 ls->region[j]=(char)chars;/** \todo ?! */
119 sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
121 /** check if we have this lang */
124 for (j=0; j<nLocalesLoaded; j++) {
126 /** match against the LANG part */
127 result=strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
128 if ((result<0)&&(result<ls->availability)){
129 ls->availability=result;
132 /** match against lang and locale */
133 if (0==strcasecmp(&lbuf[0], AvailLangLoaded[j])){
144 for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++) {
145 ls=&wanted_locales[i];
146 if ((ls->availability<=0)&&
147 (av<ls->availability)&&
148 (prio<ls->priority)&&
149 (ls->selectedlang!=-1)) {
150 nBest=ls->selectedlang;
156 /** fall back to C */
159 sess->selected_language=nBest;
160 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
166 * \brief show the language chooser on the login dialog
167 * depending on the browser locale change the sequence of the
170 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
173 #ifndef HAVE_USELOCALE
174 char *Lang = getenv("LANG");
181 if (nLocalesLoaded == 1) {
182 wprintf("<p>%s</p>", AvailLangLoaded[0]);
186 wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
188 for (i=0; i < nLocalesLoaded; ++i) {
189 #ifndef HAVE_USELOCALE
190 if (strcmp(AvailLangLoaded[i], Lang) == 0)
192 wprintf("<option %s value=%s>%s</option>\n",
193 ((WC->selected_language == i) ? "selected" : ""),
199 wprintf("</select>\n");
203 * \brief Set the selected language for this session.
204 * \param lang the locale to set.
206 void set_selected_language(const char *lang) {
207 #ifdef HAVE_USELOCALE
209 for (i=0; i<nLocalesLoaded; ++i) {
210 if (!strcasecmp(lang, AvailLangLoaded[i])) {
211 WC->selected_language = i;
218 * \brief Activate the selected language for this session.
220 void go_selected_language(void) {
221 #ifdef HAVE_USELOCALE
223 if (WCC->selected_language < 0) return;
224 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
225 textdomain(textdomain(NULL)); /** clear the cache */
229 language = getenv("LANG");
230 setlocale(LC_MESSAGES, language);
235 * \brief Deactivate the selected language for this session.
237 void stop_selected_language(void) {
238 #ifdef HAVE_USELOCALE
239 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
240 textdomain(textdomain(NULL)); /** clear the cache */
244 #ifdef HAVE_USELOCALE
245 locale_t Empty_Locale;
249 * \brief Create a locale_t for each available language
251 void initialize_locales(void) {
255 char *language = NULL;
260 while (!IsEmptyStr(AvailLang[nLocales]))
263 language = getenv("WEBCIT_LANG");
264 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
265 lprintf(9, "Nailing locale to %s\n", language);
267 else language = NULL;
269 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
270 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
271 #ifdef HAVE_USELOCALE
272 wc_locales = malloc (sizeof(locale_t) * nLocales);
273 memset(wc_locales,0, sizeof(locale_t) * nLocales);
274 /* create default locale */
275 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
281 for (i = 0; i < nLocales; ++i) {
282 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
285 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
288 sprintf(buf, "%s.UTF8", AvailLang[i]);
290 #ifdef HAVE_USELOCALE
291 wc_locales[nLocalesLoaded] = newlocale(
292 (LC_MESSAGES_MASK|LC_TIME_MASK),
294 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
296 if (wc_locales[nLocalesLoaded] == NULL) {
297 lprintf(1, "Error configuring locale for "LOCALEDIR"locale/%s: %s\n",
303 lprintf(3, "Configured available locale: %s\n", buf);
304 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
308 if (language != NULL) {
309 setenv("LANG", buf, 1);
310 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
313 else if (nLocalesLoaded == 0) {
314 setenv("LANG", buf, 1);
315 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
320 if ((language != NULL) && (nLocalesLoaded == 0)) {
321 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
322 #ifdef HAVE_USELOCALE
323 wc_locales[0] = newlocale(
324 (LC_MESSAGES_MASK|LC_TIME_MASK),
328 setlocale(LC_MESSAGES, AvailLang[0]);
329 setenv("LANG", AvailLang[0], 1);
331 AvailLangLoaded[0] = AvailLang[0];
334 #ifndef HAVE_USELOCALE
340 locale = setlocale(LC_ALL, "");
342 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
343 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
344 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
351 ServerShutdownModule_GETTEXT
354 #ifdef HAVE_USELOCALE
356 for (i = 0; i < nLocalesLoaded; ++i) {
357 if (Empty_Locale != wc_locales[i])
358 freelocale(wc_locales[i]);
361 free(AvailLangLoaded);
365 #else /* ENABLE_NLS */
366 const char *AvailLang[NUM_LANGS] = {
369 /** \brief dummy for non NLS enabled systems */
370 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
372 wprintf("English (US)");
375 /** \brief dummy for non NLS enabled systems */
376 void set_selected_language(char *lang) {
379 /** \brief dummy for non NLS enabled systems */
380 void go_selected_language(void) {
383 /** \brief dummy for non NLS enabled systems */
384 void stop_selected_language(void) {
387 #endif /* ENABLE_NLS */
390 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
392 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
397 * Returns the language currently in use.
398 * This function returns a static string, so don't do anything stupid please.
400 const char *get_selected_language(void) {
402 #ifdef HAVE_USELOCALE
403 return AvailLang[WC->selected_language];
416 initialize_locales();
417 RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);
422 SessionNewModule_GETTEXT
426 OneHttpHeader *vLine = NULL;
428 if ( (sess->Hdr->HTTPHeaders != NULL)
429 && GetHash(sess->Hdr->HTTPHeaders, HKEY("ACCEPT-LANGUAGE"), (void *)&vLine)
431 && (vLine->Val != NULL)
433 StrBuf *accept_language = vLine->Val;
434 httplang_to_locale(accept_language, sess);
440 SessionAttachModule_GETTEXT
444 go_selected_language(); /* set locale */
449 SessionDestroyModule_GETTEXT
453 stop_selected_language(); /* unset locale */