[gspell/langs.win] gspell: Add gspell_language_get_name_from_code ()



commit c0ba2faef92fa84fb065efc9b36772e3fd21f27a
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Wed Aug 19 17:30:13 2020 +0800

    gspell: Add gspell_language_get_name_from_code ()
    
    Add a new simple API to retrieve the name that corresponds to a language code
    that is passed in, by re-using the code in gspell_language_lookup() but insist
    that we have an exact match for the language code, otherwise we return
    `unknown` to the caller.

 gspell/gspell-language.c | 44 ++++++++++++++++++++++++++++++++++----------
 gspell/gspell-language.h |  3 +++
 2 files changed, 37 insertions(+), 10 deletions(-)
---
diff --git a/gspell/gspell-language.c b/gspell/gspell-language.c
index 56351aa..22c4456 100644
--- a/gspell/gspell-language.c
+++ b/gspell/gspell-language.c
@@ -197,15 +197,9 @@ gspell_language_get_default (void)
        return NULL;
 }
 
-/**
- * gspell_language_lookup:
- * @language_code: a language code.
- *
- * Returns: (nullable): a #GspellLanguage corresponding to @language_code, or
- * %NULL if not found.
- */
-const GspellLanguage *
-gspell_language_lookup (const gchar *language_code)
+static GspellLanguage *
+lookup_language_internal (const gchar *language_code,
+                          gboolean     use_closest_match)
 {
        const GspellLanguage *closest_match = NULL;
        const GList *available_languages;
@@ -226,7 +220,8 @@ gspell_language_lookup (const gchar *language_code)
                        return language;
                }
 
-               if (g_ascii_strncasecmp (language_code, code, length) == 0)
+               if (use_closest_match &&
+                   g_ascii_strncasecmp (language_code, code, length) == 0)
                {
                        closest_match = language;
                }
@@ -235,6 +230,19 @@ gspell_language_lookup (const gchar *language_code)
        return closest_match;
 }
 
+/**
+ * gspell_language_lookup:
+ * @language_code: a language code.
+ *
+ * Returns: (nullable): a #GspellLanguage corresponding to @language_code, or
+ * %NULL if not found.
+ */
+const GspellLanguage *
+gspell_language_lookup (const gchar *language_code)
+{
+       return lookup_language_internal (language_code, TRUE);
+}
+
 /**
  * gspell_language_get_code:
  * @language: a #GspellLanguage.
@@ -316,4 +324,20 @@ gspell_language_free (GspellLanguage *language)
        g_return_if_fail (language != NULL);
 }
 
+/**
+ * gspell_language_get_name_from_code (const gchar *language_code):
+ * @language_code: a language code.
+ *
+ * Returns: a @name corresponding to @language_code, or
+ * `unknown` if not found. The returned string should
+ * be freed after use
+ */
+gchar *
+gspell_language_get_name_from_code (const gchar *language_code)
+{
+       GspellLanguage *lang = lookup_language_internal (language_code, FALSE);
+
+       return lang != NULL ? g_strdup (lang->name) : g_strdup ("unknown");
+}
+
 /* ex:set ts=8 noet: */
diff --git a/gspell/gspell-language.h b/gspell/gspell-language.h
index 1d83d19..71becd1 100644
--- a/gspell/gspell-language.h
+++ b/gspell/gspell-language.h
@@ -70,6 +70,9 @@ GspellLanguage *gspell_language_copy                  (const GspellLanguage *language);
 GSPELL_AVAILABLE_IN_ALL
 void           gspell_language_free                    (GspellLanguage *language);
 
+GSPELL_AVAILABLE_IN_1_10
+gchar *                gspell_language_get_name_from_code (const gchar *language_code);
+
 G_END_DECLS
 
 #endif /* GSPELL_LANGUAGE_H */


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