2008-02-09 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / i18n.c
blobc13be8608ff5564f756fbcbf8fa996428453377b
1 /* i18n.c - gettext initialization
2 * Copyright (C) 2007 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #ifdef HAVE_LOCALE_H
22 #include <locale.h>
23 #endif
24 #ifdef HAVE_LANGINFO_CODESET
25 #include <langinfo.h>
26 #endif
28 #include "util.h"
29 #include "i18n.h"
32 void
33 i18n_init (void)
35 #ifdef USE_SIMPLE_GETTEXT
36 set_gettext_file (PACKAGE_GT, "Software\\GNU\\GnuPG");
37 #else
38 # ifdef ENABLE_NLS
39 setlocale (LC_ALL, "" );
40 bindtextdomain (PACKAGE_GT, LOCALEDIR);
41 textdomain (PACKAGE_GT);
42 # endif
43 #endif
47 /* The Assuan agent protocol requires us to transmit utf-8 strings
48 thus we need a fuctnion to temporary switch gettext from native to
49 utf8. */
50 char *
51 i18n_switchto_utf8 (void)
53 #ifdef ENABLE_NLS
54 char *orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL);
55 #ifdef HAVE_LANGINFO_CODESET
56 if (!orig_codeset)
57 orig_codeset = nl_langinfo (CODESET);
58 #endif
59 if (orig_codeset)
60 { /* We only switch when we are able to restore the codeset later.
61 Note that bind_textdomain_codeset does only return on memory
62 errors but not if a codeset is not available. Thus we don't
63 bother printing a diagnostic here. */
64 orig_codeset = xstrdup (orig_codeset);
65 if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8"))
67 xfree (orig_codeset);
68 orig_codeset = NULL;
71 return orig_codeset;
72 #else
73 return NULL;
74 #endif
77 /* Switch back to the saved codeset. */
78 void
79 i18n_switchback (char *saved_codeset)
81 #ifdef ENABLE_NLS
82 if (saved_codeset)
84 bind_textdomain_codeset (PACKAGE_GT, saved_codeset);
85 xfree (saved_codeset);
87 #else
88 (void)saved_codeset;
89 #endif
93 /* Gettext variant which temporary switches to utf-8 for string. */
94 const char *
95 i18n_utf8 (const char *string)
97 char *saved = i18n_switchto_utf8 ();
98 const char *result = _(string);
99 i18n_switchback (saved);
100 return result;