* When saving a Task, if the status is COMPLETED then also set PERCENT-COMPLETE:100...
[citadel.git] / webcit / gettext.c
blobe486fc4d94db9bed56fe12554abf75b86eb29f1a
1 /*
2 * $Id$
3 */
5 #include "webcit.h"
6 #include "webserver.h"
8 #ifdef ENABLE_NLS
9 /* actual supported locales */
10 const char *AvailLang[NUM_LANGS] = {
11 "C",
12 "en_US",
13 "de_DE",
14 "it_IT",
15 "es_ES",
16 "en_GB",
17 "da_DK",
18 "fr_FR",
19 "nl_NL",
20 "pt_BR"
23 const char *AvailLangLoaded[NUM_LANGS];
24 long nLocalesLoaded = 0;
26 #ifdef HAVE_USELOCALE
27 locale_t wc_locales[NUM_LANGS]; /**< here we keep the parsed stuff */
28 #endif
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? */
37 } LangStruct;
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:
41 * opera:
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
43 * Firefox
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];
53 LangStruct *ls;
55 int i = 0;
56 int j = 0;
57 /* size_t len = strlen(LocaleString); */
58 long prio;
59 int av;
60 int nBest;
61 int nParts;
62 StrBuf *Buf = NULL;
63 StrBuf *SBuf = NULL;
65 nParts=StrBufNum_tokens(LocaleString,',');
66 for (i=0; ((i<nParts)&&(i<SEARCH_LANG)); i++)
68 char lbuf[16];
69 int blen;
71 if (Buf == NULL) {
72 Buf = NewStrBuf();
73 SBuf = NewStrBuf();
75 else {
76 FlushStrBuf(Buf);
77 FlushStrBuf(SBuf);
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) {
85 int sbuflen, k;
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);
93 else {
94 ls->priority=1000;
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 */
118 ls->availability=1;
119 ls->selectedlang=-1;
120 for (j=0; j<nLocalesLoaded; j++) {
121 int result;
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;
126 ls->selectedlang=j;
128 /** match against lang and locale */
129 if (0==strcasecmp(&lbuf[0], AvailLangLoaded[j])){
130 ls->availability=0;
131 ls->selectedlang=j;
132 j=nLocalesLoaded;
137 prio=0;
138 av=-1000;
139 nBest=-1;
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;
147 av=ls->availability;
148 prio=ls->priority;
151 if (nBest == -1) {
152 /** fall back to C */
153 nBest=0;
155 WC->selected_language=nBest;
156 lprintf(9, "language found: %s\n", AvailLangLoaded[WC->selected_language]);
157 FreeStrBuf(&Buf);
158 FreeStrBuf(&SBuf);
162 * \brief show the language chooser on the login dialog
163 * depending on the browser locale change the sequence of the
164 * language chooser.
166 void tmplput_offer_languages(StrBuf *Target, WCTemplputParams *TP)
168 int i;
169 #ifndef HAVE_USELOCALE
170 char *Lang = getenv("LANG");
172 if (Lang == NULL)
173 Lang = "C";
174 #endif
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)
181 #endif
182 wprintf("<option %s value=%s>%s</option>\n",
183 ((WC->selected_language == i) ? "selected" : ""),
184 AvailLangLoaded[i],
185 AvailLangLoaded[i]
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) {
197 int i;
199 #ifdef HAVE_USELOCALE
200 for (i=0; i<nLocalesLoaded; ++i) {
201 if (!strcasecmp(lang, AvailLangLoaded[i])) {
202 WC->selected_language = i;
205 #endif
209 * \brief Activate the selected language for this session.
211 void go_selected_language(void) {
212 #ifdef HAVE_USELOCALE
213 wcsession *WCC = WC;
214 if (WCC->selected_language < 0) return;
215 uselocale(wc_locales[WCC->selected_language]); /** switch locales */
216 textdomain(textdomain(NULL)); /** clear the cache */
217 #else
218 char *language;
220 language = getenv("LANG");
221 setlocale(LC_MESSAGES, language);
222 #endif
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 */
232 #endif
235 void preset_locale(void)
237 #ifndef HAVE_USELOCALE
238 #ifdef HAVE_GETTEXT
239 char *language;
241 lprintf(9, "Nailing locale to %s\n", getenv("LANG"));
242 language = getenv("LANG");
243 setlocale(LC_MESSAGES, language);
244 #endif
245 #endif
248 #ifdef HAVE_USELOCALE
249 locale_t Empty_Locale;
250 #endif
253 * \brief Create a locale_t for each available language
255 void initialize_locales(void) {
256 int i;
257 char buf[32];
259 #ifdef HAVE_USELOCALE
260 /* create default locale */
261 Empty_Locale = newlocale(LC_ALL_MASK, NULL, NULL);
262 #endif
264 for (i = 0; i < NUM_LANGS; ++i) {
265 if (i == 0) {
266 sprintf(buf, "%s", AvailLang[i]); /* locale 0 (C) is ascii, not utf-8 */
268 else {
269 sprintf(buf, "%s.UTF8", AvailLang[i]);
271 #ifdef HAVE_USELOCALE
272 wc_locales[nLocalesLoaded] = newlocale(
273 (LC_MESSAGES_MASK|LC_TIME_MASK),
274 buf,
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",
279 buf,
280 strerror(errno)
283 else {
284 lprintf(3, "Configured available locale: %s\n", buf);
285 AvailLangLoaded[nLocalesLoaded] = AvailLang[i];
286 nLocalesLoaded++;
288 #endif
293 void ShutdownLocale(void)
295 int i;
296 #ifdef HAVE_USELOCALE
297 for (i = 0; i < nLocalesLoaded; ++i) {
298 if (Empty_Locale != wc_locales[i])
299 freelocale(wc_locales[i]);
301 #endif
304 #else /* ENABLE_NLS */
305 const char *AvailLang[NUM_LANGS] = {
306 "C"};
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) {
343 #ifdef ENABLE_NLS
344 #ifdef HAVE_USELOCALE
345 return AvailLang[WC->selected_language];
346 #else
347 return "en"
348 #endif
349 #else
350 return "en"
351 #endif
354 void
355 InitModule_GETTEXT
356 (void)
358 RegisterNamespace("LANG:SELECT", 0, 0, tmplput_offer_languages, CTX_NONE);