gwenhywfar  4.6.0beta
i18n.c
Go to the documentation of this file.
1 /***************************************************************************
2  $RCSfile$
3  -------------------
4  cvs : $Id$
5  begin : Fri Sep 12 2003
6  copyright : (C) 2003 by Martin Preuss
7  email : martin@libchipcard.de
8 
9  ***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
24  * MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 
33 #include "i18n_l.h"
34 #include <gwenhywfar/debug.h>
35 #include <gwenhywfar/misc.h>
36 #include <gwenhywfar/pathmanager.h>
37 #include <gwenhywfar/gwenhywfar.h>
38 #include <stdio.h>
39 #include <assert.h>
40 #include <string.h>
41 #include <errno.h>
42 
43 #ifdef HAVE_STRINGS_H
44 # include <strings.h>
45 #endif
46 
47 #ifdef HAVE_I18N
48 # include <libintl.h>
49 # include <locale.h>
50 #endif
51 
52 
54 static char *gwen_i18n__currentlocale=0;
55 
56 
57 #ifdef OS_WIN32
58 
59 struct gwen_i18n_tabletype {
60  const char *win_name;
61  const char *nls_name;
62 };
63 
64 
65 static struct gwen_i18n_tabletype gwen_i18n___localetable[]={
66 { "German_Germany", "de_DE" },
67 { "English_UK", "en_GB" },
68 { "English_US", "en_US" },
69 { "French_France", "fr_FR" },
70 { NULL, NULL }
71 };
72 
73 
74 
75 static const char *gwen_i18n_transwinlocale(const char *s) {
76  char *cs;
77  char *p;
78  struct gwen_i18n_tabletype *tt;
79 
80  cs=strdup(s);
81 
82  /* find complete */
83  tt=gwen_i18n___localetable;
84  while(tt->win_name) {
85  if (strcasecmp(tt->win_name, cs)==0) {
86  free(cs);
87  return tt->nls_name;
88  }
89  tt++;
90  }
91 
92  p=strrchr(cs, '.');
93  if (p) {
94  *p=0;
95  /* find partial string */
96  tt=gwen_i18n___localetable;
97  while(tt->win_name) {
98  if (strcasecmp(tt->win_name, cs)==0) {
99  free(cs);
100  return tt->nls_name;
101  }
102  tt++;
103  }
104  }
105 
106  p=strrchr(cs, '_');
107  if (p) {
108  *p=0;
109  /* find partial string */
110  tt=gwen_i18n___localetable;
111  while(tt->win_name) {
112  if (strcasecmp(tt->win_name, cs)==0) {
113  free(cs);
114  return tt->nls_name;
115  }
116  tt++;
117  }
118  }
119  free(cs);
120  DBG_ERROR(GWEN_LOGDOMAIN, "No translation found for WIN32 locale [%s]", s);
121  return s;
122 }
123 
124 
125 #endif
126 
127 
128 
130  const char *localedir;
131  GWEN_STRINGLIST *slist;
132 
134 
136  if (slist) {
137  if (GWEN_StringList_Count(slist) > 0) {
138  int rv;
139 
140  localedir=GWEN_StringList_FirstString(slist);
141  rv=GWEN_I18N_BindTextDomain_Dir(PACKAGE, localedir);
142  if (rv) {
143  DBG_WARN(GWEN_LOGDOMAIN, "Could not bind textdomain (%d)", rv);
144  }
145  else {
146  rv=GWEN_I18N_BindTextDomain_Codeset(PACKAGE, "UTF-8");
147  if (rv) {
148  DBG_WARN(GWEN_LOGDOMAIN, "Could not set codeset (%d)", rv);
149  }
150  }
151 
152  /* set locale */
153  if (GWEN_I18N_SetLocale("")) {
154  DBG_ERROR(GWEN_LOGDOMAIN, "Could not set locale");
155  }
156  }
157  else {
158  DBG_ERROR(GWEN_LOGDOMAIN, "Empty locale path list");
159  }
160  GWEN_StringList_free(slist);
161  }
162  else {
163  DBG_ERROR(GWEN_LOGDOMAIN, "No locale path list");
164  }
165  return 0;
166 }
167 
168 
169 
173  return 0;
174 }
175 
176 
177 
178 int GWEN_I18N_SetLocale(const char *s){
179  const char *realLocale;
180  char *p;
181  char *cs;
182 
183  assert(s);
184 
185 #ifdef HAVE_I18N
186  realLocale=setlocale(LC_ALL, s);
187  if (realLocale==NULL) {
188  DBG_INFO(GWEN_LOGDOMAIN, "Unable to set locale [%s]", s);
189  realLocale=s;
190  }
191  else {
192 #ifdef OS_WIN32
193  const char *t;
194 
195  t=gwen_i18n_transwinlocale(realLocale);
196  DBG_INFO(GWEN_LOGDOMAIN, "Real locale is [%s] (from [%s])", t, realLocale);
197  realLocale=t;
198 #else
199  DBG_INFO(GWEN_LOGDOMAIN, "Real locale is [%s]", realLocale);
200 #endif
201  }
202 #else
203  realLocale=s;
204 #endif
205 
206  cs=strdup(realLocale);
209 
210  p=strrchr(cs, '@');
211  if (p) {
212  *p=0;
214  }
215  p=strrchr(cs, '.');
216  if (p) {
217  *p=0;
219  }
220 
221  p=strrchr(cs, '_');
222  if (p) {
223  *p=0;
225  }
226  free(cs);
227 
229  gwen_i18n__currentlocale=strdup(realLocale);
230  return 0;
231 }
232 
233 
234 
236  return gwen_i18n__localelist;
237 }
238 
239 
240 
241 const char *GWEN_I18N_GetCurrentLocale(void) {
243 }
244 
245 
246 
247 const char *GWEN_I18N_Translate(const char *txtdom, const char *text) {
248 #ifdef HAVE_I18N
249  const char *p;
250 
251  p=strchr(text, '|');
252  if (p) {
253  const char *s;
254 
255  s=dgettext(txtdom, text);
256  if (strcmp(s, text)==0)
257  return ++p;
258  else
259  return s;
260  }
261  else
262  return dgettext(txtdom, text);
263 #else
264  const char *p;
265 
266  p=strchr(text, '|');
267  if (p)
268  return ++p;
269  return text;
270 #endif
271 }
272 
273 
274 
275 int GWEN_I18N_BindTextDomain_Dir(const char *txtdom, const char *folder) {
276 #ifdef HAVE_I18N
277  if (NULL==bindtextdomain(txtdom, folder)) {
278  DBG_INFO(GWEN_LOGDOMAIN, "bindtextdomain(): %s", strerror(errno));
279  return GWEN_ERROR_GENERIC;
280  }
281  return 0;
282 #else
284 #endif
285 }
286 
287 
288 
289 int GWEN_I18N_BindTextDomain_Codeset(const char *txtdom, const char *cs) {
290 #ifdef HAVE_I18N
291  if (NULL==bind_textdomain_codeset(txtdom, cs)) {
292  DBG_INFO(GWEN_LOGDOMAIN, "bind_textdomain_codeset(): %s", strerror(errno));
293  return GWEN_ERROR_GENERIC;
294  }
295  return 0;
296 #else
298 #endif
299 }
300 
301 
302 
303 
304 
305 
306 
307