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
This commit is contained in:
Florian Müllner 2018-01-07 12:14:46 +01:00
parent 84350cb566
commit a55bfeee41
5 changed files with 54 additions and 0 deletions

View File

@ -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

View File

@ -4416,6 +4416,33 @@ g_desktop_app_info_get_string (GDesktopAppInfo *info,
G_KEY_FILE_DESKTOP_GROUP, key, NULL);
}
/**
* 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

View File

@ -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);

View File

@ -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

View File

@ -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