[glib] gdesktopappinfo: Add g_desktop_app_info_get_locale_string()



commit a55bfeee418c9feea734dc7b1063c065be01de15
Author: Florian Müllner <fmuellner gnome org>
Date:   Sun Jan 7 12:14:46 2018 +0100

    gdesktopappinfo: Add g_desktop_app_info_get_locale_string()
    
    Custom desktop file fields may be translated, but there is currently
    no non-hacky way to look up the localized value; fill get gap with
    a small wrapper around g_key_file_get_locale_string().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=779413

 docs/reference/gio/gio-sections.txt |    1 +
 gio/gdesktopappinfo.c               |   27 +++++++++++++++++++++++++++
 gio/gdesktopappinfo.h               |    3 +++
 gio/tests/appinfo-test.desktop      |    2 ++
 gio/tests/desktop-app-info.c        |   21 +++++++++++++++++++++
 5 files changed, 54 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index c72f0ee..277ca61 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -1609,6 +1609,7 @@ g_desktop_app_info_get_keywords
 g_desktop_app_info_get_startup_wm_class
 g_desktop_app_info_set_desktop_env
 g_desktop_app_info_get_string
+g_desktop_app_info_get_locale_string
 g_desktop_app_info_get_boolean
 g_desktop_app_info_has_key
 GDesktopAppLaunchCallback
diff --git a/gio/gdesktopappinfo.c b/gio/gdesktopappinfo.c
index 125d7cf..26bc53b 100644
--- a/gio/gdesktopappinfo.c
+++ b/gio/gdesktopappinfo.c
@@ -4417,6 +4417,33 @@ g_desktop_app_info_get_string (GDesktopAppInfo *info,
 }
 
 /**
+ * g_desktop_app_info_get_locale_string:
+ * @info: a #GDesktopAppInfo
+ * @key: the key to look up
+ *
+ * Looks up a localized string value in the keyfile backing @info
+ * translated to the current locale.
+ *
+ * The @key is looked up in the "Desktop Entry" group.
+ *
+ * Returns: (nullable): a newly allocated string, or %NULL if the key
+ *     is not found
+ *
+ * Since: 2.56
+ */
+char *
+g_desktop_app_info_get_locale_string (GDesktopAppInfo *info,
+                                      const char      *key)
+{
+  g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
+  g_return_val_if_fail (key != NULL && *key != '\0', NULL);
+
+  return g_key_file_get_locale_string (info->keyfile,
+                                       G_KEY_FILE_DESKTOP_GROUP,
+                                       key, NULL, NULL);
+}
+
+/**
  * g_desktop_app_info_get_boolean:
  * @info: a #GDesktopAppInfo
  * @key: the key to look up
diff --git a/gio/gdesktopappinfo.h b/gio/gdesktopappinfo.h
index 7be940b..a2df3dd 100644
--- a/gio/gdesktopappinfo.h
+++ b/gio/gdesktopappinfo.h
@@ -82,6 +82,9 @@ gboolean         g_desktop_app_info_has_key           (GDesktopAppInfo *info,
 GLIB_AVAILABLE_IN_2_36
 char *           g_desktop_app_info_get_string        (GDesktopAppInfo *info,
                                                        const char      *key);
+GLIB_AVAILABLE_IN_2_56
+char *           g_desktop_app_info_get_locale_string (GDesktopAppInfo *info,
+                                                       const char      *key);
 GLIB_AVAILABLE_IN_2_36
 gboolean         g_desktop_app_info_get_boolean       (GDesktopAppInfo *info,
                                                        const char      *key);
diff --git a/gio/tests/appinfo-test.desktop b/gio/tests/appinfo-test.desktop
index 6c9a85c..90e4120 100644
--- a/gio/tests/appinfo-test.desktop
+++ b/gio/tests/appinfo-test.desktop
@@ -15,3 +15,5 @@ StartupWMClass=appinfo-class
 MimeType=image/png;image/jpeg;
 Keywords=keyword1;test keyword;
 Categories=GNOME;GTK;
+X-JunkFood=Burger
+X-JunkFood[de]=Bratwurst
diff --git a/gio/tests/desktop-app-info.c b/gio/tests/desktop-app-info.c
index 3086da4..d1b68c4 100644
--- a/gio/tests/desktop-app-info.c
+++ b/gio/tests/desktop-app-info.c
@@ -17,6 +17,8 @@
  * Author: Matthias Clasen
  */
 
+#include <locale.h>
+
 #include <glib/glib.h>
 #include <glib/gstdio.h>
 #include <gio/gio.h>
@@ -346,9 +348,14 @@ static void
 test_extra_getters (void)
 {
   GDesktopAppInfo *appinfo;
+  const gchar *lang;
   gchar *s;
   gboolean b;
 
+  lang = setlocale (LC_ALL, NULL);
+  g_setenv ("LANGUAGE", "de_DE.UTF8", TRUE);
+  setlocale (LC_ALL, "");
+
   appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", 
NULL));
   g_assert (appinfo != NULL);
 
@@ -359,10 +366,24 @@ test_extra_getters (void)
   g_assert_cmpstr (s, ==, "appinfo-class");
   g_free (s);
 
+  s = g_desktop_app_info_get_locale_string (appinfo, "X-JunkFood");
+  g_assert_cmpstr (s, ==, "Bratwurst");
+  g_free (s);
+
+  g_setenv ("LANGUAGE", "sv_SV.UTF8", TRUE);
+  setlocale (LC_ALL, "");
+
+  s = g_desktop_app_info_get_locale_string (appinfo, "X-JunkFood");
+  g_assert_cmpstr (s, ==, "Burger"); /* fallback */
+  g_free (s);
+
   b = g_desktop_app_info_get_boolean (appinfo, "Terminal");
   g_assert (b);
 
   g_object_unref (appinfo);
+
+  g_setenv ("LANGUAGE", lang, TRUE);
+  setlocale (LC_ALL, "");
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]