00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00058 int GWEN_I18N_ModuleInit(){
00059 const char *localedir;
00060 GWEN_STRINGLIST *slist;
00061
00062 gwen_i18n__localelist=GWEN_StringList_new();
00063
00064 slist=GWEN_PathManager_GetPaths(GWEN_PM_LIBNAME, GWEN_PM_LOCALEDIR);
00065 if (slist) {
00066 if (GWEN_StringList_Count(slist) > 0) {
00067 int rv;
00068
00069 localedir=GWEN_StringList_FirstString(slist);
00070 rv=GWEN_I18N_BindTextDomain_Dir(PACKAGE, localedir);
00071 if (rv) {
00072 DBG_WARN(GWEN_LOGDOMAIN, "Could not bind textdomain (%d)", rv);
00073 }
00074 else {
00075 rv=GWEN_I18N_BindTextDomain_Codeset(PACKAGE, "UTF-8");
00076 if (rv) {
00077 DBG_WARN(GWEN_LOGDOMAIN, "Could not set codeset (%d)", rv);
00078 }
00079 }
00080
00081
00082 if (GWEN_I18N_SetLocale("")) {
00083 DBG_ERROR(GWEN_LOGDOMAIN, "Could not set locale");
00084 }
00085 }
00086 else {
00087 DBG_ERROR(GWEN_LOGDOMAIN, "Empty locale path list");
00088 }
00089 GWEN_StringList_free(slist);
00090 }
00091 else {
00092 DBG_ERROR(GWEN_LOGDOMAIN, "No locale path list");
00093 }
00094 return 0;
00095 }
00096
00097
00098
00099 int GWEN_I18N_ModuleFini(){
00100 GWEN_StringList_free(gwen_i18n__localelist);
00101 free(gwen_i18n__currentlocale);
00102 return 0;
00103 }
00104
00105
00106
00107 int GWEN_I18N_SetLocale(const char *s){
00108 const char *realLocale;
00109 char *p;
00110 char *cs;
00111
00112 assert(s);
00113
00114 #ifdef HAVE_I18N
00115 realLocale=setlocale(LC_ALL, s);
00116 if (realLocale==NULL) {
00117 DBG_INFO(GWEN_LOGDOMAIN, "Unable to set locale [%s]", s);
00118 realLocale=s;
00119 }
00120 else {
00121 DBG_INFO(GWEN_LOGDOMAIN, "Real locale is [%s]", realLocale);
00122 }
00123 #else
00124 realLocale=s;
00125 #endif
00126
00127 cs=strdup(realLocale);
00128 GWEN_StringList_Clear(gwen_i18n__localelist);
00129 GWEN_StringList_AppendString(gwen_i18n__localelist, cs, 0, 1);
00130
00131 p=strrchr(cs, '@');
00132 if (p) {
00133 *p=0;
00134 GWEN_StringList_AppendString(gwen_i18n__localelist, cs, 0, 1);
00135
00136 }
00137 p=strrchr(cs, '_');
00138 if (p) {
00139 *p=0;
00140 GWEN_StringList_AppendString(gwen_i18n__localelist, cs, 0, 1);
00141
00142 }
00143 free(cs);
00144
00145 free(gwen_i18n__currentlocale);
00146 gwen_i18n__currentlocale=strdup(realLocale);
00147 return 0;
00148 }
00149
00150
00151
00152 GWEN_STRINGLIST *GWEN_I18N_GetCurrentLocaleList(){
00153 return gwen_i18n__localelist;
00154 }
00155
00156
00157
00158 const char *GWEN_I18N_GetCurrentLocale() {
00159 return gwen_i18n__currentlocale;
00160 }
00161
00162
00163
00164 const char *GWEN_I18N_Translate(const char *textdomain, const char *text) {
00165 #ifdef HAVE_I18N
00166 return dgettext(textdomain, text);
00167 #else
00168 return text;
00169 #endif
00170 }
00171
00172
00173
00174 int GWEN_I18N_BindTextDomain_Dir(const char *textdomain, const char *folder) {
00175 #ifdef HAVE_I18N
00176 if (NULL==bindtextdomain(textdomain, folder)) {
00177 DBG_INFO(GWEN_LOGDOMAIN, "bindtextdomain(): %s", strerror(errno));
00178 return GWEN_ERROR_GENERIC;
00179 }
00180 return 0;
00181 #else
00182 return GWEN_ERROR_NOT_SUPPORTED;
00183 #endif
00184 }
00185
00186
00187
00188 int GWEN_I18N_BindTextDomain_Codeset(const char *textdomain, const char *cs) {
00189 #ifdef HAVE_I18N
00190 if (NULL==bind_textdomain_codeset(textdomain, cs)) {
00191 DBG_INFO(GWEN_LOGDOMAIN, "bind_textdomain_codeset(): %s", strerror(errno));
00192 return GWEN_ERROR_GENERIC;
00193 }
00194 return 0;
00195 #else
00196 return GWEN_ERROR_NOT_SUPPORTED;
00197 #endif
00198 }
00199
00200
00201
00202
00203
00204
00205
00206