diff options
author | Richard Hughes <richard@hughsie.com> | 2016-06-29 14:51:29 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2016-06-29 14:51:29 +0100 |
commit | 267f0f0bfad3cb47abca579fa7bb49bed438946f (patch) | |
tree | c1062afb9b416d409dbe89ea4125c574daa4d157 | |
parent | 0eb52a724adaff5ab666a42567f157caf5e45f6c (diff) | |
download | appstream-glib-267f0f0bfad3cb47abca579fa7bb49bed438946f.tar.gz |
Fall back to the country code in as_app_get_language()
-rw-r--r-- | libappstream-glib/as-app.c | 25 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 3 |
2 files changed, 22 insertions, 6 deletions
diff --git a/libappstream-glib/as-app.c b/libappstream-glib/as-app.c index 4b5449b..2755e48 100644 --- a/libappstream-glib/as-app.c +++ b/libappstream-glib/as-app.c @@ -1398,16 +1398,29 @@ gint as_app_get_language (AsApp *app, const gchar *locale) { AsAppPrivate *priv = GET_PRIVATE (app); - gboolean ret; gpointer value = NULL; + g_auto(GStrv) lang = NULL; + /* fallback */ if (locale == NULL) locale = "C"; - ret = g_hash_table_lookup_extended (priv->languages, - locale, NULL, &value); - if (!ret) - return -1; - return GPOINTER_TO_INT (value); + + /* look up full locale */ + if (g_hash_table_lookup_extended (priv->languages, + locale, NULL, &value)) { + return GPOINTER_TO_INT (value); + } + + /* look up just country code */ + lang = g_strsplit (locale, "_", 2); + if (g_strv_length (lang) == 2 && + g_hash_table_lookup_extended (priv->languages, + lang[0], NULL, &value)) { + return GPOINTER_TO_INT (value); + } + + /* failed */ + return -1; } /** diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 6b66730..c5e404b 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -348,6 +348,9 @@ as_test_app_builder_gettext_func (void) g_assert_cmpint (as_app_get_language (app, "ru"), ==, 33); g_assert_cmpint (as_app_get_language (app, "fr_FR"), ==, -1); + /* check fallback */ + g_assert_cmpint (as_app_get_language (app, "ru_RU"), ==, 33); + /* check size */ list = as_app_get_languages (app); g_assert_cmpint (g_list_length (list), ==, 2); |