9 /* actual supported locales */
10 const char *AvailLang[NUM_LANGS] = {
23 const char *AvailLangLoaded[NUM_LANGS];
24 long nLocalesLoaded = 0;
27 locale_t wc_locales[NUM_LANGS]; /**< here we keep the parsed stuff */
30 /** Keep information about one locale */
31 typedef struct _lang_pref{
32 char lang[16]; /**< the language locale string */
33 char region[16]; /**< the region locale string */
34 long priority; /**< which priority does it have */
35 int availability; /**< do we know it? */
36 int selectedlang; /**< is this the selected language? */
39 /* \brief parse browser locale header
40 * seems as most browsers just do a one after coma value even if more than 10 locales are available. Sample strings:
42 * 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
44 * Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3'
45 * Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3
46 * 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
47 * \param LocaleString the string from the browser http headers
50 void httplang_to_locale(StrBuf *LocaleString)
52 LangStruct wanted_locales[SEARCH_LANG];
57 /* size_t len = strlen(LocaleString); */
65 nParts=StrBufNum_tokens(LocaleString,',');
66 for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
80 ls=&wanted_locales[i];
82 StrBufExtract_token(Buf,LocaleString, i,',');
83 /** we are searching, if this list item has something like ;q=n*/
84 if (StrBufNum_tokens(Buf,'=')>1) {
86 StrBufExtract_token(SBuf,Buf, 1,'=');
87 sbuflen=StrLength(SBuf);
88 for (k=0; k<sbuflen; k++)
89 if (ChrPtr(SBuf)[k]=='.')
90 StrBufPeek(SBuf, NULL, k, '0');
91 ls->priority=StrTol(SBuf);
96 /** get the locale part */
97 StrBufExtract_token(SBuf ,Buf, 0, ';');
98 /** get the lang part, which should be allways there */
99 extract_token(&ls->lang[0], ChrPtr(SBuf), 0, '-', 16);
100 /** get the area code if any. */
101 if (StrBufNum_tokens(SBuf,'-') > 1) {
102 extract_token(&ls->region[0],ChrPtr(SBuf),1,'-',16);
104 else { /** no ara code? use lang code */
105 blen=strlen(&ls->lang[0]);
106 memcpy(&ls->region[0], ls->lang,blen);
107 ls->region[blen]='\0';
108 } /** area codes are uppercase */
109 blen=strlen(&ls->region[0]);
110 for (j=0; j<blen; j++)
112 int chars=toupper(ls->region[j]);
113 ls->region[j]=(char)chars;/** \todo ?! */
115 sprintf(&lbuf[0],"%s_%s",&ls->lang[0],&ls->region[0]);
117 /** check if we have this lang */
120 for (j=0; j<nLocalesLoaded; j++) {
122 /** match against the LANG part */
123 result=strcasecmp(&ls->lang[0], AvailLangLoaded[j]);
124 if ((result<0)&&(result<ls->availability)){
125 ls->availability=result;
128 /** match against lang and locale */
129 if (0==strcasecmp(&lbuf[0], AvailLangLoaded[j])){
140 for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++) {
141 ls=&wanted_locales[i];
142 if ((ls->availability<=0)&&
143 (av<ls->availability)&&
144 (prio<ls->priority)&&
145 (ls->selectedlang!=-1)) {
146 nBest=ls->selectedlang;
152 /** fall back to C */
155 WC->selected_language=nBest;
156 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
162 * \brief show the language chooser on the login dialog
163 * depending on the browser locale change the sequence of the
166 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
169 #ifndef HAVE_USELOCALE
170 char *Lang = getenv("LANG");
177 if (nLocalesLoaded == 1) {
178 wprintf("<p>%s</p>", AvailLangLoaded[0]);
182 wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
184 for (i=0; i < nLocalesLoaded; ++i) {
185 #ifndef HAVE_USELOCALE
186 if (strcmp(AvailLangLoaded[i], Lang) == 0)
188 wprintf("<option %s value=%s>%s</option>\n",
189 ((WC->selected_language == i) ? "selected" : ""),
195 wprintf("</select>\n");
199 * \brief Set the selected language for this session.
200 * \param lang the locale to set.
202 void set_selected_language(const char *lang) {
203 #ifdef HAVE_USELOCALE
205 for (i=0; i<nLocalesLoaded; ++i) {
206 if (!strcasecmp(lang, AvailLangLoaded[i])) {
207 WC->selected_language = i;
214 * \brief Activate the selected language for this session.
216 void go_selected_language(void) {
217 #ifdef HAVE_USELOCALE
219 if (WCC->selected_language < 0) return;
220 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
221 textdomain(textdomain(NULL)); /** clear the cache */
225 language = getenv("LANG");
226 setlocale(LC_MESSAGES, language);
231 * \brief Deactivate the selected language for this session.
233 void stop_selected_language(void) {
234 #ifdef HAVE_USELOCALE
235 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
236 textdomain(textdomain(NULL)); /** clear the cache */
240 void preset_locale(void)
245 language = getenv("WEBCIT_LANG");
246 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
247 lprintf(9, "Nailing locale to %s\n", language);
248 setlocale(LC_MESSAGES, language);
253 #ifdef HAVE_USELOCALE
254 locale_t Empty_Locale;
258 * \brief Create a locale_t for each available language
260 void initialize_locales(void) {
263 char *language = NULL;
265 language = getenv("WEBCIT_LANG");
266 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
267 lprintf(9, "Nailing locale to %s\n", language);
268 setlocale(LC_MESSAGES, language);
270 else language = NULL;
272 #ifdef HAVE_USELOCALE
273 /* create default locale */
274 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
280 for (i = 0; i < NUM_LANGS; ++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 %s: %s\n",
302 lprintf(3, "Configured available locale: %s\n", buf);
303 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
307 setenv("LANG", buf, 1);
308 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
312 if ((language != NULL) && (nLocalesLoaded == 0)) {
313 lprintf(1, "Your selected locale [%s] isn't available on your system. falling back to C\n", language);
314 #ifdef HAVE_USELOCALE
315 wc_locales[0] = newlocale(
316 (LC_MESSAGES_MASK|LC_TIME_MASK),
320 setenv("LANG", AvailLang[0], 1);
322 AvailLangLoaded[0] = AvailLang[0];
325 #ifndef HAVE_USELOCALE
333 void ShutdownLocale(void)
335 #ifdef HAVE_USELOCALE
337 for (i = 0; i < nLocalesLoaded; ++i) {
338 if (Empty_Locale != wc_locales[i])
339 freelocale(wc_locales[i]);
344 #else /* ENABLE_NLS */
345 const char *AvailLang[NUM_LANGS] = {
348 /** \brief dummy for non NLS enabled systems */
349 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
351 wprintf("English (US)");
354 /** \brief dummy for non NLS enabled systems */
355 void set_selected_language(char *lang) {
358 /** \brief dummy for non NLS enabled systems */
359 void go_selected_language(void) {
362 /** \brief dummy for non NLS enabled systems */
363 void stop_selected_language(void) {
366 void preset_locale(void)
369 #endif /* ENABLE_NLS */
372 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
374 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
379 * Returns the language currently in use.
380 * This function returns a static string, so don't do anything stupid please.
382 const char *get_selected_language(void) {
384 #ifdef HAVE_USELOCALE
385 return AvailLang[WC->selected_language];
398 RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);