summaryrefslogtreecommitdiffstats
path: root/common/localename.c
blob: cb7fcc2f744066e4786c78008af06d03a4b16394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* localename.c - Determine the current selected locale.
   Copyright (C) 1995-1999, 2000-2003, 2007, 
                 2008 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1,
   or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/* Written by Ulrich Drepper <drepper@gnu.org>, 1995.  */
/* Win32 code written by Tor Lillqvist <tml@iki.fi>.  */
/* Modified for GpgOL use by Werner Koch <wk@gnupg.org>, 2005.  */ 
/* Modified for GnuPG use by Werner Koch <wk@gnupg.org>, 2007 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif

#include "../jnlib/w32help.h"

/* XPG3 defines the result of 'setlocale (category, NULL)' as:
   "Directs 'setlocale()' to query 'category' and return the current
    setting of 'local'."
   However it does not specify the exact format.  Neither do SUSV2 and
   ISO C 99.  So we can use this feature only on selected systems (e.g.
   those using GNU C Library).  */
#if defined _LIBC || (defined __GNU_LIBRARY__ && __GNU_LIBRARY__ >= 2)
# define HAVE_LOCALE_NULL
#endif

/* Use a dummy value for LC_MESSAGES in case it is not defined.  This
   works becuase we always test for HAVE_LC_MESSAGES and the core
   fucntion takes the category as a string as well.  */
#ifndef HAVE_LC_MESSAGES
#define LC_MESSAGES 0
#endif


/* Determine the current locale's name, and canonicalize it into XPG syntax
     language[_territory[.codeset]][@modifier]
   The codeset part in the result is not reliable; the locale_charset()
   should be used for codeset information instead.
   The result must not be freed; it is statically allocated.  */

#ifndef HAVE_W32_SYSTEM
static const char *
do_nl_locale_name (int category, const char *categoryname)
{
  const char *retval;

  /* Use the POSIX methods of looking to 'LC_ALL', 'LC_xxx', and 'LANG'.
     On some systems this can be done by the 'setlocale' function itself.  */
# if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
  (void)categoryname;
  retval = setlocale (category, NULL);
# else 
  /* Setting of LC_ALL overwrites all other.  */
  retval = getenv ("LC_ALL");
  if (retval == NULL || retval[0] == '\0')
    {
      /* Next comes the name of the desired category.  */
      retval = getenv (categoryname);
      if (retval == NULL || retval[0] == '\0')
	{
	  /* Last possibility is the LANG environment variable.  */
	  retval = getenv ("LANG");
	  if (retval == NULL || retval[0] == '\0')
	    /* We use C as the default domain.  POSIX says this is
	       implementation defined.  */
	    retval = "C";
	}
    }
# endif

  return retval;
}
#endif /* HAVE_W32_SYSTEM */



/* Return the locale used for translatable messages.  The standard C
   and POSIX are locale names are mapped to an empty string.  If a
   locale can't be found an empty string will be returned.  */
const char *
gnupg_messages_locale_name (void)
{
  const char *s;

#ifdef HAVE_W32_SYSTEM
  /* We use the localname function from ../jnlib/w32-gettext.c. */
  s = gettext_localename ();
#else
  s = do_nl_locale_name (LC_MESSAGES, "LC_MESSAGES");
#endif
  if (!s)
    s = "";
  else if (!strcmp (s, "C") || !strcmp (s, "POSIX"))
    s = "";

  return s;
}