forked from lorniu/go-translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgts-engine-stardict.el
64 lines (53 loc) · 2 KB
/
gts-engine-stardict.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
;;; gts-engine-stardict.el --- StarDict -*- lexical-binding: t -*-
;; Copyright (C) 2021 lorniu <[email protected]>
;; SPDX-License-Identifier: MIT
;;; Commentary:
;; Engine for sdcv (StarDict Console Version).
;;
;; sdcv is an offline translate software. It can be used when no network.
;;
;; Make sure sdcv is installed on your system:
;;
;; sudo pacman -S sdcv
;;
;; Then download dictionary data to ~/.stardict/dic or /usr/share/stardict/dic:
;;
;; # For example from http://download.huzheng.org:
;;
;; mkdir -p ~/.stardict/dic
;; cd ~/.stardict/dic
;; wget http://download.huzheng.org/zh_CN/stardict-langdao-ce-gb-2.4.2.tar.bz2
;; wget http://download.huzheng.org/zh_CN/stardict-langdao-ce-gb-2.4.2.tar.bz2
;; tar xvf stardict-langdao-ec-gb-2.4.2.tar.bz2
;; tar xvf stardict-langdao-ce-gb-2.4.2.tar.bz2
;; sdcv -l
;;
;; TODO: Provide more options and a better parser.
;;; Code:
(require 'gts-implements)
(defclass gts-stardict-engine (gts-engine)
((tag :initform "StarDict")
(parser :initform (gts-parser))))
(defcustom gts-stardict-program "sdcv"
"Executable command of sdcv (StarDict Console Version)."
:type 'string
:group 'go-translate)
(defcustom gts-stardict-args '("-n" "-c" "-0" "-1")
"Arguments passed to command `sdcv'."
:type 'list
:group 'go-translate)
(cl-defmethod gts-translate ((engine gts-stardict-engine) task rendercb)
(let* ((text (oref task text))
(parser (oref engine parser))
(result (with-temp-buffer
(require 'ansi-color)
(apply #'call-process gts-stardict-program nil t nil
(append gts-stardict-args (list text))) ;--json-output
(if (string-equal (buffer-substring 1 16) "Nothing similar")
"No translation result found, sorry :("
(ansi-color-apply (buffer-string))))))
(gts-update-raw task result)
(gts-parse parser task)
(funcall rendercb)))
(provide 'gts-engine-stardict)
;;; gts-engine-stardict.el ends here