From c12ce55b25685738bc1668df7b7bde87c4ba023c Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 19 Oct 2007 15:58:38 +0000 Subject: Factored utf8 switching code out to i18n.c. --- common/i18n.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'common/i18n.c') diff --git a/common/i18n.c b/common/i18n.c index 1592994d4..3694947a9 100644 --- a/common/i18n.c +++ b/common/i18n.c @@ -18,9 +18,17 @@ */ #include +#ifdef HAVE_LOCALE_H +#include +#endif +#ifdef HAVE_LANGINFO_CODESET +#include +#endif +#include "util.h" #include "i18n.h" + void i18n_init (void) { @@ -35,3 +43,59 @@ i18n_init (void) #endif } + +/* The Assuan agent protocol requires us to transmit utf-8 strings + thus we need a fucntion to temporary switch gettext from native to + utf8. */ +char * +i18n_switchto_utf8 (void) +{ +#ifdef ENABLE_NLS + char *orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL); +#ifdef HAVE_LANGINFO_CODESET + if (!orig_codeset) + orig_codeset = nl_langinfo (CODESET); +#endif + if (orig_codeset) + { /* We only switch when we are able to restore the codeset later. + Note that bind_textdomain_codeset does only return on memory + errors but not if a codeset is not available. Thus we don't + bother printing a diagnostic here. */ + orig_codeset = xstrdup (orig_codeset); + if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8")) + { + xfree (orig_codeset); + orig_codeset = NULL; + } + } + return orig_codeset; +#else + return NULL; +#endif +} + +/* Switch back to the saved codeset. */ +void +i18n_switchback (char *saved_codeset) +{ +#ifdef ENABLE_NLS + if (saved_codeset) + { + bind_textdomain_codeset (PACKAGE_GT, saved_codeset); + xfree (saved_codeset); + } +#else + (void)saved_codeset; +#endif +} + + +/* Gettext variant which temporary switches to utf-8 for string. */ +const char * +i18n_utf8 (const char *string) +{ + char *saved = i18n_switchto_utf8 (); + const char *result = _(string); + i18n_switchback (saved); + return result; +} -- cgit v1.2.3