[gspell/wip/default-language] language: add get_default() function
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gspell/wip/default-language] language: add get_default() function
- Date: Thu, 14 Jan 2016 11:50:07 +0000 (UTC)
commit 985b023ef490002450afd66f342ced3e5730e45b
Author: Sébastien Wilmet <swilmet gnome org>
Date: Thu Jan 14 12:44:32 2016 +0100
language: add get_default() function
It's a bit stupid to create a GspellChecker with a NULL language just to
know the default language. So make the function available in
GspellLanguage.
Improve also the OS X specific code, since a lookup() can return NULL if
not found.
docs/reference/gspell-1.0-sections.txt | 1 +
gspell/gspell-checker.c | 54 +-------------------------
gspell/gspell-language.c | 67 +++++++++++++++++++++++++++++++-
gspell/gspell-language.h | 5 ++-
4 files changed, 72 insertions(+), 55 deletions(-)
---
diff --git a/docs/reference/gspell-1.0-sections.txt b/docs/reference/gspell-1.0-sections.txt
index f7adf88..3df8162 100644
--- a/docs/reference/gspell-1.0-sections.txt
+++ b/docs/reference/gspell-1.0-sections.txt
@@ -26,6 +26,7 @@ gspell_checker_error_quark
<TITLE>GspellLanguage</TITLE>
GspellLanguage
gspell_language_get_available
+gspell_language_get_default
gspell_language_lookup
gspell_language_get_code
gspell_language_get_name
diff --git a/gspell/gspell-checker.c b/gspell/gspell-checker.c
index 6ff989a..ee9d3cb 100644
--- a/gspell/gspell-checker.c
+++ b/gspell/gspell-checker.c
@@ -272,58 +272,6 @@ gspell_checker_new (const GspellLanguage *language)
NULL);
}
-static const GspellLanguage *
-get_default_language (void)
-{
- const GspellLanguage *lang;
- const gchar * const *lang_names;
- const GList *langs;
- gint i;
-
- /* Try with the current locale */
- lang_names = g_get_language_names ();
-
- for (i = 0; lang_names[i] != NULL; i++)
- {
- lang = gspell_language_lookup (lang_names[i]);
-
- if (lang != NULL)
- {
- return lang;
- }
- }
-
- /* Another try specific to Mac OS X */
-#ifdef OS_OSX
- {
- gchar *code = _gspell_osx_get_preferred_spell_language ();
-
- if (code != NULL)
- {
- lang = gspell_language_lookup (code);
- g_free (code);
- return lang;
- }
- }
-#endif
-
- /* Try English */
- lang = gspell_language_lookup ("en_US");
- if (lang != NULL)
- {
- return lang;
- }
-
- /* Take the first available language */
- langs = gspell_language_get_available ();
- if (langs != NULL)
- {
- return langs->data;
- }
-
- return NULL;
-}
-
static gboolean
init_dictionary (GspellChecker *checker)
{
@@ -341,7 +289,7 @@ init_dictionary (GspellChecker *checker)
if (priv->active_lang == NULL)
{
- priv->active_lang = get_default_language ();
+ priv->active_lang = gspell_language_get_default ();
}
if (priv->active_lang != NULL)
diff --git a/gspell/gspell-language.c b/gspell/gspell-language.c
index 76a84b2..1a4a477 100644
--- a/gspell/gspell-language.c
+++ b/gspell/gspell-language.c
@@ -3,7 +3,7 @@
*
* Copyright 2006 - Paolo Maggi
* Copyright 2008 - Novell, Inc.
- * Copyright 2015 - Sébastien Wilmet
+ * Copyright 2015, 2016 - Sébastien Wilmet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -24,6 +24,7 @@
#include <string.h>
#include <glib/gi18n-lib.h>
#include <enchant.h>
+#include "gspell-osx.h"
/**
* SECTION:language
@@ -409,6 +410,70 @@ gspell_language_get_available (void)
}
/**
+ * gspell_language_get_default:
+ *
+ * Finds the best available language based on the current locale.
+ *
+ * Returns: (nullable): the default #GspellLanguage, or %NULL if no dictionaries
+ * are available.
+ */
+const GspellLanguage *
+gspell_language_get_default (void)
+{
+ const GspellLanguage *lang;
+ const gchar * const *lang_names;
+ const GList *langs;
+ gint i;
+
+ /* Try with the current locale */
+ lang_names = g_get_language_names ();
+
+ for (i = 0; lang_names[i] != NULL; i++)
+ {
+ lang = gspell_language_lookup (lang_names[i]);
+
+ if (lang != NULL)
+ {
+ return lang;
+ }
+ }
+
+ /* Another try specific to Mac OS X */
+#ifdef OS_OSX
+ {
+ gchar *code = _gspell_osx_get_preferred_spell_language ();
+
+ if (code != NULL)
+ {
+ lang = gspell_language_lookup (code);
+ g_free (code);
+
+ if (lang != NULL)
+ {
+ return lang;
+ }
+ }
+ }
+#endif
+
+ /* Try English */
+ lang = gspell_language_lookup ("en_US");
+ if (lang != NULL)
+ {
+ return lang;
+ }
+
+ /* Take the first available language */
+ langs = gspell_language_get_available ();
+ if (langs != NULL)
+ {
+ return langs->data;
+ }
+
+ return NULL;
+}
+
+/**
* gspell_language_lookup:
* @language_code: a language code.
*
diff --git a/gspell/gspell-language.h b/gspell/gspell-language.h
index f9df49e..076c929 100644
--- a/gspell/gspell-language.h
+++ b/gspell/gspell-language.h
@@ -3,7 +3,7 @@
*
* Copyright 2006 - Paolo Maggi
* Copyright 2008 - Novell, Inc.
- * Copyright 2015 - Sébastien Wilmet
+ * Copyright 2015, 2016 - Sébastien Wilmet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -44,6 +44,9 @@ GType gspell_language_get_type (void) G_GNUC_CONST;
const GList * gspell_language_get_available (void);
const GspellLanguage *
+ gspell_language_get_default (void);
+
+const GspellLanguage *
gspell_language_lookup (const gchar *language_code);
const gchar * gspell_language_get_code (const GspellLanguage *language);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]