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




commit 7131bc7ad3062a7227a3f203b4eb15cf0fc52e47
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 and supported by the dictionaries and spell checkers found
    by Enchant, 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 (<language_code>)` to the caller.
    
    Also add `GSPELL_AVAILABLE_IN_1_10` since we are adding new API for the next
    release series

 gspell/gspell-language.c | 47 +++++++++++++++++++++++++++++++++++++----------
 gspell/gspell-language.h |  3 +++
 gspell/gspell-version.h  |  1 +
 3 files changed, 41 insertions(+), 10 deletions(-)
---
diff --git a/gspell/gspell-language.c b/gspell/gspell-language.c
index 56351aa..291dd69 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,23 @@ gspell_language_free (GspellLanguage *language)
        g_return_if_fail (language != NULL);
 }
 
+/**
+ * gspell_language_get_name_from_code:
+ * @language_code: a language code.
+ *
+ * Returns: a @name corresponding to @language_code which is supported
+ * by the dictionaries and spell checkers found by Enchant, or
+ * `unknown (@language_code)` 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_printf ("unknown (%s)", language_code);
+}
+
 /* 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 */
diff --git a/gspell/gspell-version.h b/gspell/gspell-version.h
index 6709177..8cec8df 100644
--- a/gspell/gspell-version.h
+++ b/gspell/gspell-version.h
@@ -36,6 +36,7 @@ G_BEGIN_DECLS
 #define GSPELL_AVAILABLE_IN_1_2 _GSPELL_EXTERN
 #define GSPELL_AVAILABLE_IN_1_4 _GSPELL_EXTERN
 #define GSPELL_AVAILABLE_IN_1_6 _GSPELL_EXTERN
+#define GSPELL_AVAILABLE_IN_1_10 _GSPELL_EXTERN
 
 G_END_DECLS
 


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