1 /* localename.c - Determine the current selected locale.
2 * Copyright (C) 1995-1999, 2000-2003, 2007,
3 * 2008 Free Software Foundation, Inc.
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of either
8 * - the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
14 * - the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at
16 * your option) any later version.
18 * or both in parallel, as here.
20 * This file is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public
26 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
28 /* Written by Ulrich Drepper <drepper@gnu.org>, 1995. */
29 /* Win32 code written by Tor Lillqvist <tml@iki.fi>. */
30 /* Modified for GpgOL use by Werner Koch <wk@gnupg.org>, 2005. */
31 /* Modified for GnuPG use by Werner Koch <wk@gnupg.org>, 2007 */
42 #include <gpg-error.h> /* We need gettext_localename for W32. */
44 #include "../common/w32help.h"
46 /* XPG3 defines the result of 'setlocale (category, NULL)' as:
47 "Directs 'setlocale()' to query 'category' and return the current
49 However it does not specify the exact format. Neither do SUSV2 and
50 ISO C 99. So we can use this feature only on selected systems (e.g.
51 those using GNU C Library). */
52 #if defined _LIBC || (defined __GNU_LIBRARY__ && __GNU_LIBRARY__ >= 2)
53 # define HAVE_LOCALE_NULL
56 /* Use a dummy value for LC_MESSAGES in case it is not defined. This
57 works because we always test for HAVE_LC_MESSAGES and the core
58 function takes the category as a string as well. */
59 #ifndef HAVE_LC_MESSAGES
64 /* Determine the current locale's name, and canonicalize it into XPG syntax
65 language[_territory[.codeset]][@modifier]
66 The codeset part in the result is not reliable; the locale_charset()
67 should be used for codeset information instead.
68 The result must not be freed; it is statically allocated. */
70 #ifndef HAVE_W32_SYSTEM
72 do_nl_locale_name (int category, const char *categoryname)
76 /* Use the POSIX methods of looking to 'LC_ALL', 'LC_xxx', and 'LANG'.
77 On some systems this can be done by the 'setlocale' function itself. */
78 # if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
80 retval = setlocale (category, NULL);
82 /* Setting of LC_ALL overwrites all other. */
83 retval = getenv ("LC_ALL");
84 if (retval == NULL || retval[0] == '\0')
86 /* Next comes the name of the desired category. */
87 retval = getenv (categoryname);
88 if (retval == NULL || retval[0] == '\0')
90 /* Last possibility is the LANG environment variable. */
91 retval = getenv ("LANG");
92 if (retval == NULL || retval[0] == '\0')
93 /* We use C as the default domain. POSIX says this is
94 implementation defined. */
102 #endif /* HAVE_W32_SYSTEM */
106 /* Return the locale used for translatable messages. The standard C
107 and POSIX are locale names are mapped to an empty string. If a
108 locale can't be found an empty string will be returned. */
110 gnupg_messages_locale_name (void)
114 #ifdef HAVE_W32_SYSTEM
115 /* We use the localename function libgpg-error. */
116 s = gettext_localename ();
118 s = do_nl_locale_name (LC_MESSAGES, "LC_MESSAGES");
122 else if (!strcmp (s, "C") || !strcmp (s, "POSIX"))