i18n.c
Go to the documentation of this file.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 *txtdom, const char *text) {
00165 #ifdef HAVE_I18N
00166 const char *p;
00167
00168 p=strchr(text, '|');
00169 if (p) {
00170 const char *s;
00171
00172 s=dgettext(txtdom, text);
00173 if (strcmp(s, text)==0)
00174 return ++p;
00175 else
00176 return s;
00177 }
00178 else
00179 return dgettext(txtdom, text);
00180 #else
00181 const char *p;
00182
00183 p=strchr(text, '|');
00184 if (p)
00185 return ++p;
00186 return text;
00187 #endif
00188 }
00189
00190
00191
00192 int GWEN_I18N_BindTextDomain_Dir(const char *txtdom, const char *folder) {
00193 #ifdef HAVE_I18N
00194 if (NULL==bindtextdomain(txtdom, folder)) {
00195 DBG_INFO(GWEN_LOGDOMAIN, "bindtextdomain(): %s", strerror(errno));
00196 return GWEN_ERROR_GENERIC;
00197 }
00198 return 0;
00199 #else
00200 return GWEN_ERROR_NOT_SUPPORTED;
00201 #endif
00202 }
00203
00204
00205
00206 int GWEN_I18N_BindTextDomain_Codeset(const char *txtdom, const char *cs) {
00207 #ifdef HAVE_I18N
00208 if (NULL==bind_textdomain_codeset(txtdom, cs)) {
00209 DBG_INFO(GWEN_LOGDOMAIN, "bind_textdomain_codeset(): %s", strerror(errno));
00210 return GWEN_ERROR_GENERIC;
00211 }
00212 return 0;
00213 #else
00214 return GWEN_ERROR_NOT_SUPPORTED;
00215 #endif
00216 }
00217
00218
00219
00220
00221
00222
00223
00224