Updated the german translation
[gnupg.git] / common / i18n.c
blobdb5ddf5f8d542662da5d42b7a4f6fd046beba5e4
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 bindtextdomain (PACKAGE_GT, gnupg_localedir ());
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 way to temporary switch gettext from native to
49 utf8. */
50 char *
51 i18n_switchto_utf8 (void)
53 #ifdef USE_SIMPLE_GETTEXT
54 gettext_select_utf8 (1);
55 return NULL;
56 #elif defined(ENABLE_NLS)
57 char *orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL);
58 # ifdef HAVE_LANGINFO_CODESET
59 if (!orig_codeset)
60 orig_codeset = nl_langinfo (CODESET);
61 # endif
62 if (orig_codeset)
63 { /* We only switch when we are able to restore the codeset later.
64 Note that bind_textdomain_codeset does only return on memory
65 errors but not if a codeset is not available. Thus we don't
66 bother printing a diagnostic here. */
67 orig_codeset = xstrdup (orig_codeset);
68 if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8"))
70 xfree (orig_codeset);
71 orig_codeset = NULL;
74 return orig_codeset;
75 #else
76 return NULL;
77 #endif
80 /* Switch back to the saved codeset. */
81 void
82 i18n_switchback (char *saved_codeset)
84 #ifdef USE_SIMPLE_GETTEXT
85 (void)saved_codeset;
86 gettext_select_utf8 (0);
87 #elif defined(ENABLE_NLS)
88 if (saved_codeset)
90 bind_textdomain_codeset (PACKAGE_GT, saved_codeset);
91 xfree (saved_codeset);
93 #else
94 (void)saved_codeset;
95 #endif
99 /* Gettext variant which temporary switches to utf-8 for string. */
100 const char *
101 i18n_utf8 (const char *string)
103 char *saved = i18n_switchto_utf8 ();
104 const char *result = _(string);
105 i18n_switchback (saved);
106 return result;