10 #define NUM_LANGS 10 /* how many different locales do we know? */
11 #define SEARCH_LANG 20 /* how many langs should we parse? */
13 /* actual supported locales */
14 const char *AvailLang[NUM_LANGS] = {
27 const char *AvailLangLoaded[NUM_LANGS];
28 long nLocalesLoaded = 0;
31 locale_t wc_locales[NUM_LANGS]; /**< 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)
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++)
85 ls=&wanted_locales[i];
87 StrBufExtract_token(Buf,LocaleString, i,',');
88 /** we are searching, if this list item has something like ;q=n*/
89 if (StrBufNum_tokens(Buf,'=')>1) {
91 StrBufExtract_token(SBuf,Buf, 1,'=');
92 sbuflen=strlen(&sbuf[0]);
93 for (k=0; k<sbuflen; k++) if (sbuf[k]=='.') sbuf[k]='0';
94 ls->priority=atol(&sbuf[0]);
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 WC->selected_language=nBest;
159 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
164 /* TODO: we skip the language weighting so far. */
165 /* Accept-Language: 'de-de,en-us;q=0.7,en;q=0.3' */
166 /* Accept-Language: de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3 */
167 //void httplang_to_locale(char *LocaleString)
169 // char selected_locale[16];
172 // int num_accept = 0;
174 // lprintf(9, "languageAccept: %s\n", LocaleString);
176 // strcpy(selected_locale, "C");
177 // num_accept = num_tokens(LocaleString, ',');
179 // for (i=num_accept-1; i>=0; --i) {
180 // extract_token(lang, LocaleString, i, ',', sizeof lang);
182 // /* Strip out the weights; we don't use them. Also convert
183 // * hyphens to underscores.
185 // for (j=0; j<strlen(lang); ++j) {
186 // if (lang[j] == '-') lang[j] = '_';
187 // if (lang[j] == ';') lang[j] = 0;
190 // for (j=0; j<NUM_LANGS; ++j) {
191 // if (!strncasecmp(lang, AvailLang[j], strlen(lang))) {
192 // strcpy(selected_locale, AvailLang[j]);
197 // lprintf(9, "language found: %s\n", selected_locale);
198 // set_selected_language(selected_locale);
203 * \brief show the language chooser on the login dialog
204 * depending on the browser locale change the sequence of the
207 void offer_languages(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType) {
209 #ifndef HAVE_USELOCALE
210 char *Lang = getenv("LANG");
216 wprintf("<select name=\"language\" id=\"lname\" size=\"1\">\n");
218 for (i=0; i < nLocalesLoaded; ++i) {
219 #ifndef HAVE_USELOCALE
220 if (strcmp(AvailLangLoaded[i], Lang) == 0)
222 wprintf("<option %s value=%s>%s</option>\n",
223 ((WC->selected_language == i) ? "selected" : ""),
229 wprintf("</select>\n");
233 * \brief Set the selected language for this session.
234 * \param lang the locale to set.
236 void set_selected_language(const char *lang) {
239 #ifdef HAVE_USELOCALE
240 for (i=0; i<nLocalesLoaded; ++i) {
241 if (!strcasecmp(lang, AvailLangLoaded[i])) {
242 WC->selected_language = i;
249 * \brief Activate the selected language for this session.
251 void go_selected_language(void) {
252 #ifdef HAVE_USELOCALE
253 struct wcsession *WCC = WC;
254 if (WCC->selected_language < 0) return;
255 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
256 textdomain(textdomain(NULL)); /** clear the cache */
260 language = getenv("LANG");
261 setlocale(LC_MESSAGES, language);
266 * \brief Deactivate the selected language for this session.
268 void stop_selected_language(void) {
269 #ifdef HAVE_USELOCALE
270 uselocale(LC_GLOBAL_LOCALE); /** switch locales */
271 textdomain(textdomain(NULL)); /** clear the cache */
275 void preset_locale(void)
277 #ifndef HAVE_USELOCALE
281 lprintf(9, "Nailing locale to %s\n", getenv("LANG"));
282 language = getenv("LANG");
283 setlocale(LC_MESSAGES, language);
288 #ifdef HAVE_USELOCALE
289 locale_t Empty_Locale;
293 * \brief Create a locale_t for each available language
295 void initialize_locales(void) {
299 #ifdef HAVE_USELOCALE
300 /* create default locale */
301 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
304 for (i = 0; i < NUM_LANGS; ++i) {
306 sprintf(buf, "%s", AvailLang[i]); // locale 0 (C) is ascii, not utf-8
309 sprintf(buf, "%s.UTF8", AvailLang[i]);
311 #ifdef HAVE_USELOCALE
312 wc_locales[nLocalesLoaded] = newlocale(
313 (LC_MESSAGES_MASK|LC_TIME_MASK),
315 (((i > 0) && (wc_locales[0] != NULL)) ? wc_locales[0] : Empty_Locale)
317 if (wc_locales[nLocalesLoaded] == NULL) {
318 lprintf(1, "Error configuring locale for %s: %s\n",
324 lprintf(3, "Configured available locale: %s\n", buf);
325 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
333 void ShutdownLocale(void)
336 #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 /** \brief dummy for non NLS enabled systems */
346 void offer_languages(void) {
347 wprintf("English (US)");
350 /** \brief dummy for non NLS enabled systems */
351 void set_selected_language(char *lang) {
354 /** \brief dummy for non NLS enabled systems */
355 void go_selected_language(void) {
358 /** \brief dummy for non NLS enabled systems */
359 void stop_selected_language(void) {
362 void preset_locale(void)
365 #endif /* ENABLE_NLS */
368 void TmplGettext(StrBuf *Target, int nTokens, WCTemplateToken *Token)
370 StrBufAppendBufPlain(Target, _(Token->Params[0]->Start), -1, 0);