diff --git a/src/as-context.c b/src/as-context.c index cade065d3..9ce9b83f4 100644 --- a/src/as-context.c +++ b/src/as-context.c @@ -513,7 +513,7 @@ as_context_localized_ht_set (AsContext *ctx, GHashTable *lht, const gchar *value if (selected_locale == NULL) selected_locale = "C"; - locale_noenc = as_locale_strip_encoding (g_strdup (selected_locale)); + locale_noenc = as_locale_strip_encoding (selected_locale); g_hash_table_insert (lht, g_ref_string_new_intern (locale_noenc), g_strdup (value)); diff --git a/src/as-utils-private.h b/src/as-utils-private.h index 82e7158f2..4cdafacf8 100644 --- a/src/as-utils-private.h +++ b/src/as-utils-private.h @@ -136,7 +136,10 @@ AS_INTERNAL_VISIBLE gboolean as_copy_file (const gchar *source, const gchar *destination, GError **error); gboolean as_is_cruft_locale (const gchar *locale); -gchar *as_locale_strip_encoding (gchar *locale); + +AS_INTERNAL_VISIBLE +gchar *as_locale_strip_encoding (const gchar *locale); + gchar *as_utils_locale_to_language (const gchar *locale); gchar *as_get_current_arch (void); diff --git a/src/as-utils.c b/src/as-utils.c index 2b900c92b..566db6f85 100644 --- a/src/as-utils.c +++ b/src/as-utils.c @@ -832,16 +832,12 @@ as_is_cruft_locale (const gchar *locale) * as_locale_strip_encoding: * * Remove the encoding from a locale string. - * The function modifies the string directly. + * The function returns a newly allocated string. */ gchar* -as_locale_strip_encoding (gchar *locale) +as_locale_strip_encoding (const gchar *locale) { - gchar *tmp; - tmp = g_strstr_len (locale, -1, ".UTF-8"); - if (tmp != NULL) - *tmp = '\0'; - return locale; + return as_str_replace (locale, ".UTF-8", ""); } /** diff --git a/src/as-yaml.c b/src/as-yaml.c index 701429975..170d80151 100644 --- a/src/as-yaml.c +++ b/src/as-yaml.c @@ -509,7 +509,7 @@ as_yaml_set_localized_table (AsContext *ctx, GNode *node, GHashTable *l10n_table for (GNode *n = node->children; n != NULL; n = n->next) { const gchar *locale = as_yaml_get_node_locale (ctx, n); if (locale != NULL) { - g_autofree gchar *locale_noenc = as_locale_strip_encoding (g_strdup (locale)); + g_autofree gchar *locale_noenc = as_locale_strip_encoding (locale); g_hash_table_insert (l10n_table, g_ref_string_new_intern (locale_noenc), g_strdup (as_yaml_node_get_value (n))); @@ -641,6 +641,7 @@ as_yaml_emit_sequence_from_str_array (yaml_emitter_t *emitter, const gchar *key, static void as_yaml_localized_list_helper (gchar *key, gchar **strv, yaml_emitter_t *emitter) { + g_autofree gchar *locale_noenc = NULL; guint i; if (strv == NULL) return; @@ -649,7 +650,8 @@ as_yaml_localized_list_helper (gchar *key, gchar **strv, yaml_emitter_t *emitter if (as_is_cruft_locale (key)) return; - as_yaml_emit_scalar (emitter, as_locale_strip_encoding (key)); + locale_noenc = as_locale_strip_encoding (key); + as_yaml_emit_scalar (emitter, locale_noenc); as_yaml_sequence_start (emitter); for (i = 0; strv[i] != NULL; i++) { as_yaml_emit_scalar (emitter, strv[i]); diff --git a/tests/test-misc.c b/tests/test-misc.c index 4e9bca8de..0d7bdce50 100644 --- a/tests/test-misc.c +++ b/tests/test-misc.c @@ -21,6 +21,7 @@ #include #include "appstream.h" #include "as-news-convert.h" +#include "as-utils-private.h" #include "as-test-utils.h" @@ -192,6 +193,22 @@ test_readwrite_text_news () g_free (tmp); } +static void +test_locale_strip_encoding () +{ + g_autofree gchar *c = NULL; + g_autofree gchar *cutf8 = NULL; + g_autofree gchar *cutf8valencia = NULL; + + c = as_locale_strip_encoding ("C"); + cutf8 = as_locale_strip_encoding ("C.UTF-8"); + cutf8valencia = as_locale_strip_encoding ("C.UTF-8@valencia"); + + g_assert_cmpstr (c, ==, "C"); + g_assert_cmpstr (cutf8, ==, "C"); + g_assert_cmpstr (cutf8valencia, ==, "C@valencia"); +} + int main (int argc, char **argv) { @@ -214,6 +231,7 @@ main (int argc, char **argv) g_test_add_func ("/AppStream/Misc/YAMLNews", test_readwrite_yaml_news); g_test_add_func ("/AppStream/Misc/TextNews", test_readwrite_text_news); + g_test_add_func ("/AppStream/Misc/StripLocaleEncoding", test_locale_strip_encoding); ret = g_test_run (); g_free (datadir);