Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
devel:BCI:SLCS:next
glib2
0001-Look-for-translation-of-desktop-entry-stri...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0001-Look-for-translation-of-desktop-entry-strings-via-ge.patch of Package glib2
From 5bda213d583ac081e1d121fd698d17aec8659568 Mon Sep 17 00:00:00 2001 From: Vincent Untz <vuntz@novell.com> Date: Fri, 23 Feb 2024 10:58:56 +0100 Subject: [PATCH 1/4] Look for translation of desktop entry strings via gettext, part that we share with Ubuntu and try to push upstream --- glib/gkeyfile.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ glib/gkeyfile.h | 1 + 2 files changed, 71 insertions(+) diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c index 637ac9c15..8ed904f0b 100644 --- a/glib/gkeyfile.c +++ b/glib/gkeyfile.c @@ -510,6 +510,7 @@ struct _GKeyFile gboolean checked_locales; /* TRUE if @locales has been initialised */ gchar **locales; /* (nullable) */ + gchar *gettext_domain; gint ref_count; /* (atomic) */ }; @@ -636,6 +637,7 @@ g_key_file_init (GKeyFile *key_file) key_file->parse_buffer = NULL; key_file->list_separator = ';'; key_file->flags = 0; + key_file->gettext_domain = NULL; } static void @@ -656,6 +658,12 @@ g_key_file_clear (GKeyFile *key_file) key_file->parse_buffer = NULL; } + if (key_file->gettext_domain) + { + g_free (key_file->gettext_domain); + key_file->gettext_domain = NULL; + } + tmp = key_file->groups; while (tmp != NULL) { @@ -876,6 +884,11 @@ g_key_file_load_from_fd (GKeyFile *key_file, return FALSE; } + key_file->gettext_domain = g_key_file_get_string (key_file, + G_KEY_FILE_DESKTOP_GROUP, + G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN, + NULL); + return TRUE; } @@ -988,6 +1001,11 @@ g_key_file_load_from_data (GKeyFile *key_file, return FALSE; } + key_file->gettext_domain = g_key_file_get_string (key_file, + G_KEY_FILE_DESKTOP_GROUP, + G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN, + NULL); + return TRUE; } @@ -2230,6 +2248,8 @@ g_key_file_get_locale_string (GKeyFile *key_file, GError *key_file_error; gchar **languages; gboolean free_languages = FALSE; + gboolean try_gettext = FALSE; + const gchar *msg_locale; gint i; g_return_val_if_fail (key_file != NULL, NULL); @@ -2251,6 +2271,23 @@ g_key_file_get_locale_string (GKeyFile *key_file, free_languages = FALSE; } + /* we're only interested in gettext translation if we don't have a + * translation in the .desktop file itself and if the key is one of the keys + * we know we want to translate: Name, GenericName, Comment. Blindly doing + * this for all keys can give strange result for the icons, since the Icon is + * a locale string in the spec, eg. We also only get translation in the mo + * file if the requested locale is the LC_MESSAGES one. Ideally, we should do + * more and change LC_MESSAGES to use the requested locale, but there's no + * guarantee it's installed on the system and it might have some + * side-effects. Since this is a corner case, let's ignore it. */ + + msg_locale = setlocale (LC_MESSAGES, NULL); + try_gettext = msg_locale && key_file->gettext_domain && + strcmp (group_name, G_KEY_FILE_DESKTOP_GROUP) == 0 && + (strcmp (key, G_KEY_FILE_DESKTOP_KEY_NAME) == 0 || + strcmp (key, G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME) == 0 || + strcmp (key, G_KEY_FILE_DESKTOP_KEY_COMMENT) == 0); + for (i = 0; languages[i]; i++) { candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]); @@ -2264,6 +2301,39 @@ g_key_file_get_locale_string (GKeyFile *key_file, break; } + /* Fallback to gettext */ + if (try_gettext && !translated_value) + { + gchar *orig_value = g_key_file_get_string (key_file, group_name, key, NULL); + + if (orig_value) + { + gboolean codeset_set; + const gchar *translated; + gboolean has_gettext; + + codeset_set = bind_textdomain_codeset (key_file->gettext_domain, "UTF-8") != NULL; + translated = NULL; + + translated = g_dgettext (key_file->gettext_domain, + orig_value); + has_gettext = translated != orig_value; + + g_free (orig_value); + + if (has_gettext) + { + if (codeset_set) + translated_value = g_strdup (translated); + else + translated_value = g_locale_to_utf8 (translated, + -1, NULL, NULL, NULL); + } + else + translated_value = NULL; + } + } + /* Fallback to untranslated key */ if (!translated_value) diff --git a/glib/gkeyfile.h b/glib/gkeyfile.h index 9d026d681..7ac48f1e1 100644 --- a/glib/gkeyfile.h +++ b/glib/gkeyfile.h @@ -322,6 +322,7 @@ gboolean g_key_file_remove_group (GKeyFile *key_file, #define G_KEY_FILE_DESKTOP_KEY_URL "URL" #define G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE "DBusActivatable" #define G_KEY_FILE_DESKTOP_KEY_ACTIONS "Actions" +#define G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN "X-GNOME-Gettext-Domain" #define G_KEY_FILE_DESKTOP_TYPE_APPLICATION "Application" #define G_KEY_FILE_DESKTOP_TYPE_LINK "Link" -- 2.43.2
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor