gwenhywfar
4.3.3
|
00001 /*************************************************************************** 00002 $RCSfile$ 00003 ------------------- 00004 cvs : $Id$ 00005 begin : Fri Sep 12 2003 00006 copyright : (C) 2003 by Martin Preuss 00007 email : martin@libchipcard.de 00008 00009 *************************************************************************** 00010 * * 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of the GNU Lesser General Public * 00013 * License as published by the Free Software Foundation; either * 00014 * version 2.1 of the License, or (at your option) any later version. * 00015 * * 00016 * This library is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00019 * Lesser General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Lesser General Public * 00022 * License along with this library; if not, write to the Free Software * 00023 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * 00024 * MA 02111-1307 USA * 00025 * * 00026 ***************************************************************************/ 00027 00028 #ifdef HAVE_CONFIG_H 00029 # include <config.h> 00030 #endif 00031 00032 00033 #include "i18n_l.h" 00034 #include <gwenhywfar/debug.h> 00035 #include <gwenhywfar/misc.h> 00036 #include <gwenhywfar/pathmanager.h> 00037 #include <gwenhywfar/gwenhywfar.h> 00038 #include <stdio.h> 00039 #include <assert.h> 00040 #include <string.h> 00041 #include <errno.h> 00042 00043 #ifdef HAVE_STRINGS_H 00044 # include <strings.h> 00045 #endif 00046 00047 #ifdef HAVE_I18N 00048 # include <libintl.h> 00049 # include <locale.h> 00050 #endif 00051 00052 00053 static GWEN_STRINGLIST *gwen_i18n__localelist=0; 00054 static char *gwen_i18n__currentlocale=0; 00055 00056 00057 #ifdef OS_WIN32 00058 00059 struct gwen_i18n_tabletype { 00060 const char *win_name; 00061 const char *nls_name; 00062 }; 00063 00064 00065 static struct gwen_i18n_tabletype gwen_i18n___localetable[]={ 00066 { "German_Germany", "de_DE" }, 00067 { "English_UK", "en_GB" }, 00068 { "English_US", "en_US" }, 00069 { "French_France", "fr_FR" }, 00070 { NULL, NULL } 00071 }; 00072 00073 00074 00075 static const char *gwen_i18n_transwinlocale(const char *s) { 00076 char *cs; 00077 char *p; 00078 struct gwen_i18n_tabletype *tt; 00079 00080 cs=strdup(s); 00081 00082 /* find complete */ 00083 tt=gwen_i18n___localetable; 00084 while(tt->win_name) { 00085 if (strcasecmp(tt->win_name, cs)==0) { 00086 free(cs); 00087 return tt->nls_name; 00088 } 00089 tt++; 00090 } 00091 00092 p=strrchr(cs, '.'); 00093 if (p) { 00094 *p=0; 00095 /* find partial string */ 00096 tt=gwen_i18n___localetable; 00097 while(tt->win_name) { 00098 if (strcasecmp(tt->win_name, cs)==0) { 00099 free(cs); 00100 return tt->nls_name; 00101 } 00102 tt++; 00103 } 00104 } 00105 00106 p=strrchr(cs, '_'); 00107 if (p) { 00108 *p=0; 00109 /* find partial string */ 00110 tt=gwen_i18n___localetable; 00111 while(tt->win_name) { 00112 if (strcasecmp(tt->win_name, cs)==0) { 00113 free(cs); 00114 return tt->nls_name; 00115 } 00116 tt++; 00117 } 00118 } 00119 free(cs); 00120 DBG_ERROR(GWEN_LOGDOMAIN, "No translation found for WIN32 locale [%s]", s); 00121 return s; 00122 } 00123 00124 00125 #endif 00126 00127 00128 00129 int GWEN_I18N_ModuleInit(void){ 00130 const char *localedir; 00131 GWEN_STRINGLIST *slist; 00132 00133 gwen_i18n__localelist=GWEN_StringList_new(); 00134 00135 slist=GWEN_PathManager_GetPaths(GWEN_PM_LIBNAME, GWEN_PM_LOCALEDIR); 00136 if (slist) { 00137 if (GWEN_StringList_Count(slist) > 0) { 00138 int rv; 00139 00140 localedir=GWEN_StringList_FirstString(slist); 00141 rv=GWEN_I18N_BindTextDomain_Dir(PACKAGE, localedir); 00142 if (rv) { 00143 DBG_WARN(GWEN_LOGDOMAIN, "Could not bind textdomain (%d)", rv); 00144 } 00145 else { 00146 rv=GWEN_I18N_BindTextDomain_Codeset(PACKAGE, "UTF-8"); 00147 if (rv) { 00148 DBG_WARN(GWEN_LOGDOMAIN, "Could not set codeset (%d)", rv); 00149 } 00150 } 00151 00152 /* set locale */ 00153 if (GWEN_I18N_SetLocale("")) { 00154 DBG_ERROR(GWEN_LOGDOMAIN, "Could not set locale"); 00155 } 00156 } 00157 else { 00158 DBG_ERROR(GWEN_LOGDOMAIN, "Empty locale path list"); 00159 } 00160 GWEN_StringList_free(slist); 00161 } 00162 else { 00163 DBG_ERROR(GWEN_LOGDOMAIN, "No locale path list"); 00164 } 00165 return 0; 00166 } 00167 00168 00169 00170 int GWEN_I18N_ModuleFini(void){ 00171 GWEN_StringList_free(gwen_i18n__localelist); 00172 free(gwen_i18n__currentlocale); 00173 return 0; 00174 } 00175 00176 00177 00178 int GWEN_I18N_SetLocale(const char *s){ 00179 const char *realLocale; 00180 char *p; 00181 char *cs; 00182 00183 assert(s); 00184 00185 #ifdef HAVE_I18N 00186 realLocale=setlocale(LC_ALL, s); 00187 if (realLocale==NULL) { 00188 DBG_INFO(GWEN_LOGDOMAIN, "Unable to set locale [%s]", s); 00189 realLocale=s; 00190 } 00191 else { 00192 #ifdef OS_WIN32 00193 const char *t; 00194 00195 t=gwen_i18n_transwinlocale(realLocale); 00196 DBG_INFO(GWEN_LOGDOMAIN, "Real locale is [%s] (from [%s])", t, realLocale); 00197 realLocale=t; 00198 #else 00199 DBG_INFO(GWEN_LOGDOMAIN, "Real locale is [%s]", realLocale); 00200 #endif 00201 } 00202 #else 00203 realLocale=s; 00204 #endif 00205 00206 cs=strdup(realLocale); 00207 GWEN_StringList_Clear(gwen_i18n__localelist); 00208 GWEN_StringList_AppendString(gwen_i18n__localelist, cs, 0, 1); 00209 00210 p=strrchr(cs, '@'); 00211 if (p) { 00212 *p=0; 00213 GWEN_StringList_AppendString(gwen_i18n__localelist, cs, 0, 1); 00214 } 00215 p=strrchr(cs, '.'); 00216 if (p) { 00217 *p=0; 00218 GWEN_StringList_AppendString(gwen_i18n__localelist, cs, 0, 1); 00219 } 00220 00221 p=strrchr(cs, '_'); 00222 if (p) { 00223 *p=0; 00224 GWEN_StringList_AppendString(gwen_i18n__localelist, cs, 0, 1); 00225 } 00226 free(cs); 00227 00228 free(gwen_i18n__currentlocale); 00229 gwen_i18n__currentlocale=strdup(realLocale); 00230 return 0; 00231 } 00232 00233 00234 00235 GWEN_STRINGLIST *GWEN_I18N_GetCurrentLocaleList(void){ 00236 return gwen_i18n__localelist; 00237 } 00238 00239 00240 00241 const char *GWEN_I18N_GetCurrentLocale(void) { 00242 return gwen_i18n__currentlocale; 00243 } 00244 00245 00246 00247 const char *GWEN_I18N_Translate(const char *txtdom, const char *text) { 00248 #ifdef HAVE_I18N 00249 const char *p; 00250 00251 p=strchr(text, '|'); 00252 if (p) { 00253 const char *s; 00254 00255 s=dgettext(txtdom, text); 00256 if (strcmp(s, text)==0) 00257 return ++p; 00258 else 00259 return s; 00260 } 00261 else 00262 return dgettext(txtdom, text); 00263 #else 00264 const char *p; 00265 00266 p=strchr(text, '|'); 00267 if (p) 00268 return ++p; 00269 return text; 00270 #endif 00271 } 00272 00273 00274 00275 int GWEN_I18N_BindTextDomain_Dir(const char *txtdom, const char *folder) { 00276 #ifdef HAVE_I18N 00277 if (NULL==bindtextdomain(txtdom, folder)) { 00278 DBG_INFO(GWEN_LOGDOMAIN, "bindtextdomain(): %s", strerror(errno)); 00279 return GWEN_ERROR_GENERIC; 00280 } 00281 return 0; 00282 #else 00283 return GWEN_ERROR_NOT_SUPPORTED; 00284 #endif 00285 } 00286 00287 00288 00289 int GWEN_I18N_BindTextDomain_Codeset(const char *txtdom, const char *cs) { 00290 #ifdef HAVE_I18N 00291 if (NULL==bind_textdomain_codeset(txtdom, cs)) { 00292 DBG_INFO(GWEN_LOGDOMAIN, "bind_textdomain_codeset(): %s", strerror(errno)); 00293 return GWEN_ERROR_GENERIC; 00294 } 00295 return 0; 00296 #else 00297 return GWEN_ERROR_NOT_SUPPORTED; 00298 #endif 00299 } 00300 00301 00302 00303 00304 00305 00306 00307