[gtranslator] Replace GtkSpell with Gspell



commit 336bca68ad164158199a3c2b7f470bc3cc79c416
Author: Daniel GarcĂ­a Moreno <danigm wadobo com>
Date:   Tue Sep 18 17:30:52 2018 +0200

    Replace GtkSpell with Gspell
    
    Fix #18

 build-aux/flatpak/org.gnome.Gtranslator.json |  34 ++++++
 meson.build                                  |   1 +
 src/gtr-header.c                             |  10 ++
 src/gtr-header.h                             |   2 +
 src/gtr-tab.c                                |  11 +-
 src/gtr-view.c                               | 172 ++++++++++++++++++---------
 src/gtr-view.h                               |   3 +
 7 files changed, 178 insertions(+), 55 deletions(-)
---
diff --git a/build-aux/flatpak/org.gnome.Gtranslator.json b/build-aux/flatpak/org.gnome.Gtranslator.json
index d73d51bb..f078c3dd 100644
--- a/build-aux/flatpak/org.gnome.Gtranslator.json
+++ b/build-aux/flatpak/org.gnome.Gtranslator.json
@@ -38,6 +38,40 @@
         "*.a"
     ],
     "modules" : [
+        {
+            "name" : "enchant",
+            "cleanup" : [
+                "/bin",
+                "/share"
+            ],
+            "sources" : [
+                {
+                    "type" : "archive",
+                    "url" : 
"https://github.com/AbiWord/enchant/releases/download/v2.1.3/enchant-2.1.3.tar.gz";,
+                    "sha256" : "086f37cdecd42eacd0e1dc291f5410d9ca2c5ed2cd9cd9367729e3d2d18a8550"
+                }
+            ]
+        },
+        {
+            "name" : "gspell",
+            "config-opts" : [
+                "--disable-gtk-doc",
+                "--disable-gtk-doc-html"
+            ],
+            "cleanup" : [
+                "/bin",
+                "/include",
+                "/lib/pkgconfig",
+                "*.la",
+                "/share"
+            ],
+            "sources" : [
+                {
+                    "type" : "git",
+                    "url" : "https://gitlab.gnome.org/GNOME/gspell.git";
+                }
+            ]
+        },
         {
             "name" : "gtksourceview",
             "config-opts" : [
diff --git a/meson.build b/meson.build
index 1195b6c9..b7712914 100644
--- a/meson.build
+++ b/meson.build
@@ -122,6 +122,7 @@ gtranslator_deps = [
   gtk_dep,
   glib_dep,
   gda_dep,
+  dependency('gspell-1', version: '>= 1.2.0'),
   dependency('libxml-2.0', version: '>=2.4.12'),
   dependency('gthread-2.0', version: '>=2.13.0'),
   dependency('gio-2.0', version: '>=2.36.0'),
diff --git a/src/gtr-header.c b/src/gtr-header.c
index 78528f0e..28ed203d 100644
--- a/src/gtr-header.c
+++ b/src/gtr-header.c
@@ -386,6 +386,16 @@ gtr_header_get_language (GtrHeader * header)
   return language;
 }
 
+gchar *
+gtr_header_get_language_code (GtrHeader * header)
+{
+  gchar *language;
+  g_return_val_if_fail (GTR_IS_HEADER (header), NULL);
+  language = po_header_field (gtr_msg_get_msgstr (GTR_MSG (header)),
+                              "Language");
+  return language;
+}
+
 gchar *
 gtr_header_get_lg_email (GtrHeader * header)
 {
diff --git a/src/gtr-header.h b/src/gtr-header.h
index 6495bf74..83efb306 100644
--- a/src/gtr-header.h
+++ b/src/gtr-header.h
@@ -112,6 +112,8 @@ void             gtr_header_set_profile              (GtrHeader  *header,
                                                       GtrProfile *profile);
 GtrProfile      *gtr_header_get_profile              (GtrHeader *header);
 
+gchar           *gtr_header_get_language_code        (GtrHeader * header);
+
 G_END_DECLS
 
 #endif /* __HEADER_H__ */
diff --git a/src/gtr-tab.c b/src/gtr-tab.c
index 0eb16890..12461bc8 100644
--- a/src/gtr-tab.c
+++ b/src/gtr-tab.c
@@ -337,7 +337,9 @@ gtr_tab_append_msgstr_page (const gchar * tab_label,
   if (spellcheck &&
       g_settings_get_boolean (priv->editor_settings,
                               GTR_SETTINGS_SPELLCHECK))
-    gtr_view_enable_spellcheck (GTR_VIEW (widget), spellcheck);
+    {
+      gtr_view_enable_spellcheck (GTR_VIEW (widget), spellcheck);
+    }
 
   gtk_container_add (GTK_CONTAINER (scroll), widget);
 
@@ -534,6 +536,7 @@ gtr_tab_add_msgstr_tabs (GtrTab * tab)
   gchar *label;
   GtkTextBuffer *buf;
   gint i = 0;
+  gchar *lang_code = NULL;
 
   priv = gtr_tab_get_instance_private (tab);
 
@@ -541,14 +544,19 @@ gtr_tab_add_msgstr_tabs (GtrTab * tab)
    * We get the header of the po file
    */
   header = gtr_po_get_header (priv->po);
+  lang_code = gtr_header_get_language_code (header);
 
   do
     {
+
       label = g_strdup_printf (_("Plural %d"), i);
       priv->trans_msgstr[i] = gtr_tab_append_msgstr_page (label,
                                                           priv->trans_notebook,
                                                           TRUE,
                                                           tab);
+
+      gtr_view_set_language (GTR_VIEW (priv->trans_msgstr[i]), lang_code);
+
       buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->trans_msgstr[i]));
       g_signal_connect (buf, "end-user-action",
                         G_CALLBACK (gtr_message_translation_update), tab);
@@ -561,6 +569,7 @@ gtr_tab_add_msgstr_tabs (GtrTab * tab)
       g_free (label);
     }
   while (i < gtr_header_get_nplurals (header));
+  g_free (lang_code);
 }
 
 static void
diff --git a/src/gtr-view.c b/src/gtr-view.c
index fcaeb432..f18e4739 100644
--- a/src/gtr-view.c
+++ b/src/gtr-view.c
@@ -40,9 +40,45 @@
 #include <gtk/gtk.h>
 
 #include <gtksourceview/gtksource.h>
-#ifdef HAVE_GTKSPELL
-#include <gtkspell/gtkspell.h>
-#endif
+
+#include <gspell/gspell.h>
+
+/**
+ * Converts the language code to a complete language code with the country
+ * If the language contains the country code this returns a new allocated
+ * string copied from *lang*.
+ *
+ * In other case the code is duplicated by default:
+ *
+ * es -> es_ES
+ * pt -> pt_PT
+ */
+static gchar*
+get_default_lang (const gchar *lang) {
+  gchar *up;
+  gchar *ret;
+
+  if (g_strrstr (lang, "_"))
+    {
+      return g_strdup (lang);
+    }
+
+  up = g_ascii_strup (lang, -1);
+  ret = g_strdup_printf ("%s_%s", lang, up);
+  g_free (up);
+
+  return ret;
+}
+
+static void
+inline_spellcheck (GObject *object,
+                   GParamSpec *param,
+                   GtrView *view)
+{
+  GspellTextView *gspell_view;
+  gspell_view = gspell_text_view_get_from_gtk_text_view (GTK_TEXT_VIEW (view));
+  gspell_text_view_set_inline_spell_checking (gspell_view, TRUE);
+}
 
 typedef struct
 {
@@ -54,44 +90,11 @@ typedef struct
   guint search_flags;
   gchar *search_text;
 
-#ifdef HAVE_GTKSPELL
-  GtkSpellChecker *spell;
-#endif
+  GspellChecker *spell;
 } GtrViewPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtrView, gtr_view, GTK_SOURCE_TYPE_VIEW)
 
-#ifdef HAVE_GTKSPELL
-static void
-gtr_attach_gtkspell (GtrView * view)
-{
-  GError *error = NULL;
-  GtrViewPrivate *priv;
-  gchar *errortext = NULL;
-
-  priv = gtr_view_get_instance_private (view);
-
-  priv->spell = gtk_spell_checker_new ();
-  gtk_spell_checker_set_language (priv->spell, NULL, &error);
-  if (error)
-    {
-      g_warning (_("gtkspell error: %s\n"), error->message);
-      errortext =
-        g_strdup_printf (_("GtkSpell was unable to initialize.\n %s"),
-                         error->message);
-      g_warning ("%s", errortext);
-
-      g_error_free (error);
-      g_free (errortext);
-    }
-  else
-    {
-      gtk_spell_checker_attach (priv->spell,
-                                GTK_TEXT_VIEW (view));
-    }
-}
-#endif
-
 static void
 gtr_view_init (GtrView * view)
 {
@@ -105,6 +108,7 @@ gtr_view_init (GtrView * view)
 
   priv = gtr_view_get_instance_private (view);
 
+  priv->spell = NULL;
   priv->editor_settings = g_settings_new ("org.gnome.gtranslator.preferences.editor");
   priv->ui_settings = g_settings_new ("org.gnome.gtranslator.preferences.ui");
 
@@ -155,6 +159,7 @@ gtr_view_dispose (GObject * object)
 
   g_clear_object (&priv->editor_settings);
   g_clear_object (&priv->ui_settings);
+  g_clear_object (&priv->spell);
 
   G_OBJECT_CLASS (gtr_view_parent_class)->dispose (object);
 }
@@ -222,6 +227,67 @@ gtr_view_get_selected_text (GtrView * view,
   return TRUE;
 }
 
+void
+gtr_view_set_language (GtrView *view,
+                       const gchar *lang)
+{
+  GtrViewPrivate *priv = gtr_view_get_instance_private (view);
+  GList *langs = (GList *)gspell_language_get_available ();
+  gchar **lang_parts = NULL;
+  gboolean found = FALSE;
+  gchar *def_lang = get_default_lang (lang);
+
+  while (langs)
+    {
+      GspellLanguage *l = (GspellLanguage*) langs->data;
+      const gchar *code = gspell_language_get_code (l);
+      if (g_strcmp0 (def_lang, code) == 0)
+        {
+          gspell_checker_set_language (priv->spell, l);
+          // If we found the language exacly, we're finished
+          found = TRUE;
+          break;
+        }
+
+      langs = g_list_next (langs);
+    }
+
+  g_free (def_lang);
+
+  if (found)
+    return;
+
+  // Not found, trying again, but this time only with the first part of
+  // the language code
+
+  langs = (GList *)gspell_language_get_available ();
+  lang_parts = g_strsplit (lang, "_", 2);
+  while (langs)
+    {
+      GspellLanguage *l = (GspellLanguage*) langs->data;
+      const gchar *code = gspell_language_get_code (l);
+      gchar **parts = g_strsplit (code, "_", 2);
+      if (parts[0] && g_strcmp0 (parts[0], lang_parts[0]) == 0)
+        {
+          gspell_checker_set_language (priv->spell, l);
+          g_strfreev (parts);
+          found = TRUE;
+          break;
+        }
+      g_strfreev (parts);
+
+      langs = g_list_next (langs);
+    }
+  g_strfreev (lang_parts);
+
+  if (!found)
+    {
+      GspellTextView *gspell_view;
+      gspell_view = gspell_text_view_get_from_gtk_text_view (GTK_TEXT_VIEW (view));
+      gspell_text_view_set_inline_spell_checking (gspell_view, FALSE);
+    }
+}
+
 /**
  * gtr_view_enable_spellcheck:
  * @view: a #GtrView
@@ -232,26 +298,24 @@ gtr_view_get_selected_text (GtrView * view,
 void
 gtr_view_enable_spellcheck (GtrView * view, gboolean enable)
 {
-#ifdef HAVE_GTKSPELL
   GtrViewPrivate *priv;
-#endif
+  GspellTextView *gspell_view;
+  GtkTextBuffer *gtk_buffer;
+  GspellTextBuffer *gspell_buffer;
 
-  if (enable)
-    {
-#ifdef HAVE_GTKSPELL
-      gtr_attach_gtkspell (view);
-#endif
-    }
-  else
-    {
-#ifdef HAVE_GTKSPELL
-      priv = gtr_view_get_instance_private (view);
+  priv = gtr_view_get_instance_private (view);
 
-      if (!priv->spell)
-        return;
-      gtk_spell_checker_detach (priv->spell);
-#endif
-    }
+  priv->spell = gspell_checker_new (NULL);
+  gtk_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+  gspell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (gtk_buffer);
+  gspell_text_buffer_set_spell_checker (gspell_buffer, priv->spell);
+
+  gspell_view = gspell_text_view_get_from_gtk_text_view (GTK_TEXT_VIEW (view));
+  gspell_text_view_set_inline_spell_checking (gspell_view, enable);
+  gspell_text_view_set_enable_language_menu (gspell_view, TRUE);
+
+  g_signal_connect (G_OBJECT (priv->spell), "notify::language",
+                    G_CALLBACK (inline_spellcheck), view);
 }
 
 /**
diff --git a/src/gtr-view.h b/src/gtr-view.h
index 3b48e38c..87ee0e3d 100644
--- a/src/gtr-view.h
+++ b/src/gtr-view.h
@@ -112,6 +112,9 @@ gtr_view_get_type (void)
 
      void gtr_view_reload_scheme_color (GtrView * view);
 
+     void gtr_view_set_language (GtrView *view,
+                                 const gchar *lang);
+
 
 /* Search macros */
 #define GTR_SEARCH_IS_DONT_SET_FLAGS(sflags) ((sflags & GTR_SEARCH_DONT_SET_FLAGS) != 0)


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