Skip to content

Commit 7b720ac

Browse files
authored
Merge pull request #36 from ray2501/master
Add output to simplified Chinese support (using OpenCC)
2 parents d8c70c8 + c8ae29f commit 7b720ac

File tree

5 files changed

+86
-9
lines changed

5 files changed

+86
-9
lines changed

configure.ac

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Define to the read-only
7676
AM_GNU_GETTEXT([external])
7777
AM_GNU_GETTEXT_VERSION(0.16.1)
7878

79+
AC_ARG_ENABLE(opencc,
80+
AC_HELP_STRING([--enable-opencc],
81+
[Use opencc for simplified Chinese conversion]),
82+
[enable_opencc=$enableval],
83+
[enable_opencc=yes]
84+
)
85+
if test x"$enable_opencc" = x"yes"; then
86+
# check opencc
87+
PKG_CHECK_MODULES(OPENCC, [opencc >= 1.0.0], [
88+
AC_DEFINE(HAVE_OPENCC, 1, [Define if found opencc])
89+
CFLAGS="$CFLAGS $OPENCC_CFLAGS"
90+
LDFLAGS="$LDFLAGS $OPENCC_LIBS"
91+
])
92+
fi
7993

8094
# OUTPUT files
8195
AC_CONFIG_FILES(po/Makefile.in

ibus-array.spec.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Source0: https://github.com/lexical/ibus-array/archive/release-%{version}.tar
1212

1313
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
1414
BuildRequires: gettext-devel
15+
BuildRequires: opencc-devel
1516

1617
Requires: ibus
1718

po/zh_TW.po

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ msgid ""
66
msgstr ""
77
"Project-Id-Version: ibus-array 0.2.2\n"
88
"Report-Msgid-Bugs-To: https://github.com/lexical/ibus-array/issues\n"
9-
"POT-Creation-Date: 2023-03-18 17:29+0800\n"
9+
"POT-Creation-Date: 2023-03-28 19:12+0800\n"
1010
"PO-Revision-Date: 2019-12-10 22:12+0800\n"
1111
"Last-Translator: Anthony Fok <foka@debian.org>\n"
1212
"Language-Team: Chinese (traditional)\n"
@@ -27,15 +27,15 @@ msgstr "行列"
2727
msgid "Array 30 Input Method 行列30輸入法"
2828
msgstr "行列30輸入法 Array 30 Input Method"
2929

30-
#: src/engine.c:198
30+
#: src/engine.c:219
3131
msgid "Setup"
3232
msgstr "設定"
3333

34-
#: src/engine.c:199
34+
#: src/engine.c:220
3535
msgid "Configure Array 30 engine"
3636
msgstr "設定行列輸入法"
3737

38-
#: src/engine.c:464
38+
#: src/engine.c:504
3939
msgid ""
4040
"1.comma 2.bracket 3.symbol 4.math 5.arrow 6.unit 7.table 8.roman 9.greek 0."
4141
"bopomo"
@@ -52,3 +52,7 @@ msgstr "當有特別碼時,顯示提示"
5252
#: setup/main.py:56
5353
msgid "Special Code Only Mode"
5454
msgstr "當有特別碼時,只能用特別碼輸入 (特別碼練習模式)"
55+
56+
#: setup/main.py:58
57+
msgid "Convert output to simplified Chinese"
58+
msgstr "出字時轉為簡體中文"

setup/main.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# You should have received a copy of the GNU General Public License along
2020
# with this program; if not, write to the Free Software Foundation, Inc.,
2121
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22-
22+
2323
import sys
2424
import os
2525
import gi
@@ -34,7 +34,7 @@
3434

3535
class Setup:
3636
def __init__(self, bus):
37-
self.__bus = bus
37+
self.__bus = bus
3838
self.__config = self.__bus.get_config()
3939
self.__config.connect("value-changed", self.on_value_changed, None)
4040
self.__create_ui()
@@ -55,14 +55,19 @@ def __create_ui(self):
5555
self.__window.vbox.pack_start(self.__special_notify_button, True, True, 10)
5656
self.__special_only_button = Gtk.CheckButton(label=_("Special Code Only Mode"))
5757
self.__window.vbox.pack_start(self.__special_only_button, True, True ,10)
58+
self.__output_simplified_button = Gtk.CheckButton(label=_("Convert output to simplified Chinese"))
59+
self.__window.vbox.pack_start(self.__output_simplified_button, True, True, 10)
5860

5961
current_special_mode = self.__read("SpecialOnly", False)
6062
current_special_notify = self.__read("SpecialNotify", False)
63+
current_output_simplified = self.__read("OutputSimplified", False)
6164

6265
if current_special_notify:
6366
self.__special_notify_button.set_active(True)
6467
if current_special_mode:
6568
self.__special_only_button.set_active(True)
69+
if current_output_simplified:
70+
self.__output_simplified_button.set_active(True)
6671

6772
self.__window.show_all()
6873

@@ -75,6 +80,7 @@ def run(self):
7580
def apply(self):
7681
select_special_notify = self.__special_notify_button.get_active()
7782
select_special_mode = self.__special_only_button.get_active()
83+
select_output_simplified = self.__output_simplified_button.get_active()
7884

7985
if select_special_notify:
8086
self.__write("SpecialNotify", GLib.Variant.new_boolean(True))
@@ -86,6 +92,11 @@ def apply(self):
8692
else:
8793
self.__write("SpecialOnly", GLib.Variant.new_boolean(False))
8894

95+
if select_output_simplified:
96+
self.__write("OutputSimplified", GLib.Variant.new_boolean(True))
97+
else:
98+
self.__write("OutputSimplified", GLib.Variant.new_boolean(False))
99+
89100
def on_value_changed(self, config, section, name, value, data):
90101
if section == 'engine/Array':
91102
if name == 'SpecialNotify':
@@ -96,9 +107,15 @@ def on_value_changed(self, config, section, name, value, data):
96107

97108
elif name == 'SpecialOnly':
98109
if value:
99-
self.__special_notify_button.set_active(True)
110+
self.__special_only_button.set_active(True)
100111
else:
101-
self.__special_notify_button.set_active(False)
112+
self.__special_only_button.set_active(False)
113+
114+
elif name == 'OutputSimplified':
115+
if value:
116+
self.__output_simplified_button.set_active(True)
117+
else:
118+
self.__output_simplified_button.set_active(False)
102119

103120
def __read(self, name, v):
104121
value = self.__config.get_value("engine/Array", name)

src/engine.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include "array.h"
2626
#include "config.h"
2727

28+
#ifdef HAVE_OPENCC
29+
# include <opencc.h>
30+
#endif
31+
2832
#define _(String) gettext(String)
2933
#define ARRAY_SHORT_CODE_EMPTY_STRING "⎔"
3034

@@ -100,8 +104,12 @@ static IBusEngineClass *parent_class = NULL;
100104
static IBusConfig *config = NULL;
101105
static gboolean is_special_notify;
102106
static gboolean is_special_only;
107+
static gboolean is_output_simplified;
103108
static gboolean is_aux_shown = FALSE;
104109
static ArrayContext *array_context = NULL;
110+
#ifdef HAVE_OPENCC
111+
static opencc_t cc_handle;
112+
#endif
105113

106114
GType ibus_array_engine_get_type (void) {
107115
static GType type = 0;
@@ -135,6 +143,7 @@ void ibus_array_init (IBusBus *bus) {
135143

136144
is_special_notify = FALSE;
137145
is_special_only = FALSE;
146+
is_output_simplified = FALSE;
138147

139148
/* load config */
140149
GVariant* value;
@@ -147,14 +156,26 @@ void ibus_array_init (IBusBus *bus) {
147156
if (value && g_variant_classify(value) == G_VARIANT_CLASS_BOOLEAN)
148157
is_special_only = g_variant_get_boolean(value);
149158

159+
value = ibus_config_get_value (config, "engine/Array", "OutputSimplified");
160+
if (value && g_variant_classify(value) == G_VARIANT_CLASS_BOOLEAN)
161+
is_output_simplified = g_variant_get_boolean(value);
162+
150163
/* gettext preparation */
151164
setlocale (LC_ALL, "");
152165
bindtextdomain (PACKAGE, LOCALEDIR);
153166
textdomain (PACKAGE);
167+
168+
#ifdef HAVE_OPENCC
169+
cc_handle = opencc_open("tw2s.json");
170+
#endif
154171
}
155172

156173
void ibus_array_exit (void)
157174
{
175+
#ifdef HAVE_OPENCC
176+
opencc_close(cc_handle);
177+
#endif
178+
158179
array_release_context(array_context);
159180

160181
if (g_object_is_floating (config))
@@ -419,7 +440,25 @@ static gboolean ibus_array_engine_commit_current_candidate (IBusArrayEngine *arr
419440
return FALSE;
420441
}
421442
}
422-
ibus_engine_commit_text((IBusEngine*)arrayeng, text);
443+
444+
#ifdef HAVE_OPENCC
445+
if (is_output_simplified) {
446+
char *converted = opencc_convert_utf8(cc_handle,
447+
text->text,
448+
strlen(text->text));
449+
if(converted) {
450+
IBusText* newtext = ibus_text_new_from_string(converted);
451+
ibus_engine_commit_text((IBusEngine*)arrayeng, newtext);
452+
opencc_convert_utf8_free(converted);
453+
} else {
454+
ibus_engine_commit_text((IBusEngine*)arrayeng, text);
455+
}
456+
} else {
457+
#endif
458+
ibus_engine_commit_text((IBusEngine*)arrayeng, text);
459+
#ifdef HAVE_OPENCC
460+
}
461+
#endif
423462

424463
ibus_array_engine_reset((IBusEngine*)arrayeng);
425464

@@ -741,5 +780,7 @@ static void ibus_config_value_changed_cb (IBusConfig *config, const gchar *secti
741780
is_special_notify = g_variant_get_boolean (value);
742781
else if (g_strcmp0(name, "specialonly") == 0)
743782
is_special_only = g_variant_get_boolean (value);
783+
else if (g_strcmp0(name, "outputsimplified") == 0)
784+
is_output_simplified = g_variant_get_boolean (value);
744785
}
745786

0 commit comments

Comments
 (0)