Blame glib/gcharset.c

Packit ae235b
/* gcharset.c - Charset information
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2011 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gcharsetprivate.h"
Packit ae235b
Packit ae235b
#include "garray.h"
Packit ae235b
#include "genviron.h"
Packit ae235b
#include "ghash.h"
Packit ae235b
#include "gmessages.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include "gwin32.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "libcharset/libcharset.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (aliases);
Packit ae235b
Packit ae235b
static GHashTable *
Packit ae235b
get_alias_hash (void)
Packit ae235b
{
Packit ae235b
  static GHashTable *alias_hash = NULL;
Packit ae235b
  const char *aliases;
Packit ae235b
Packit ae235b
  G_LOCK (aliases);
Packit ae235b
Packit ae235b
  if (!alias_hash)
Packit ae235b
    {
Packit ae235b
      alias_hash = g_hash_table_new (g_str_hash, g_str_equal);
Packit ae235b
Packit ae235b
      aliases = _g_locale_get_charset_aliases ();
Packit ae235b
      while (*aliases != '\0')
Packit ae235b
        {
Packit ae235b
          const char *canonical;
Packit ae235b
          const char *alias;
Packit ae235b
          const char **alias_array;
Packit ae235b
          int count = 0;
Packit ae235b
Packit ae235b
          alias = aliases;
Packit ae235b
          aliases += strlen (aliases) + 1;
Packit ae235b
          canonical = aliases;
Packit ae235b
          aliases += strlen (aliases) + 1;
Packit ae235b
Packit ae235b
          alias_array = g_hash_table_lookup (alias_hash, canonical);
Packit ae235b
          if (alias_array)
Packit ae235b
            {
Packit ae235b
              while (alias_array[count])
Packit ae235b
                count++;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          alias_array = g_renew (const char *, alias_array, count + 2);
Packit ae235b
          alias_array[count] = alias;
Packit ae235b
          alias_array[count + 1] = NULL;
Packit ae235b
Packit ae235b
          g_hash_table_insert (alias_hash, (char *)canonical, alias_array);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  G_UNLOCK (aliases);
Packit ae235b
Packit ae235b
  return alias_hash;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* As an abuse of the alias table, the following routines gets
Packit ae235b
 * the charsets that are aliases for the canonical name.
Packit ae235b
 */
Packit ae235b
const char **
Packit ae235b
_g_charset_get_aliases (const char *canonical_name)
Packit ae235b
{
Packit ae235b
  GHashTable *alias_hash = get_alias_hash ();
Packit ae235b
Packit ae235b
  return g_hash_table_lookup (alias_hash, canonical_name);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_utf8_get_charset_internal (const char  *raw_data,
Packit ae235b
                             const char **a)
Packit ae235b
{
Packit ae235b
  const char *charset = g_getenv ("CHARSET");
Packit ae235b
Packit ae235b
  if (charset && *charset)
Packit ae235b
    {
Packit ae235b
      *a = charset;
Packit ae235b
Packit ae235b
      if (charset && strstr (charset, "UTF-8"))
Packit ae235b
        return TRUE;
Packit ae235b
      else
Packit ae235b
        return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* The libcharset code tries to be thread-safe without
Packit ae235b
   * a lock, but has a memory leak and a missing memory
Packit ae235b
   * barrier, so we lock for it
Packit ae235b
   */
Packit ae235b
  G_LOCK (aliases);
Packit ae235b
  charset = _g_locale_charset_unalias (raw_data);
Packit ae235b
  G_UNLOCK (aliases);
Packit ae235b
Packit ae235b
  if (charset && *charset)
Packit ae235b
    {
Packit ae235b
      *a = charset;
Packit ae235b
Packit ae235b
      if (charset && strstr (charset, "UTF-8"))
Packit ae235b
        return TRUE;
Packit ae235b
      else
Packit ae235b
        return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Assume this for compatibility at present.  */
Packit ae235b
  *a = "US-ASCII";
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct _GCharsetCache GCharsetCache;
Packit ae235b
Packit ae235b
struct _GCharsetCache {
Packit ae235b
  gboolean is_utf8;
Packit ae235b
  gchar *raw;
Packit ae235b
  gchar *charset;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
charset_cache_free (gpointer data)
Packit ae235b
{
Packit ae235b
  GCharsetCache *cache = data;
Packit ae235b
  g_free (cache->raw);
Packit ae235b
  g_free (cache->charset);
Packit ae235b
  g_free (cache);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_get_charset:
Packit ae235b
 * @charset: (out) (optional) (transfer none): return location for character set
Packit ae235b
 *   name, or %NULL.
Packit ae235b
 *
Packit ae235b
 * Obtains the character set for the [current locale][setlocale]; you
Packit ae235b
 * might use this character set as an argument to g_convert(), to convert
Packit ae235b
 * from the current locale's encoding to some other encoding. (Frequently
Packit ae235b
 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts, though.)
Packit ae235b
 *
Packit ae235b
 * On Windows the character set returned by this function is the
Packit ae235b
 * so-called system default ANSI code-page. That is the character set
Packit ae235b
 * used by the "narrow" versions of C library and Win32 functions that
Packit ae235b
 * handle file names. It might be different from the character set
Packit ae235b
 * used by the C library's current locale.
Packit ae235b
 *
Packit ae235b
 * On Linux, the character set is found by consulting nl_langinfo() if
Packit ae235b
 * available. If not, the environment variables `LC_ALL`, `LC_CTYPE`, `LANG`
Packit ae235b
 * and `CHARSET` are queried in order.
Packit ae235b
 *
Packit ae235b
 * The return value is %TRUE if the locale's encoding is UTF-8, in that
Packit ae235b
 * case you can perhaps avoid calling g_convert().
Packit ae235b
 *
Packit ae235b
 * The string returned in @charset is not allocated, and should not be
Packit ae235b
 * freed.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the returned charset is UTF-8
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_get_charset (const char **charset)
Packit ae235b
{
Packit ae235b
  static GPrivate cache_private = G_PRIVATE_INIT (charset_cache_free);
Packit ae235b
  GCharsetCache *cache = g_private_get (&cache_private);
Packit ae235b
  const gchar *raw;
Packit ae235b
Packit ae235b
  if (!cache)
Packit ae235b
    {
Packit ae235b
      cache = g_new0 (GCharsetCache, 1);
Packit ae235b
      g_private_set (&cache_private, cache);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  G_LOCK (aliases);
Packit ae235b
  raw = _g_locale_charset_raw ();
Packit ae235b
  G_UNLOCK (aliases);
Packit ae235b
Packit ae235b
  if (!(cache->raw && strcmp (cache->raw, raw) == 0))
Packit ae235b
    {
Packit ae235b
      const gchar *new_charset;
Packit ae235b
Packit ae235b
      g_free (cache->raw);
Packit ae235b
      g_free (cache->charset);
Packit ae235b
      cache->raw = g_strdup (raw);
Packit ae235b
      cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset);
Packit ae235b
      cache->charset = g_strdup (new_charset);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (charset)
Packit ae235b
    *charset = cache->charset;
Packit ae235b
Packit ae235b
  return cache->is_utf8;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_get_codeset:
Packit ae235b
 *
Packit ae235b
 * Gets the character set for the current locale.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated string containing the name
Packit ae235b
 *     of the character set. This string must be freed with g_free().
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_get_codeset (void)
Packit ae235b
{
Packit ae235b
  const gchar *charset;
Packit ae235b
Packit ae235b
  g_get_charset (&charset);
Packit ae235b
Packit ae235b
  return g_strdup (charset);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
Packit ae235b
/* read an alias file for the locales */
Packit ae235b
static void
Packit ae235b
read_aliases (gchar      *file,
Packit ae235b
              GHashTable *alias_table)
Packit ae235b
{
Packit ae235b
  FILE *fp;
Packit ae235b
  char buf[256];
Packit ae235b
Packit ae235b
  fp = fopen (file,"r");
Packit ae235b
  if (!fp)
Packit ae235b
    return;
Packit ae235b
  while (fgets (buf, 256, fp))
Packit ae235b
    {
Packit ae235b
      char *p, *q;
Packit ae235b
Packit ae235b
      g_strstrip (buf);
Packit ae235b
Packit ae235b
      /* Line is a comment */
Packit ae235b
      if ((buf[0] == '#') || (buf[0] == '\0'))
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      /* Reads first column */
Packit ae235b
      for (p = buf, q = NULL; *p; p++) {
Packit ae235b
        if ((*p == '\t') || (*p == ' ') || (*p == ':')) {
Packit ae235b
          *p = '\0';
Packit ae235b
          q = p+1;
Packit ae235b
          while ((*q == '\t') || (*q == ' ')) {
Packit ae235b
            q++;
Packit ae235b
          }
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
      }
Packit ae235b
      /* The line only had one column */
Packit ae235b
      if (!q || *q == '\0')
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      /* Read second column */
Packit ae235b
      for (p = q; *p; p++) {
Packit ae235b
        if ((*p == '\t') || (*p == ' ')) {
Packit ae235b
          *p = '\0';
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
      }
Packit ae235b
Packit ae235b
      /* Add to alias table if necessary */
Packit ae235b
      if (!g_hash_table_lookup (alias_table, buf)) {
Packit ae235b
        g_hash_table_insert (alias_table, g_strdup (buf), g_strdup (q));
Packit ae235b
      }
Packit ae235b
    }
Packit ae235b
  fclose (fp);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static char *
Packit ae235b
unalias_lang (char *lang)
Packit ae235b
{
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  static GHashTable *alias_table = NULL;
Packit ae235b
  char *p;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&alias_table))
Packit ae235b
    {
Packit ae235b
      GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
Packit ae235b
      read_aliases ("/usr/share/locale/locale.alias", table);
Packit ae235b
      g_once_init_leave (&alias_table, table);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  i = 0;
Packit ae235b
  while ((p = g_hash_table_lookup (alias_table, lang)) && (strcmp (p, lang) != 0))
Packit ae235b
    {
Packit ae235b
      lang = p;
Packit ae235b
      if (i++ == 30)
Packit ae235b
        {
Packit ae235b
          static gboolean said_before = FALSE;
Packit ae235b
          if (!said_before)
Packit ae235b
            g_warning ("Too many alias levels for a locale, "
Packit ae235b
                       "may indicate a loop");
Packit ae235b
          said_before = TRUE;
Packit ae235b
          return lang;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
  return lang;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Mask for components of locale spec. The ordering here is from
Packit ae235b
 * least significant to most significant
Packit ae235b
 */
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  COMPONENT_CODESET =   1 << 0,
Packit ae235b
  COMPONENT_TERRITORY = 1 << 1,
Packit ae235b
  COMPONENT_MODIFIER =  1 << 2
Packit ae235b
};
Packit ae235b
Packit ae235b
/* Break an X/Open style locale specification into components
Packit ae235b
 */
Packit ae235b
static guint
Packit ae235b
explode_locale (const gchar *locale,
Packit ae235b
                gchar      **language,
Packit ae235b
                gchar      **territory,
Packit ae235b
                gchar      **codeset,
Packit ae235b
                gchar      **modifier)
Packit ae235b
{
Packit ae235b
  const gchar *uscore_pos;
Packit ae235b
  const gchar *at_pos;
Packit ae235b
  const gchar *dot_pos;
Packit ae235b
Packit ae235b
  guint mask = 0;
Packit ae235b
Packit ae235b
  uscore_pos = strchr (locale, '_');
Packit ae235b
  dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
Packit ae235b
  at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');
Packit ae235b
Packit ae235b
  if (at_pos)
Packit ae235b
    {
Packit ae235b
      mask |= COMPONENT_MODIFIER;
Packit ae235b
      *modifier = g_strdup (at_pos);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    at_pos = locale + strlen (locale);
Packit ae235b
Packit ae235b
  if (dot_pos)
Packit ae235b
    {
Packit ae235b
      mask |= COMPONENT_CODESET;
Packit ae235b
      *codeset = g_strndup (dot_pos, at_pos - dot_pos);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    dot_pos = at_pos;
Packit ae235b
Packit ae235b
  if (uscore_pos)
Packit ae235b
    {
Packit ae235b
      mask |= COMPONENT_TERRITORY;
Packit ae235b
      *territory = g_strndup (uscore_pos, dot_pos - uscore_pos);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    uscore_pos = dot_pos;
Packit ae235b
Packit ae235b
  *language = g_strndup (locale, uscore_pos - locale);
Packit ae235b
Packit ae235b
  return mask;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Compute all interesting variants for a given locale name -
Packit ae235b
 * by stripping off different components of the value.
Packit ae235b
 *
Packit ae235b
 * For simplicity, we assume that the locale is in
Packit ae235b
 * X/Open format: language[_territory][.codeset][@modifier]
Packit ae235b
 *
Packit ae235b
 * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
Packit ae235b
 *       as well. We could just copy the code from glibc wholesale
Packit ae235b
 *       but it is big, ugly, and complicated, so I'm reluctant
Packit ae235b
 *       to do so when this should handle 99% of the time...
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
append_locale_variants (GPtrArray *array,
Packit ae235b
                        const gchar *locale)
Packit ae235b
{
Packit ae235b
  gchar *language = NULL;
Packit ae235b
  gchar *territory = NULL;
Packit ae235b
  gchar *codeset = NULL;
Packit ae235b
  gchar *modifier = NULL;
Packit ae235b
Packit ae235b
  guint mask;
Packit ae235b
  guint i, j;
Packit ae235b
Packit ae235b
  g_return_if_fail (locale != NULL);
Packit ae235b
Packit ae235b
  mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
Packit ae235b
Packit ae235b
  /* Iterate through all possible combinations, from least attractive
Packit ae235b
   * to most attractive.
Packit ae235b
   */
Packit ae235b
  for (j = 0; j <= mask; ++j)
Packit ae235b
    {
Packit ae235b
      i = mask - j;
Packit ae235b
Packit ae235b
      if ((i & ~mask) == 0)
Packit ae235b
        {
Packit ae235b
          gchar *val = g_strconcat (language,
Packit ae235b
                                    (i & COMPONENT_TERRITORY) ? territory : "",
Packit ae235b
                                    (i & COMPONENT_CODESET) ? codeset : "",
Packit ae235b
                                    (i & COMPONENT_MODIFIER) ? modifier : "",
Packit ae235b
                                    NULL);
Packit ae235b
          g_ptr_array_add (array, val);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (language);
Packit ae235b
  if (mask & COMPONENT_CODESET)
Packit ae235b
    g_free (codeset);
Packit ae235b
  if (mask & COMPONENT_TERRITORY)
Packit ae235b
    g_free (territory);
Packit ae235b
  if (mask & COMPONENT_MODIFIER)
Packit ae235b
    g_free (modifier);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_get_locale_variants:
Packit ae235b
 * @locale: a locale identifier
Packit ae235b
 *
Packit ae235b
 * Returns a list of derived variants of @locale, which can be used to
Packit ae235b
 * e.g. construct locale-dependent filenames or search paths. The returned
Packit ae235b
 * list is sorted from most desirable to least desirable.
Packit ae235b
 * This function handles territory, charset and extra locale modifiers.
Packit ae235b
 *
Packit ae235b
 * For example, if @locale is "fr_BE", then the returned list
Packit ae235b
 * is "fr_BE", "fr".
Packit ae235b
 *
Packit ae235b
 * If you need the list of variants for the current locale,
Packit ae235b
 * use g_get_language_names().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full) (array zero-terminated=1) (element-type utf8): a newly
Packit ae235b
 *   allocated array of newly allocated strings with the locale variants. Free with
Packit ae235b
 *   g_strfreev().
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gchar **
Packit ae235b
g_get_locale_variants (const gchar *locale)
Packit ae235b
{
Packit ae235b
  GPtrArray *array;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (locale != NULL, NULL);
Packit ae235b
Packit ae235b
  array = g_ptr_array_sized_new (8);
Packit ae235b
  append_locale_variants (array, locale);
Packit ae235b
  g_ptr_array_add (array, NULL);
Packit ae235b
Packit ae235b
  return (gchar **) g_ptr_array_free (array, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* The following is (partly) taken from the gettext package.
Packit ae235b
   Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.  */
Packit ae235b
Packit ae235b
static const gchar *
Packit ae235b
guess_category_value (const gchar *category_name)
Packit ae235b
{
Packit ae235b
  const gchar *retval;
Packit ae235b
Packit ae235b
  /* The highest priority value is the 'LANGUAGE' environment
Packit ae235b
     variable.  This is a GNU extension.  */
Packit ae235b
  retval = g_getenv ("LANGUAGE");
Packit ae235b
  if ((retval != NULL) && (retval[0] != '\0'))
Packit ae235b
    return retval;
Packit ae235b
Packit ae235b
  /* 'LANGUAGE' is not set.  So we have to proceed with the POSIX
Packit ae235b
     methods of looking to 'LC_ALL', 'LC_xxx', and 'LANG'.  On some
Packit ae235b
     systems this can be done by the 'setlocale' function itself.  */
Packit ae235b
Packit ae235b
  /* Setting of LC_ALL overwrites all other.  */
Packit ae235b
  retval = g_getenv ("LC_ALL");
Packit ae235b
  if ((retval != NULL) && (retval[0] != '\0'))
Packit ae235b
    return retval;
Packit ae235b
Packit ae235b
  /* Next comes the name of the desired category.  */
Packit ae235b
  retval = g_getenv (category_name);
Packit ae235b
  if ((retval != NULL) && (retval[0] != '\0'))
Packit ae235b
    return retval;
Packit ae235b
Packit ae235b
  /* Last possibility is the LANG environment variable.  */
Packit ae235b
  retval = g_getenv ("LANG");
Packit ae235b
  if ((retval != NULL) && (retval[0] != '\0'))
Packit ae235b
    return retval;
Packit ae235b
Packit ae235b
#ifdef G_PLATFORM_WIN32
Packit ae235b
  /* g_win32_getlocale() first checks for LC_ALL, LC_MESSAGES and
Packit ae235b
   * LANG, which we already did above. Oh well. The main point of
Packit ae235b
   * calling g_win32_getlocale() is to get the thread's locale as used
Packit ae235b
   * by Windows and the Microsoft C runtime (in the "English_United
Packit ae235b
   * States" format) translated into the Unixish format.
Packit ae235b
   */
Packit ae235b
  {
Packit ae235b
    char *locale = g_win32_getlocale ();
Packit ae235b
    retval = g_intern_string (locale);
Packit ae235b
    g_free (locale);
Packit ae235b
    return retval;
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct _GLanguageNamesCache GLanguageNamesCache;
Packit ae235b
Packit ae235b
struct _GLanguageNamesCache {
Packit ae235b
  gchar *languages;
Packit ae235b
  gchar **language_names;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
language_names_cache_free (gpointer data)
Packit ae235b
{
Packit ae235b
  GLanguageNamesCache *cache = data;
Packit ae235b
  g_free (cache->languages);
Packit ae235b
  g_strfreev (cache->language_names);
Packit ae235b
  g_free (cache);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_get_language_names:
Packit ae235b
 *
Packit ae235b
 * Computes a list of applicable locale names, which can be used to
Packit ae235b
 * e.g. construct locale-dependent filenames or search paths. The returned
Packit ae235b
 * list is sorted from most desirable to least desirable and always contains
Packit ae235b
 * the default locale "C".
Packit ae235b
 *
Packit ae235b
 * For example, if LANGUAGE=de:en_US, then the returned list is
Packit ae235b
 * "de", "en_US", "en", "C".
Packit ae235b
 *
Packit ae235b
 * This function consults the environment variables `LANGUAGE`, `LC_ALL`,
Packit ae235b
 * `LC_MESSAGES` and `LANG` to find the list of locales specified by the
Packit ae235b
 * user.
Packit ae235b
 *
Packit ae235b
 * Returns: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib
Packit ae235b
 *    that must not be modified or freed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.6
Packit ae235b
 **/
Packit ae235b
const gchar * const *
Packit ae235b
g_get_language_names (void)
Packit ae235b
{
Packit ae235b
  static GPrivate cache_private = G_PRIVATE_INIT (language_names_cache_free);
Packit ae235b
  GLanguageNamesCache *cache = g_private_get (&cache_private);
Packit ae235b
  const gchar *value;
Packit ae235b
Packit ae235b
  if (!cache)
Packit ae235b
    {
Packit ae235b
      cache = g_new0 (GLanguageNamesCache, 1);
Packit ae235b
      g_private_set (&cache_private, cache);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  value = guess_category_value ("LC_MESSAGES");
Packit ae235b
  if (!value)
Packit ae235b
    value = "C";
Packit ae235b
Packit ae235b
  if (!(cache->languages && strcmp (cache->languages, value) == 0))
Packit ae235b
    {
Packit ae235b
      GPtrArray *array;
Packit ae235b
      gchar **alist, **a;
Packit ae235b
Packit ae235b
      g_free (cache->languages);
Packit ae235b
      g_strfreev (cache->language_names);
Packit ae235b
      cache->languages = g_strdup (value);
Packit ae235b
Packit ae235b
      array = g_ptr_array_sized_new (8);
Packit ae235b
Packit ae235b
      alist = g_strsplit (value, ":", 0);
Packit ae235b
      for (a = alist; *a; a++)
Packit ae235b
        append_locale_variants (array, unalias_lang (*a));
Packit ae235b
      g_strfreev (alist);
Packit ae235b
      g_ptr_array_add (array, g_strdup ("C"));
Packit ae235b
      g_ptr_array_add (array, NULL);
Packit ae235b
Packit ae235b
      cache->language_names = (gchar **) g_ptr_array_free (array, FALSE);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return (const gchar * const *) cache->language_names;
Packit ae235b
}