Beispiel #1
0
/**
 * gnlp_client_get_available_languages:
 * @cancellable: (nullable): a #GCancellable
 * @error: (nullable): return location for a #GError, or %NULL
 *
 * Retrieves the list of available languages.
 *
 * Returns: (transfer container): a #GPtrArray that may be empty or not.
 * Free with @g_ptr_array_free.
 *
 * Since: 0.1.0
 */
GPtrArray*
gnlp_get_available_languages_sync (GCancellable  *cancellable,
                                   GError       **error)
{
  GPtrArray *languages;
  gboolean cancelled;
  gchar **locales;
  guint i;

  cancelled = FALSE;
  languages = g_ptr_array_new_with_free_func (g_object_unref);
  locales = gnome_get_all_locales ();

  for (i = 0; locales[i]; i++)
    {
      if (g_cancellable_set_error_if_cancelled (cancellable, error))
        {
          cancelled = TRUE;
          break;
        }

      g_ptr_array_add (languages, gnlp_language_new (locales[i]));
    }

  g_strfreev (locales);

  if (cancelled)
    g_clear_pointer (&languages, g_ptr_array_unref);

  return languages;
}
static void
add_all_languages (GtkDialog *chooser)
{
        gchar **locale_ids;
        GHashTable *initial;

        locale_ids = gnome_get_all_locales ();
        initial = cc_common_language_get_initial_languages ();
        add_languages (chooser, locale_ids, initial);
        g_hash_table_destroy (initial);
        g_strfreev (locale_ids);
}
int main (int argc, char **argv)
{
        char **locales;
        guint i;

        setlocale (LC_ALL, "");
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");

        if (argc > 1) {
                guint i;
                for (i = 1; i < argc; i++) {
                        char *lang, *country, *norm;
                        norm = gnome_normalize_locale (argv[i]);
                        lang = gnome_get_language_from_locale (norm, NULL);
                        country = gnome_get_country_from_locale (norm, NULL);
                        g_print ("%s (norm: %s) == %s -- %s\n", argv[i], norm, lang, country);
                        g_free (norm);
                        g_free (lang);
                        g_free (country);
                }
                return 0;
        }

        locales = gnome_get_all_locales ();
        if (locales == NULL) {
                g_warning ("No locales found");
                return 1;
        }

        for (i = 0; locales[i] != NULL; i++) {
                char *lang, *country;
                lang = gnome_get_language_from_locale (locales[i], NULL);
                country = gnome_get_country_from_locale (locales[i], NULL);
                g_print ("%s == %s -- %s\n", locales[i], lang, country);
                g_free (lang);
                g_free (country);
        }

        g_strfreev (locales);

        return 0;
}
static void
get_locale_infos (GtkWidget *chooser)
{
  CcInputChooserPrivate *priv = GET_PRIVATE (chooser);
  GHashTable *layouts_with_locale;
  LocaleInfo *info;
  gchar **locale_ids;
  gchar **locale;
  GList *list, *l;

  priv->locales = g_hash_table_new_full (g_str_hash, g_str_equal,
                                         NULL, locale_info_free);
  priv->locales_by_language = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                     g_free, (GDestroyNotify) g_hash_table_destroy);

  layouts_with_locale = g_hash_table_new (g_str_hash, g_str_equal);

  locale_ids = gnome_get_all_locales ();
  for (locale = locale_ids; *locale; ++locale)
    {
      gchar *lang_code, *country_code;
      gchar *simple_locale;
      gchar *tmp;
      const gchar *type = NULL;
      const gchar *id = NULL;

      if (!gnome_parse_locale (*locale, &lang_code, &country_code, NULL, NULL))
        continue;

      if (country_code != NULL)
	simple_locale = g_strdup_printf ("%s_%s.UTF-8", lang_code, country_code);
      else
	simple_locale = g_strdup_printf ("%s.UTF-8", lang_code);

      if (g_hash_table_contains (priv->locales, simple_locale))
        {
          g_free (simple_locale);
          g_free (country_code);
          g_free (lang_code);
          continue;
        }

      info = g_new0 (LocaleInfo, 1);
      info->id = simple_locale; /* Take ownership */
      info->name = gnome_get_language_from_locale (simple_locale, NULL);
      info->unaccented_name = cc_util_normalize_casefold_and_unaccent (info->name);
      tmp = gnome_get_language_from_locale (simple_locale, "C");
      info->untranslated_name = cc_util_normalize_casefold_and_unaccent (tmp);
      g_free (tmp);

      g_hash_table_replace (priv->locales, simple_locale, info);
      add_locale_to_table (priv->locales_by_language, lang_code, info);

      if (gnome_get_input_source_from_locale (simple_locale, &type, &id) &&
          g_str_equal (type, INPUT_SOURCE_TYPE_XKB))
        {
          add_default_row (chooser, info, type, id);
          g_hash_table_add (layouts_with_locale, (gpointer) id);
        }

      /* We don't own these ids */
      info->layout_rows_by_id = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                       NULL, g_object_unref);
      info->engine_rows_by_id = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                       NULL, g_object_unref);

      list = gnome_xkb_info_get_layouts_for_language (priv->xkb_info, lang_code);
      add_rows_to_table (chooser, info, list, INPUT_SOURCE_TYPE_XKB, id);
      add_ids_to_set (layouts_with_locale, list);
      g_list_free (list);

      if (country_code != NULL)
        {
          list = gnome_xkb_info_get_layouts_for_country (priv->xkb_info, country_code);
          add_rows_to_table (chooser, info, list, INPUT_SOURCE_TYPE_XKB, id);
          add_ids_to_set (layouts_with_locale, list);
          g_list_free (list);
        }

      g_free (lang_code);
      g_free (country_code);
    }
  g_strfreev (locale_ids);

  /* Add a "Other" locale to hold the remaining input sources */
  info = g_new0 (LocaleInfo, 1);
  info->id = g_strdup ("");
  info->name = g_strdup (C_("Keyboard Layout", "Other"));
  info->unaccented_name = g_strdup ("");
  info->untranslated_name = g_strdup ("");
  g_hash_table_replace (priv->locales, info->id, info);

  info->layout_rows_by_id = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                   NULL, g_object_unref);
  info->engine_rows_by_id = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                   NULL, g_object_unref);

  list = gnome_xkb_info_get_all_layouts (priv->xkb_info);
  for (l = list; l; l = l->next)
    if (!g_hash_table_contains (layouts_with_locale, l->data))
      add_row_other (chooser, INPUT_SOURCE_TYPE_XKB, l->data);

  g_list_free (list);

  g_hash_table_destroy (layouts_with_locale);
}