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