9 /* actual supported locales */
10 const char *AvailLang[NUM_LANGS] = {
24 const char *AvailLangLoaded[NUM_LANGS];
25 long nLocalesLoaded = 0;
28 locale_t wc_locales[NUM_LANGS]; /**< here we keep the parsed stuff */
31 /** Keep information about one locale */
32 typedef struct _lang_pref{
33 char lang[16]; /**< the language locale string */
34 char region[16]; /**< the region locale string */
35 long priority; /**< which priority does it have */
36 int availability; /**< do we know it? */
37 int selectedlang; /**< is this the selected language? */
40 /* \brief parse browser locale header
41 * seems as most browsers just do a one after coma value even if more than 10 locales are available. Sample strings:
43 * 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
45 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
46 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
47 * 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
48 * \param LocaleString the string from the browser http headers
51 void httplang_to_locale(StrBuf *LocaleString, wcsession *sess)
53 LangStruct wanted_locales[SEARCH_LANG];
58 /* size_t len = strlen(LocaleString); */
66 nParts=StrBufNum_tokens(LocaleString,',');
67 for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
81 ls=&wanted_locales[i];
83 StrBufExtract_token(Buf,LocaleString, i,',');
84 /** we are searching, if this list item has something like ;q=n*/
85 if (StrBufNum_tokens(Buf,'=')>1) {
87 StrBufExtract_token(SBuf,Buf, 1,'=');
88 sbuflen=StrLength(SBuf);
89 for (k=0; k<sbuflen; k++)
90 if (ChrPtr(SBuf)[k]=='.')
91 StrBufPeek(SBuf, NULL, k, '0');
92 ls->priority=StrTol(SBuf);
97 /** get the locale part */
98 StrBufExtract_token(SBuf ,Buf, 0, ';');
99 /** get the lang part, which should be allways there */
100 extract_token(&ls->lang[0], ChrPtr(SBuf), 0, '-', 16);
101 /** get the area code if any. */
102 if (StrBufNum_tokens(SBuf,'-') > 1) {
103 extract_token(&ls->region[0],ChrPtr(SBuf),1,'-',16);
105 else { /** no ara code? use lang code */
106 blen=strlen(&ls->lang[0]);
107 memcpy(&ls->region[0], ls->lang,blen);
108 ls->region[blen]='\0';
109 } /** area codes are uppercase */
110 blen=strlen(&ls->region[0]);
111 for (j=0; j<blen; j++)
113 int chars=toupper(ls->region[j]);
114 ls->region[j]=(char)chars;/** \todo ?! */
116 sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
118 /** check if we have this lang */
121 for (j=0; j<nLocalesLoaded; j++) {
123 /** match against the LANG part */
124 result=strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
125 if ((result<0)&&(result<ls->availability)){
126 ls->availability=result;
129 /** match against lang and locale */
130 if (0==strcasecmp(&lbuf[0], AvailLangLoaded[j])){
141 for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++) {
142 ls=&wanted_locales[i];
143 if ((ls->availability<=0)&&
144 (av<ls->availability)&&
145 (prio<ls->priority)&&
146 (ls->selectedlang!=-1)) {
147 nBest=ls->selectedlang;
153 /** fall back to C */
156 sess->selected_language=nBest;
157 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
163 * \brief show the language chooser on the login dialog
164 * depending on the browser locale change the sequence of the
167 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
170 #ifndef HAVE_USELOCALE
171 char *Lang = getenv("LANG");
178 if (nLocalesLoaded == 1) {
179 wprintf("<p>%s</p>", AvailLangLoaded[0]);
183 wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
185 for (i=0; i < nLocalesLoaded; ++i) {
186 #ifndef HAVE_USELOCALE
187 if (strcmp(AvailLangLoaded[i], Lang) == 0)
189 wprintf("<option %s value=%s>%s</option>\n",
190 ((WC->selected_language == i) ? "selected" : ""),
196 wprintf("</select>\n");
200 * \brief Set the selected language for this session.
201 * \param lang the locale to set.
203 void set_selected_language(const char *lang) {
204 #ifdef HAVE_USELOCALE
206 for (i=0; i<nLocalesLoaded; ++i) {
207 if (!strcasecmp(lang, AvailLangLoaded[i])) {
208 WC->selected_language = i;
215 * \brief Activate the selected language for this session.
217 void go_selected_language(void) {
218 #ifdef HAVE_USELOCALE
220 if (WCC->selected_language < 0) return;
221 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
222 textdomain(textdomain(NULL)); /** clear the cache */
226 language = getenv("LANG");
227 setlocale(LC_MESSAGES, language);
232 * \brief Deactivate the selected language for this session.
234 void stop_selected_language(void) {
235 #ifdef HAVE_USELOCALE
236 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
237 textdomain(textdomain(NULL)); /** clear the cache */
241 #ifdef HAVE_USELOCALE
242 locale_t Empty_Locale;
246 * \brief Create a locale_t for each available language
248 void initialize_locales(void) {
251 char *language = NULL;
253 language = getenv("WEBCIT_LANG");
254 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
255 lprintf(9, "Nailing locale to %s\n", language);
257 else language = NULL;
259 #ifdef HAVE_USELOCALE
260 /* create default locale */
261 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
267 for (i = 0; i < NUM_LANGS; ++i) {
268 if ((language != NULL) && (strcmp(AvailLang[i], language) != 0))
271 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
274 sprintf(buf, "%s.UTF8", AvailLang[i]);
276 #ifdef HAVE_USELOCALE
277 wc_locales[nLocalesLoaded] = newlocale(
278 (LC_MESSAGES_MASK|LC_TIME_MASK),
280 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
282 if (wc_locales[nLocalesLoaded] == NULL) {
283 lprintf(1, "Error configuring locale for %s: %s\n",
289 lprintf(3, "Configured available locale: %s\n", buf);
290 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
294 if (language != NULL) {
295 setenv("LANG", buf, 1);
296 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
299 else if (nLocalesLoaded == 0) {
300 setenv("LANG", buf, 1);
301 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
306 if ((language != NULL) && (nLocalesLoaded == 0)) {
307 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
308 #ifdef HAVE_USELOCALE
309 wc_locales[0] = newlocale(
310 (LC_MESSAGES_MASK|LC_TIME_MASK),
314 setlocale(LC_MESSAGES, AvailLang[0]);
315 setenv("LANG", AvailLang[0], 1);
317 AvailLangLoaded[0] = AvailLang[0];
320 #ifndef HAVE_USELOCALE
328 void ShutdownLocale(void)
330 #ifdef HAVE_USELOCALE
332 for (i = 0; i < nLocalesLoaded; ++i) {
333 if (Empty_Locale != wc_locales[i])
334 freelocale(wc_locales[i]);
339 #else /* ENABLE_NLS */
340 const char *AvailLang[NUM_LANGS] = {
343 /** \brief dummy for non NLS enabled systems */
344 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
346 wprintf("English (US)");
349 /** \brief dummy for non NLS enabled systems */
350 void set_selected_language(char *lang) {
353 /** \brief dummy for non NLS enabled systems */
354 void go_selected_language(void) {
357 /** \brief dummy for non NLS enabled systems */
358 void stop_selected_language(void) {
361 #endif /* ENABLE_NLS */
364 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
366 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
371 * Returns the language currently in use.
372 * This function returns a static string, so don't do anything stupid please.
374 const char *get_selected_language(void) {
376 #ifdef HAVE_USELOCALE
377 return AvailLang[WC->selected_language];
390 RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);
395 SessionNewModule_GETTEXT
399 OneHttpHeader *vLine = NULL;
401 if ( (sess->Hdr->HTTPHeaders != NULL)
402 && GetHash(sess->Hdr->HTTPHeaders, HKEY("ACCEPT-LANGUAGE"), (void *)&vLine)
404 && (vLine->Val != NULL)
406 StrBuf *accept_language = vLine->Val;
407 httplang_to_locale(accept_language, sess);
413 SessionAttachModule_GETTEXT
417 go_selected_language(); /* set locale */
422 SessionDestroyModule_GETTEXT
426 stop_selected_language(); /* unset locale */