7 #define SEARCH_LANG 20 /* how many langs should we parse? */
10 /* actual supported locales */
11 const char *AvailLang[] = {
26 const char **AvailLangLoaded;
27 long nLocalesLoaded = 0;
30 locale_t *wc_locales; /**< here we keep the parsed stuff */
33 /** Keep information about one locale */
34 typedef struct _lang_pref{
35 char lang[16]; /**< the language locale string */
36 char region[16]; /**< the region locale string */
37 long priority; /**< which priority does it have */
38 int availability; /**< do we know it? */
39 int selectedlang; /**< is this the selected language? */
42 /* \brief parse browser locale header
43 * seems as most browsers just do a one after coma value even if more than 10 locales are available. Sample strings:
45 * 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
47 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
48 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
49 * 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
50 * \param LocaleString the string from the browser http headers
53 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
55 LangStruct wanted_locales[SEARCH_LANG];
60 /* size_t len = strlen(LocaleString); */
68 nParts=StrBufNum_tokens(LocaleString,',');
69 for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
83 ls=&wanted_locales[i];
85 StrBufExtract_token(Buf,LocaleString, i,',');
86 /** we are searching, if this list item has something like ;q=n*/
87 if (StrBufNum_tokens(Buf,'=')>1) {
89 StrBufExtract_token(SBuf,Buf, 1,'=');
90 sbuflen=StrLength(SBuf);
91 for (k=0; k<sbuflen; k++)
92 if (ChrPtr(SBuf)[k]=='.')
93 StrBufPeek(SBuf, NULL, k, '0');
94 ls->priority=StrTol(SBuf);
99 /** get the locale part */
100 StrBufExtract_token(SBuf ,Buf, 0, ';');
101 /** get the lang part, which should be allways there */
102 extract_token(&ls->lang[0], ChrPtr(SBuf), 0, '-', 16);
103 /** get the area code if any. */
104 if (StrBufNum_tokens(SBuf,'-') > 1) {
105 extract_token(&ls->region[0],ChrPtr(SBuf),1,'-',16);
107 else { /** no ara code? use lang code */
108 blen=strlen(&ls->lang[0]);
109 memcpy(&ls->region[0], ls->lang,blen);
110 ls->region[blen]='\0';
111 } /** area codes are uppercase */
112 blen=strlen(&ls->region[0]);
113 for (j=0; j<blen; j++)
115 int chars=toupper(ls->region[j]);
116 ls->region[j]=(char)chars;/** \todo ?! */
118 sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
120 /** check if we have this lang */
123 for (j=0; j<nLocalesLoaded; j++) {
125 /** match against the LANG part */
126 result=strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
127 if ((result<0)&&(result<ls->availability)){
128 ls->availability=result;
131 /** match against lang and locale */
132 if (0==strcasecmp(&lbuf[0], AvailLangLoaded[j])){
143 for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++) {
144 ls=&wanted_locales[i];
145 if ((ls->availability<=0)&&
146 (av<ls->availability)&&
147 (prio<ls->priority)&&
148 (ls->selectedlang!=-1)) {
149 nBest=ls->selectedlang;
155 /** fall back to C */
158 sess->selected_language=nBest;
159 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
165 * \brief show the language chooser on the login dialog
166 * depending on the browser locale change the sequence of the
169 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
172 #ifndef HAVE_USELOCALE
173 char *Lang = getenv("LANG");
180 if (nLocalesLoaded == 1) {
181 wprintf("<p>%s</p>", AvailLangLoaded[0]);
185 wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
187 for (i=0; i < nLocalesLoaded; ++i) {
188 #ifndef HAVE_USELOCALE
189 if (strcmp(AvailLangLoaded[i], Lang) == 0)
191 wprintf("<option %s value=%s>%s</option>\n",
192 ((WC->selected_language == i) ? "selected" : ""),
198 wprintf("</select>\n");
202 * \brief Set the selected language for this session.
203 * \param lang the locale to set.
205 void set_selected_language(const char *lang) {
206 #ifdef HAVE_USELOCALE
208 for (i=0; i<nLocalesLoaded; ++i) {
209 if (!strcasecmp(lang, AvailLangLoaded[i])) {
210 WC->selected_language = i;
217 * \brief Activate the selected language for this session.
219 void go_selected_language(void) {
220 #ifdef HAVE_USELOCALE
222 if (WCC->selected_language < 0) return;
223 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
224 textdomain(textdomain(NULL)); /** clear the cache */
228 language = getenv("LANG");
229 setlocale(LC_MESSAGES, language);
234 * \brief Deactivate the selected language for this session.
236 void stop_selected_language(void) {
237 #ifdef HAVE_USELOCALE
238 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
239 textdomain(textdomain(NULL)); /** clear the cache */
243 #ifdef HAVE_USELOCALE
244 locale_t Empty_Locale;
248 * \brief Create a locale_t for each available language
250 void initialize_locales(void) {
254 char *language = NULL;
259 while (!IsEmptyStr(AvailLang[nLocales]))
262 language = getenv("WEBCIT_LANG");
263 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
264 lprintf(9, "Nailing locale to %s\n", language);
266 else language = NULL;
268 AvailLangLoaded = malloc (sizeof(char*) * nLocales);
269 memset(AvailLangLoaded, 0, sizeof(char*) * nLocales);
270 #ifdef HAVE_USELOCALE
271 wc_locales = malloc (sizeof(locale_t) * nLocales);
272 memset(wc_locales,0, sizeof(locale_t) * nLocales);
273 /* create default locale */
274 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
280 for (i = 0; i < nLocales; ++i) {
281 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
284 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
287 sprintf(buf, "%s.UTF8", AvailLang[i]);
289 #ifdef HAVE_USELOCALE
290 wc_locales[nLocalesLoaded] = newlocale(
291 (LC_MESSAGES_MASK|LC_TIME_MASK),
293 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
295 if (wc_locales[nLocalesLoaded] == NULL) {
296 lprintf(1, "Error configuring locale for "LOCALEDIR"locale/%s: %s\n",
302 lprintf(3, "Configured available locale: %s\n", buf);
303 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
307 if (language != NULL) {
308 setenv("LANG", buf, 1);
309 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
312 else if (nLocalesLoaded == 0) {
313 setenv("LANG", buf, 1);
314 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
319 if ((language != NULL) && (nLocalesLoaded == 0)) {
320 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
321 #ifdef HAVE_USELOCALE
322 wc_locales[0] = newlocale(
323 (LC_MESSAGES_MASK|LC_TIME_MASK),
327 setlocale(LC_MESSAGES, AvailLang[0]);
328 setenv("LANG", AvailLang[0], 1);
330 AvailLangLoaded[0] = AvailLang[0];
333 #ifndef HAVE_USELOCALE
339 locale = setlocale(LC_ALL, "");
341 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
342 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
343 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
350 ServerShutdownModule_GETTEXT
353 #ifdef HAVE_USELOCALE
355 for (i = 0; i < nLocalesLoaded; ++i) {
356 if (Empty_Locale != wc_locales[i])
357 freelocale(wc_locales[i]);
360 free(AvailLangLoaded);
364 #else /* ENABLE_NLS */
365 const char *AvailLang[NUM_LANGS] = {
368 /** \brief dummy for non NLS enabled systems */
369 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
371 wprintf("English (US)");
374 /** \brief dummy for non NLS enabled systems */
375 void set_selected_language(char *lang) {
378 /** \brief dummy for non NLS enabled systems */
379 void go_selected_language(void) {
382 /** \brief dummy for non NLS enabled systems */
383 void stop_selected_language(void) {
386 #endif /* ENABLE_NLS */
389 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
391 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
396 * Returns the language currently in use.
397 * This function returns a static string, so don't do anything stupid please.
399 const char *get_selected_language(void) {
401 #ifdef HAVE_USELOCALE
402 return AvailLang[WC->selected_language];
415 initialize_locales();
416 RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);
421 SessionNewModule_GETTEXT
425 OneHttpHeader *vLine = NULL;
427 if ( (sess->Hdr->HTTPHeaders != NULL)
428 && GetHash(sess->Hdr->HTTPHeaders, HKEY("ACCEPT-LANGUAGE"), (void *)&vLine)
430 && (vLine->Val != NULL)
432 StrBuf *accept_language = vLine->Val;
433 httplang_to_locale(accept_language, sess);
439 SessionAttachModule_GETTEXT
443 go_selected_language(); /* set locale */
448 SessionDestroyModule_GETTEXT
452 stop_selected_language(); /* unset locale */