From c8ae29f8746253b3eb9febb6f11c9fb018eff0d8 Mon Sep 17 00:00:00 2001 From: Danilo Chang Date: Tue, 28 Mar 2023 20:44:57 +0800 Subject: [PATCH] Add output to simplified Chinese support (using OpenCC) 1. Update configure.ac to enable OpenCC 2. Update setup UI 3. Check flag when commit string --- configure.ac | 14 ++++++++++++++ ibus-array.spec.in | 1 + po/zh_TW.po | 12 ++++++++---- setup/main.py | 25 +++++++++++++++++++++---- src/engine.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 86 insertions(+), 9 deletions(-) diff --git a/configure.ac b/configure.ac index 7974696..a18a96f 100644 --- a/configure.ac +++ b/configure.ac @@ -76,6 +76,20 @@ AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Define to the read-only AM_GNU_GETTEXT([external]) AM_GNU_GETTEXT_VERSION(0.16.1) +AC_ARG_ENABLE(opencc, + AC_HELP_STRING([--enable-opencc], + [Use opencc for simplified Chinese conversion]), + [enable_opencc=$enableval], + [enable_opencc=yes] +) +if test x"$enable_opencc" = x"yes"; then + # check opencc + PKG_CHECK_MODULES(OPENCC, [opencc >= 1.0.0], [ + AC_DEFINE(HAVE_OPENCC, 1, [Define if found opencc]) + CFLAGS="$CFLAGS $OPENCC_CFLAGS" + LDFLAGS="$LDFLAGS $OPENCC_LIBS" + ]) +fi # OUTPUT files AC_CONFIG_FILES(po/Makefile.in diff --git a/ibus-array.spec.in b/ibus-array.spec.in index 437f63b..9f6652f 100644 --- a/ibus-array.spec.in +++ b/ibus-array.spec.in @@ -12,6 +12,7 @@ Source0: https://github.com/lexical/ibus-array/archive/release-%{version}.tar BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) BuildRequires: gettext-devel +BuildRequires: opencc-devel Requires: ibus diff --git a/po/zh_TW.po b/po/zh_TW.po index 8f8bc35..387ba23 100644 --- a/po/zh_TW.po +++ b/po/zh_TW.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: ibus-array 0.2.2\n" "Report-Msgid-Bugs-To: https://github.com/lexical/ibus-array/issues\n" -"POT-Creation-Date: 2023-03-18 17:29+0800\n" +"POT-Creation-Date: 2023-03-28 19:12+0800\n" "PO-Revision-Date: 2019-12-10 22:12+0800\n" "Last-Translator: Anthony Fok \n" "Language-Team: Chinese (traditional)\n" @@ -27,15 +27,15 @@ msgstr "行列" msgid "Array 30 Input Method 行列30輸入法" msgstr "行列30輸入法 Array 30 Input Method" -#: src/engine.c:198 +#: src/engine.c:219 msgid "Setup" msgstr "設定" -#: src/engine.c:199 +#: src/engine.c:220 msgid "Configure Array 30 engine" msgstr "設定行列輸入法" -#: src/engine.c:464 +#: src/engine.c:504 msgid "" "1.comma 2.bracket 3.symbol 4.math 5.arrow 6.unit 7.table 8.roman 9.greek 0." "bopomo" @@ -52,3 +52,7 @@ msgstr "當有特別碼時,顯示提示" #: setup/main.py:56 msgid "Special Code Only Mode" msgstr "當有特別碼時,只能用特別碼輸入 (特別碼練習模式)" + +#: setup/main.py:58 +msgid "Convert output to simplified Chinese" +msgstr "出字時轉為簡體中文" diff --git a/setup/main.py b/setup/main.py index 4b4b5db..f099d70 100644 --- a/setup/main.py +++ b/setup/main.py @@ -19,7 +19,7 @@ # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - + import sys import os import gi @@ -34,7 +34,7 @@ class Setup: def __init__(self, bus): - self.__bus = bus + self.__bus = bus self.__config = self.__bus.get_config() self.__config.connect("value-changed", self.on_value_changed, None) self.__create_ui() @@ -55,14 +55,19 @@ def __create_ui(self): self.__window.vbox.pack_start(self.__special_notify_button, True, True, 10) self.__special_only_button = Gtk.CheckButton(label=_("Special Code Only Mode")) self.__window.vbox.pack_start(self.__special_only_button, True, True ,10) + self.__output_simplified_button = Gtk.CheckButton(label=_("Convert output to simplified Chinese")) + self.__window.vbox.pack_start(self.__output_simplified_button, True, True, 10) current_special_mode = self.__read("SpecialOnly", False) current_special_notify = self.__read("SpecialNotify", False) + current_output_simplified = self.__read("OutputSimplified", False) if current_special_notify: self.__special_notify_button.set_active(True) if current_special_mode: self.__special_only_button.set_active(True) + if current_output_simplified: + self.__output_simplified_button.set_active(True) self.__window.show_all() @@ -75,6 +80,7 @@ def run(self): def apply(self): select_special_notify = self.__special_notify_button.get_active() select_special_mode = self.__special_only_button.get_active() + select_output_simplified = self.__output_simplified_button.get_active() if select_special_notify: self.__write("SpecialNotify", GLib.Variant.new_boolean(True)) @@ -86,6 +92,11 @@ def apply(self): else: self.__write("SpecialOnly", GLib.Variant.new_boolean(False)) + if select_output_simplified: + self.__write("OutputSimplified", GLib.Variant.new_boolean(True)) + else: + self.__write("OutputSimplified", GLib.Variant.new_boolean(False)) + def on_value_changed(self, config, section, name, value, data): if section == 'engine/Array': if name == 'SpecialNotify': @@ -96,9 +107,15 @@ def on_value_changed(self, config, section, name, value, data): elif name == 'SpecialOnly': if value: - self.__special_notify_button.set_active(True) + self.__special_only_button.set_active(True) else: - self.__special_notify_button.set_active(False) + self.__special_only_button.set_active(False) + + elif name == 'OutputSimplified': + if value: + self.__output_simplified_button.set_active(True) + else: + self.__output_simplified_button.set_active(False) def __read(self, name, v): value = self.__config.get_value("engine/Array", name) diff --git a/src/engine.c b/src/engine.c index 8ea44c9..7aaf8de 100644 --- a/src/engine.c +++ b/src/engine.c @@ -25,6 +25,10 @@ #include "array.h" #include "config.h" +#ifdef HAVE_OPENCC +# include +#endif + #define _(String) gettext(String) #define ARRAY_SHORT_CODE_EMPTY_STRING "⎔" @@ -100,8 +104,12 @@ static IBusEngineClass *parent_class = NULL; static IBusConfig *config = NULL; static gboolean is_special_notify; static gboolean is_special_only; +static gboolean is_output_simplified; static gboolean is_aux_shown = FALSE; static ArrayContext *array_context = NULL; +#ifdef HAVE_OPENCC +static opencc_t cc_handle; +#endif GType ibus_array_engine_get_type (void) { static GType type = 0; @@ -135,6 +143,7 @@ void ibus_array_init (IBusBus *bus) { is_special_notify = FALSE; is_special_only = FALSE; + is_output_simplified = FALSE; /* load config */ GVariant* value; @@ -147,14 +156,26 @@ void ibus_array_init (IBusBus *bus) { if (value && g_variant_classify(value) == G_VARIANT_CLASS_BOOLEAN) is_special_only = g_variant_get_boolean(value); + value = ibus_config_get_value (config, "engine/Array", "OutputSimplified"); + if (value && g_variant_classify(value) == G_VARIANT_CLASS_BOOLEAN) + is_output_simplified = g_variant_get_boolean(value); + /* gettext preparation */ setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); + +#ifdef HAVE_OPENCC + cc_handle = opencc_open("tw2s.json"); +#endif } void ibus_array_exit (void) { +#ifdef HAVE_OPENCC + opencc_close(cc_handle); +#endif + array_release_context(array_context); if (g_object_is_floating (config)) @@ -419,7 +440,25 @@ static gboolean ibus_array_engine_commit_current_candidate (IBusArrayEngine *arr return FALSE; } } - ibus_engine_commit_text((IBusEngine*)arrayeng, text); + +#ifdef HAVE_OPENCC + if (is_output_simplified) { + char *converted = opencc_convert_utf8(cc_handle, + text->text, + strlen(text->text)); + if(converted) { + IBusText* newtext = ibus_text_new_from_string(converted); + ibus_engine_commit_text((IBusEngine*)arrayeng, newtext); + opencc_convert_utf8_free(converted); + } else { + ibus_engine_commit_text((IBusEngine*)arrayeng, text); + } + } else { +#endif + ibus_engine_commit_text((IBusEngine*)arrayeng, text); +#ifdef HAVE_OPENCC + } +#endif ibus_array_engine_reset((IBusEngine*)arrayeng); @@ -741,5 +780,7 @@ static void ibus_config_value_changed_cb (IBusConfig *config, const gchar *secti is_special_notify = g_variant_get_boolean (value); else if (g_strcmp0(name, "specialonly") == 0) is_special_only = g_variant_get_boolean (value); + else if (g_strcmp0(name, "outputsimplified") == 0) + is_output_simplified = g_variant_get_boolean (value); }