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");
176 wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
178 for (i=0; i < nLocalesLoaded; ++i) {
179 #ifndef HAVE_USELOCALE
180 if (strcmp(AvailLangLoaded[i], Lang) == 0)
182 wprintf("<option %s value=%s>%s</option>\n",
183 ((WC->selected_language == i) ? "selected" : ""),
189 wprintf("</select>\n");
193 * \brief Set the selected language for this session.
194 * \param lang the locale to set.
196 void set_selected_language(const char *lang) {
199 #ifdef HAVE_USELOCALE
200 for (i=0; i<nLocalesLoaded; ++i) {
201 if (!strcasecmp(lang, AvailLangLoaded[i])) {
202 WC->selected_language = i;
209 * \brief Activate the selected language for this session.
211 void go_selected_language(void) {
212 #ifdef HAVE_USELOCALE
214 if (WCC->selected_language < 0) return;
215 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
216 textdomain(textdomain(NULL)); /** clear the cache */
220 language = getenv("LANG");
221 setlocale(LC_MESSAGES, language);
226 * \brief Deactivate the selected language for this session.
228 void stop_selected_language(void) {
229 #ifdef HAVE_USELOCALE
230 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
231 textdomain(textdomain(NULL)); /** clear the cache */
235 void preset_locale(void)
240 language = getenv("WEBCIT_LANG");
241 if ((language) && (!IsEmptyStr(language)) && (strcmp(language, "UNLIMITED") != 0)) {
242 lprintf(9, "Nailing locale to %s\n", language);
243 setlocale(LC_MESSAGES, language);
248 #ifdef HAVE_USELOCALE
249 locale_t Empty_Locale;
253 * \brief Create a locale_t for each available language
255 void initialize_locales(void) {
259 #ifdef HAVE_USELOCALE
260 /* create default locale */
261 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
264 for (i = 0; i < NUM_LANGS; ++i) {
266 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
269 sprintf(buf, "%s.UTF8", AvailLang[i]);
271 #ifdef HAVE_USELOCALE
272 wc_locales[nLocalesLoaded] = newlocale(
273 (LC_MESSAGES_MASK|LC_TIME_MASK),
275 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
277 if (wc_locales[nLocalesLoaded] == NULL) {
278 lprintf(1, "Error configuring locale for %s: %s\n",
284 lprintf(3, "Configured available locale: %s\n", buf);
285 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
293 void ShutdownLocale(void)
296 #ifdef HAVE_USELOCALE
297 for (i = 0; i < nLocalesLoaded; ++i) {
298 if (Empty_Locale != wc_locales[i])
299 freelocale(wc_locales[i]);
304 #else /* ENABLE_NLS */
305 const char *AvailLang[NUM_LANGS] = {
308 /** \brief dummy for non NLS enabled systems */
309 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
311 wprintf("English (US)");
314 /** \brief dummy for non NLS enabled systems */
315 void set_selected_language(char *lang) {
318 /** \brief dummy for non NLS enabled systems */
319 void go_selected_language(void) {
322 /** \brief dummy for non NLS enabled systems */
323 void stop_selected_language(void) {
326 void preset_locale(void)
329 #endif /* ENABLE_NLS */
332 void TmplGettext(StrBuf *Target, WCTemplputParams *TP)
334 StrBufAppendBufPlain(Target, _(TP->Tokens->Params[0]->Start), -1, 0);
339 * Returns the language currently in use.
340 * This function returns a static string, so don't do anything stupid please.
342 const char *get_selected_language(void) {
344 #ifdef HAVE_USELOCALE
345 return AvailLang[WC->selected_language];
358 RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);