gnome-desktop/gnome-desktop-fate300461-desktop-gettext.patch

135 lines
4.4 KiB
Diff

Add support of gettext for desktop entry files.
We only support this for the following keys: Name, GenericName, Comment. We
don't support all keys because it can create issues for the Icon key (which is
localizable -- which can result in a broken icon).
Translations that are present in the desktop entry take precedence over
translations via gettext. If we don't do this, then user modifications won't
appear since they will have lower precedence.
Index: gnome-desktop-2.23.4/libgnome-desktop/gnome-desktop-item.c
===================================================================
--- gnome-desktop-2.23.4.orig/libgnome-desktop/gnome-desktop-item.c
+++ gnome-desktop-2.23.4/libgnome-desktop/gnome-desktop-item.c
@@ -84,6 +84,7 @@ struct _GnomeDesktopItem {
GHashTable *main_hash;
char *location;
+ const char *gettext_domain;
time_t mtime;
@@ -139,6 +140,8 @@ static GnomeDesktopItem *gnome_desktop_i
static void update_recently_used_apps (const GnomeDesktopItem *item);
+static const char *lookup (const GnomeDesktopItem *item, const char *key);
+
static int
readbuf_getc (ReadBuf *rb)
{
@@ -399,6 +402,7 @@ gnome_desktop_item_new (void)
"1.0");
retval->launch_time = 0;
+ retval->gettext_domain = NULL;
return retval;
}
@@ -477,6 +481,8 @@ gnome_desktop_item_copy (const GnomeDesk
copy_string_hash,
retval->main_hash);
+ retval->gettext_domain = lookup (retval, GNOME_DESKTOP_ITEM_GETTEXT_DOMAIN);
+
return retval;
}
@@ -924,6 +930,9 @@ gnome_desktop_item_unref (GnomeDesktopIt
g_free (item->location);
item->location = NULL;
+ /* no need to free it, it's a const key */
+ item->gettext_domain = NULL;
+
g_free (item);
}
@@ -999,16 +1008,51 @@ lookup (const GnomeDesktopItem *item, co
static const char *
lookup_locale (const GnomeDesktopItem *item, const char *key, const char *locale)
{
+ const char *ret;
+
+ ret = NULL;
+
if (locale == NULL ||
strcmp (locale, "C") == 0) {
- return lookup (item, key);
+ ret = lookup (item, key);
} else {
- const char *ret;
char *full = g_strdup_printf ("%s[%s]", key, locale);
ret = lookup (item, full);
g_free (full);
- return ret;
}
+
+ /* 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. */
+ if (!ret && item->gettext_domain &&
+ (strcmp (key, GNOME_DESKTOP_ITEM_NAME) == 0 ||
+ strcmp (key, GNOME_DESKTOP_ITEM_GENERIC_NAME) == 0 ||
+ strcmp (key, GNOME_DESKTOP_ITEM_COMMENT) == 0)) {
+ const char *msg_locale = setlocale (LC_MESSAGES, NULL);
+
+ /* 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. */
+ if (msg_locale && locale &&
+ strcmp (msg_locale, locale) == 0) {
+ const char *value = lookup (item, key);
+ if (value != NULL && value[0] != '\0') {
+ ret = dgettext (item->gettext_domain, value);
+ /* don't accept no translation, since we might
+ * have something better later, with another
+ * locale */
+ if (ret == value)
+ ret = NULL;
+ }
+ }
+ }
+
+ return ret;
}
static const char *
@@ -4039,6 +4083,8 @@ ditem_load (ReadBuf *rb,
readbuf_close (rb);
+ item->gettext_domain = lookup (item, GNOME_DESKTOP_ITEM_GETTEXT_DOMAIN);
+
return item;
}
Index: gnome-desktop-2.23.4/libgnome-desktop/libgnome/gnome-desktop-item.h
===================================================================
--- gnome-desktop-2.23.4.orig/libgnome-desktop/libgnome/gnome-desktop-item.h
+++ gnome-desktop-2.23.4/libgnome-desktop/libgnome/gnome-desktop-item.h
@@ -98,6 +98,7 @@ typedef struct _GnomeDesktopItem GnomeDe
#define GNOME_DESKTOP_ITEM_DOC_PATH "X-GNOME-DocPath" /* string */
#define GNOME_DESKTOP_ITEM_SUBSTITUTEUID "X-KDE-SubstituteUID" /*boolean*/
#define GNOME_DESKTOP_ITEM_ROOT_ONLY "X-KDE-RootOnly" /*boolean*/
+#define GNOME_DESKTOP_ITEM_GETTEXT_DOMAIN "X-SUSE-Gettext-Domain" /* string */
/* The vfolder proposal */
#define GNOME_DESKTOP_ITEM_CATEGORIES "Categories" /* string */
#define GNOME_DESKTOP_ITEM_ONLY_SHOW_IN "OnlyShowIn" /* string */