diff options
author | Werner Koch <[email protected]> | 2007-10-19 15:58:38 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2007-10-19 15:58:38 +0000 |
commit | c12ce55b25685738bc1668df7b7bde87c4ba023c (patch) | |
tree | a863f6912c1d113edc361ee47d69e3135fe703b0 /common | |
parent | Enhanced gpg-conect-agent scripting. (diff) | |
download | gnupg-c12ce55b25685738bc1668df7b7bde87c4ba023c.tar.gz gnupg-c12ce55b25685738bc1668df7b7bde87c4ba023c.zip |
Factored utf8 switching code out to i18n.c.
Diffstat (limited to 'common')
-rw-r--r-- | common/ChangeLog | 4 | ||||
-rw-r--r-- | common/i18n.c | 64 | ||||
-rw-r--r-- | common/i18n.h | 3 |
3 files changed, 71 insertions, 0 deletions
diff --git a/common/ChangeLog b/common/ChangeLog index 577367292..b7c583797 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -1,3 +1,7 @@ +2007-10-19 Werner Koch <[email protected]> + + * i18n.c (i18n_switchto_utf8, i18n_switchback): New. + 2007-10-01 Werner Koch <[email protected]> * sysutils.h (FD2INT, INT2FD): New. 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 <config.h> +#ifdef HAVE_LOCALE_H +#include <locale.h> +#endif +#ifdef HAVE_LANGINFO_CODESET +#include <langinfo.h> +#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; +} diff --git a/common/i18n.h b/common/i18n.h index b692feaa7..7405f9a55 100644 --- a/common/i18n.h +++ b/common/i18n.h @@ -39,6 +39,9 @@ #endif /*!USE_SIMPLE_GETTEXT*/ void i18n_init (void); +char *i18n_switchto_utf8 (void); +void i18n_switchback (char *saved_codeset); +const char *i18n_utf8 (const char *string); #endif /*GNUPG_COMMON_I18N_H*/ |