Back to index

courier  0.68.2
Functions
localename.c File Reference
#include <config.h>
#include "localename.h"
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>

Go to the source code of this file.

Functions

const char * gl_locale_name_thread (int category, const char *categoryname)
const char * gl_locale_name_posix (int category, const char *categoryname)
const char * gl_locale_name_environ (int category, const char *categoryname)
const char * gl_locale_name_default (void)
const char * gl_locale_name (int category, const char *categoryname)

Function Documentation

const char* gl_locale_name ( int  category,
const char *  categoryname 
)

Definition at line 2946 of file localename.c.

{
  const char *retval;

  retval = gl_locale_name_thread (category, categoryname);
  if (retval != NULL)
    return retval;

  retval = gl_locale_name_posix (category, categoryname);
  if (retval != NULL)
    return retval;

  return gl_locale_name_default ();
}

Here is the call graph for this function:

const char* gl_locale_name_default ( void  )

Definition at line 2841 of file localename.c.

{
  /* POSIX:2001 says:
     "All implementations shall define a locale as the default locale, to be
      invoked when no environment variables are set, or set to the empty
      string.  This default locale can be the POSIX locale or any other
      implementation-defined locale.  Some implementations may provide
      facilities for local installation administrators to set the default
      locale, customizing it for each location.  POSIX:2001 does not require
      such a facility.

     The systems with such a facility are MacOS X and Windows: They provide a
     GUI that allows the user to choose a locale.
       - On MacOS X, by default, none of LC_* or LANG are set.  Starting with
         MacOS X 10.4 or 10.5, LANG is set for processes launched by the
         'Terminal' application (but sometimes to an incorrect value "UTF-8").
         When no environment variable is set, setlocale (LC_ALL, "") uses the
         "C" locale.
       - On native Windows, by default, none of LC_* or LANG are set.
         When no environment variable is set, setlocale (LC_ALL, "") uses the
         locale chosen by the user.
       - On Cygwin 1.5.x, by default, none of LC_* or LANG are set.
         When no environment variable is set, setlocale (LC_ALL, "") uses the
         "C" locale.
       - On Cygwin 1.7, by default, LANG is set to "C.UTF-8" when the default
         ~/.profile is executed.
         When no environment variable is set, setlocale (LC_ALL, "") uses the
         "C.UTF-8" locale, which operates in the same way as the "C" locale.
  */

#if !(HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE || defined WIN32_NATIVE || defined __CYGWIN__)

  /* The system does not have a way of setting the locale, other than the
     POSIX specified environment variables.  We use C as default locale.  */
  return "C";

#else

  /* Return an XPG style locale name language[_territory][@modifier].
     Don't even bother determining the codeset; it's not useful in this
     context, because message catalogs are not specific to a single
     codeset.  */

# if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE
  /* MacOS X 10.2 or newer */
  {
    /* Cache the locale name, since CoreFoundation calls are expensive.  */
    static const char *cached_localename;

    if (cached_localename == NULL)
      {
        char namebuf[256];
#  if HAVE_CFLOCALECOPYCURRENT /* MacOS X 10.3 or newer */
        CFLocaleRef locale = CFLocaleCopyCurrent ();
        CFStringRef name = CFLocaleGetIdentifier (locale);

        if (CFStringGetCString (name, namebuf, sizeof (namebuf),
                                kCFStringEncodingASCII))
          {
            gl_locale_name_canonicalize (namebuf);
            cached_localename = strdup (namebuf);
          }
        CFRelease (locale);
#  elif HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.2 or newer */
        CFTypeRef value =
          CFPreferencesCopyAppValue (CFSTR ("AppleLocale"),
                                     kCFPreferencesCurrentApplication);
        if (value != NULL
            && CFGetTypeID (value) == CFStringGetTypeID ()
            && CFStringGetCString ((CFStringRef)value,
                                   namebuf, sizeof (namebuf),
                                   kCFStringEncodingASCII))
          {
            gl_locale_name_canonicalize (namebuf);
            cached_localename = strdup (namebuf);
          }
#  endif
        if (cached_localename == NULL)
          cached_localename = "C";
      }
    return cached_localename;
  }

# endif

# if defined WIN32_NATIVE || defined __CYGWIN__ /* WIN32 or Cygwin */
  {
    LCID lcid;

    /* Use native Win32 API locale ID.  */
    lcid = GetThreadLocale ();

    return gl_locale_name_from_win32_LCID (lcid);
  }
# endif
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* gl_locale_name_environ ( int  category,
const char *  categoryname 
)

Definition at line 2808 of file localename.c.

{
  const char *retval;

  /* Setting of LC_ALL overrides all other.  */
  retval = getenv ("LC_ALL");
  if (retval != NULL && retval[0] != '\0')
    return retval;
  /* Next comes the name of the desired category.  */
  retval = getenv (categoryname);
  if (retval != NULL && retval[0] != '\0')
    return retval;
  /* Last possibility is the LANG environment variable.  */
  retval = getenv ("LANG");
  if (retval != NULL && retval[0] != '\0')
    {
#if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE
      /* MacOS X 10.2 or newer.
         Ignore invalid LANG value set by the Terminal application.  */
      if (strcmp (retval, "UTF-8") != 0)
#endif
#if defined __CYGWIN__
      /* Cygwin.
         Ignore dummy LANG value set by ~/.profile.  */
      if (strcmp (retval, "C.UTF-8") != 0)
#endif
        return retval;
    }

  return NULL;
}

Here is the caller graph for this function:

const char* gl_locale_name_posix ( int  category,
const char *  categoryname 
)

Definition at line 2779 of file localename.c.

{
  /* 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
  return setlocale (category, NULL);
#else
  /* On other systems we ignore what setlocale reports and instead look at the
     environment variables directly.  This is necessary
       1. on systems which have a facility for customizing the default locale
          (MacOS X, native Windows, Cygwin) and where the system's setlocale()
          function ignores this default locale (MacOS X, Cygwin), in two cases:
          a. when the user missed to use the setlocale() override from libintl
             (for example by not including <libintl.h>),
          b. when setlocale supports only the "C" locale, such as on Cygwin
             1.5.x.  In this case even the override from libintl cannot help.
       2. on all systems where setlocale supports only the "C" locale.  */
  /* Strictly speaking, it is a POSIX violation to look at the environment
     variables regardless whether setlocale has been called or not.  POSIX
     says:
         "For C-language programs, the POSIX locale shall be the
          default locale when the setlocale() function is not called."
     But we assume that all programs that use internationalized APIs call
     setlocale (LC_ALL, "").  */
  return gl_locale_name_environ (category, categoryname);
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* gl_locale_name_thread ( int  category,
const char *  categoryname 
)

Definition at line 2758 of file localename.c.

{
#if HAVE_USELOCALE
  const char *name = gl_locale_name_thread_unsafe (category, categoryname);
  if (name != NULL)
    return struniq (name);
#endif
  return NULL;
}

Here is the caller graph for this function: