Example #1
0
/**
 * gnome_get_country_from_locale:
 * @locale: a locale string
 * @translation: (allow-none): a locale string
 *
 * Gets the country description for @locale. If @translation is
 * provided the returned string is translated accordingly.
 *
 * Return value: (transfer full): the country description. Caller
 * takes ownership.
 *
 * Since: 3.8
 */
char *
gnome_get_country_from_locale (const char *locale,
                               const char *translation)
{
        GString *full_name;
        g_autofree char *language_code = NULL;
        g_autofree char *territory_code = NULL;
        g_autofree char *codeset_code = NULL;
        g_autofree char *langinfo_codeset = NULL;
        g_autofree char *translated_language = NULL;
        g_autofree char *translated_territory = NULL;
        gboolean is_utf8 = TRUE;

        g_return_val_if_fail (locale != NULL, NULL);
        g_return_val_if_fail (*locale != '\0', NULL);

        full_name = g_string_new (NULL);

        languages_init ();
        territories_init ();

        gnome_parse_locale (locale,
                            &language_code,
                            &territory_code,
                            &codeset_code,
                            NULL);

        if (territory_code == NULL) {
                goto out;
        }

        translated_territory = get_translated_territory (territory_code, translation);
        g_string_append (full_name, translated_territory);

	if (is_unique_territory (territory_code)) {
		goto out;
	}

        if (language_code != NULL) {
                translated_language = get_translated_language (language_code, translation);
        }
        if (translated_language != NULL) {
                g_string_append_printf (full_name,
                                        " (%s)",
                                        translated_language);
        }

        language_name_get_codeset_details (translation, &langinfo_codeset, &is_utf8);

        if (codeset_code == NULL && langinfo_codeset != NULL) {
                codeset_code = g_strdup (langinfo_codeset);
        }

        if (!is_utf8 && codeset_code) {
                g_string_append_printf (full_name,
                                        " [%s]",
                                        codeset_code);
        }

 out:
        if (full_name->len == 0) {
                g_string_free (full_name, TRUE);
                return NULL;
        }

        return g_string_free (full_name, FALSE);
}
Example #2
0
static gboolean
add_locale (const char *language_name,
            gboolean    utf8_only)
{
        GnomeLocale *locale;
        GnomeLocale *old_locale;
        g_autofree char *name = NULL;
        gboolean   is_utf8 = FALSE;
        gboolean   valid = FALSE;

        g_return_val_if_fail (language_name != NULL, FALSE);
        g_return_val_if_fail (*language_name != '\0', FALSE);

        language_name_get_codeset_details (language_name, NULL, &is_utf8);

        if (is_utf8) {
                name = g_strdup (language_name);
        } else if (utf8_only) {

                if (strchr (language_name, '.'))
                        return FALSE;

                /* If the locale name has no dot, assume that its
                 * encoding part is missing and try again after adding
                 * ".UTF-8". This catches locale names like "de_DE".
                 */
                name = g_strdup_printf ("%s.UTF-8", language_name);

                language_name_get_codeset_details (name, NULL, &is_utf8);
                if (!is_utf8)
                        return FALSE;
        } else {
                name = g_strdup (language_name);
        }

        if (!language_name_is_valid (name)) {
                g_debug ("Ignoring '%s' as a locale, since it's invalid", name);
                return FALSE;
        }

        locale = g_new0 (GnomeLocale, 1);
        valid = gnome_parse_locale (name,
                                    &locale->language_code,
                                    &locale->territory_code,
                                    &locale->codeset,
                                    &locale->modifier);
        if (!valid) {
                gnome_locale_free (locale);
                return FALSE;
        }

        locale->id = construct_language_name (locale->language_code, locale->territory_code,
                                              NULL, locale->modifier);
        locale->name = construct_language_name (locale->language_code, locale->territory_code,
                                                locale->codeset, locale->modifier);

        if (!gnome_language_has_translations (locale->name) &&
            !gnome_language_has_translations (locale->id) &&
            !gnome_language_has_translations (locale->language_code) &&
            utf8_only) {
                g_debug ("Ignoring '%s' as a locale, since it lacks translations", locale->name);
                gnome_locale_free (locale);
                return FALSE;
        }

        if (!utf8_only) {
                g_free (locale->id);
                locale->id = g_strdup (locale->name);
        }

        old_locale = g_hash_table_lookup (gnome_available_locales_map, locale->id);
        if (old_locale != NULL) {
                if (strlen (old_locale->name) > strlen (locale->name)) {
                        gnome_locale_free (locale);
                        return FALSE;
                }
        }

        g_hash_table_insert (gnome_available_locales_map, g_strdup (locale->id), locale);

        return TRUE;
}
static gboolean
add_locale (const char *language_name,
            gboolean    utf8_only)
{
        GdmLocale *locale;
        GdmLocale *old_locale;
        char      *name;
        gboolean   is_utf8 = FALSE;

        g_return_val_if_fail (language_name != NULL, FALSE);
        g_return_val_if_fail (*language_name != '\0', FALSE);

        language_name_get_codeset_details (language_name, NULL, &is_utf8);

        if (is_utf8) {
                name = g_strdup (language_name);
        } else if (utf8_only) {
                name = g_strdup_printf ("%s.utf8", language_name);

                language_name_get_codeset_details (name, NULL, &is_utf8);
                if (!is_utf8) {
                        g_free (name);
                        return FALSE;
                }
        } else {
                name = g_strdup (language_name);
        }

        if (!language_name_is_valid (name)) {
                g_debug ("Ignoring '%s' as a locale, since it's invalid", name);
                g_free (name);
                return FALSE;
        }

        locale = g_new0 (GdmLocale, 1);
        gdm_parse_language_name (name,
                                 &locale->language_code,
                                 &locale->territory_code,
                                 &locale->codeset,
                                 &locale->modifier);
        g_free (name);
        name = NULL;

#ifdef WITH_INCOMPLETE_LOCALES
        if (utf8_only) {
                if (locale->territory_code == NULL || locale->modifier) {
                        g_debug ("Ignoring '%s' as a locale, since it lacks territory code or modifier", name);
                        gdm_locale_free (locale);
                        return FALSE;
                }
        }
#endif

        locale->id = construct_language_name (locale->language_code, locale->territory_code,
                                              NULL, locale->modifier);
        locale->name = construct_language_name (locale->language_code, locale->territory_code,
                                                locale->codeset, locale->modifier);

#ifndef WITH_INCOMPLETE_LOCALES
        if (!gdm_language_has_translations (locale->name) &&
            !gdm_language_has_translations (locale->id) &&
            !gdm_language_has_translations (locale->language_code) &&
            utf8_only) {
                g_debug ("Ignoring '%s' as a locale, since it lacks translations", locale->name);
                gdm_locale_free (locale);
                return FALSE;
        }
#endif

        if (!utf8_only) {
                g_free (locale->id);
                locale->id = g_strdup (locale->name);
        }

        old_locale = g_hash_table_lookup (gdm_available_locales_map, locale->id);
        if (old_locale != NULL) {
                if (strlen (old_locale->name) > strlen (locale->name)) {
                        gdm_locale_free (locale);
                        return FALSE;
                }
        }

        g_hash_table_insert (gdm_available_locales_map, g_strdup (locale->id), locale);

        return TRUE;
}