gwenhywfar  4.99.15beta
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 {
77  char *cs;
78  char *p;
79  struct gwen_i18n_tabletype *tt;
80 
81  cs=strdup(s);
82 
83  /* find complete */
84  tt=gwen_i18n___localetable;
85  while (tt->win_name) {
86  if (strcasecmp(tt->win_name, cs)==0) {
87  free(cs);
88  return tt->nls_name;
89  }
90  tt++;
91  }
92 
93  p=strrchr(cs, '.');
94  if (p) {
95  *p=0;
96  /* find partial string */
97  tt=gwen_i18n___localetable;
98  while (tt->win_name) {
99  if (strcasecmp(tt->win_name, cs)==0) {
100  free(cs);
101  return tt->nls_name;
102  }
103  tt++;
104  }
105  }
106 
107  p=strrchr(cs, '_');
108  if (p) {
109  *p=0;
110  /* find partial string */
111  tt=gwen_i18n___localetable;
112  while (tt->win_name) {
113  if (strcasecmp(tt->win_name, cs)==0) {
114  free(cs);
115  return tt->nls_name;
116  }
117  tt++;
118  }
119  }
120  free(cs);
121  DBG_ERROR(GWEN_LOGDOMAIN, "No translation found for WIN32 locale [%s]", s);
122  return s;
123 }
124 
125 
126 #endif
127 
128 
129 
131 {
132  const char *localedir;
133  GWEN_STRINGLIST *slist;
134 
136 
138  if (slist) {
139  if (GWEN_StringList_Count(slist) > 0) {
140  int rv;
141 
142  localedir=GWEN_StringList_FirstString(slist);
143  rv=GWEN_I18N_BindTextDomain_Dir(PACKAGE, localedir);
144  if (rv) {
145  DBG_WARN(GWEN_LOGDOMAIN, "Could not bind textdomain (%d)", rv);
146  }
147  else {
148  rv=GWEN_I18N_BindTextDomain_Codeset(PACKAGE, "UTF-8");
149  if (rv) {
150  DBG_WARN(GWEN_LOGDOMAIN, "Could not set codeset (%d)", rv);
151  }
152  }
153 
154  /* set locale */
155  if (GWEN_I18N_SetLocale("")) {
156  DBG_ERROR(GWEN_LOGDOMAIN, "Could not set locale");
157  }
158  }
159  else {
160  DBG_ERROR(GWEN_LOGDOMAIN, "Empty locale path list");
161  }
162  GWEN_StringList_free(slist);
163  }
164  else {
165  DBG_ERROR(GWEN_LOGDOMAIN, "No locale path list");
166  }
167  return 0;
168 }
169 
170 
171 
173 {
176  return 0;
177 }
178 
179 
180 
181 int GWEN_I18N_SetLocale(const char *s)
182 {
183  const char *realLocale;
184  char *p;
185  char *cs;
186 
187  assert(s);
188 
189 #ifdef HAVE_I18N
190  realLocale=setlocale(LC_ALL, s);
191  if (realLocale==NULL) {
192  DBG_INFO(GWEN_LOGDOMAIN, "Unable to set locale [%s]", s);
193  realLocale=s;
194  }
195  else {
196 #ifdef OS_WIN32
197  const char *t;
198 
199  t=gwen_i18n_transwinlocale(realLocale);
200  DBG_INFO(GWEN_LOGDOMAIN, "Real locale is [%s] (from [%s])", t, realLocale);
201  realLocale=t;
202 #else
203  DBG_INFO(GWEN_LOGDOMAIN, "Real locale is [%s]", realLocale);
204 #endif
205  }
206 #else
207  realLocale=s;
208 #endif
209 
210  cs=strdup(realLocale);
213 
214  p=strrchr(cs, '@');
215  if (p) {
216  *p=0;
218  }
219  p=strrchr(cs, '.');
220  if (p) {
221  *p=0;
223  }
224 
225  p=strrchr(cs, '_');
226  if (p) {
227  *p=0;
229  }
230  free(cs);
231 
233  gwen_i18n__currentlocale=strdup(realLocale);
234  return 0;
235 }
236 
237 
238 
240 {
241  return gwen_i18n__localelist;
242 }
243 
244 
245 
246 const char *GWEN_I18N_GetCurrentLocale(void)
247 {
249 }
250 
251 
252 
253 const char *GWEN_I18N_Translate(const char *txtdom, const char *text)
254 {
255 #ifdef HAVE_I18N
256  const char *p;
257 
258  p=strchr(text, '|');
259  if (p) {
260  const char *s;
261 
262  s=dgettext(txtdom, text);
263  if (strcmp(s, text)==0)
264  return ++p;
265  else
266  return s;
267  }
268  else
269  return dgettext(txtdom, text);
270 #else
271  const char *p;
272 
273  p=strchr(text, '|');
274  if (p)
275  return ++p;
276  return text;
277 #endif
278 }
279 
280 
281 
282 int GWEN_I18N_BindTextDomain_Dir(const char *txtdom, const char *folder)
283 {
284 #ifdef HAVE_I18N
285  if (NULL==bindtextdomain(txtdom, folder)) {
286  DBG_INFO(GWEN_LOGDOMAIN, "bindtextdomain(): %s", strerror(errno));
287  return GWEN_ERROR_GENERIC;
288  }
289  return 0;
290 #else
292 #endif
293 }
294 
295 
296 
297 int GWEN_I18N_BindTextDomain_Codeset(const char *txtdom, const char *cs)
298 {
299 #ifdef HAVE_I18N
300  if (NULL==bind_textdomain_codeset(txtdom, cs)) {
301  DBG_INFO(GWEN_LOGDOMAIN, "bind_textdomain_codeset(): %s", strerror(errno));
302  return GWEN_ERROR_GENERIC;
303  }
304  return 0;
305 #else
307 #endif
308 }
309 
310 
311 
312 
313 
314 
315 
316 
int GWEN_I18N_SetLocale(const char *s)
Definition: i18n.c:181
void GWEN_StringList_Clear(GWEN_STRINGLIST *sl)
Definition: stringlist.c:224
#define NULL
Definition: binreloc.c:297
#define DBG_WARN(dbg_logger, format, args...)
Definition: debug.h:123
#define GWEN_LOGDOMAIN
Definition: logger.h:35
static char * gwen_i18n__currentlocale
Definition: i18n.c:54
int GWEN_I18N_ModuleInit(void)
Definition: i18n.c:130
void GWEN_StringList_free(GWEN_STRINGLIST *sl)
Definition: stringlist.c:58
int GWEN_I18N_ModuleFini(void)
Definition: i18n.c:172
int GWEN_I18N_BindTextDomain_Codeset(const char *txtdom, const char *cs)
Definition: i18n.c:297
int GWEN_StringList_AppendString(GWEN_STRINGLIST *sl, const char *s, int take, int checkDouble)
Definition: stringlist.c:241
int GWEN_I18N_BindTextDomain_Dir(const char *txtdom, const char *folder)
Definition: i18n.c:282
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:54
#define GWEN_ERROR_GENERIC
Definition: error.h:62
GWEN_STRINGLIST * GWEN_PathManager_GetPaths(const char *destLib, const char *pathName)
Definition: pathmanager.c:494
unsigned int GWEN_StringList_Count(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:403
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
const char * GWEN_StringList_FirstString(const GWEN_STRINGLIST *l)
Definition: stringlist.c:552
#define GWEN_ERROR_NOT_SUPPORTED
Definition: error.h:109
static GWEN_STRINGLIST * gwen_i18n__localelist
Definition: i18n.c:53
const char * GWEN_I18N_Translate(const char *txtdom, const char *text)
Definition: i18n.c:253
#define GWEN_PM_LOCALEDIR
Definition: gwenhywfar.h:49
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:177
#define GWEN_PM_LIBNAME
Definition: gwenhywfar.h:42
GWEN_STRINGLIST * GWEN_I18N_GetCurrentLocaleList(void)
Definition: i18n.c:239
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:46
const char * GWEN_I18N_GetCurrentLocale(void)
Definition: i18n.c:246