Skip to content

Instantly share code, notes, and snippets.

@VitoVan
Last active September 21, 2017 06:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VitoVan/16e8c7ac31a01c02cebdb33cd896b874 to your computer and use it in GitHub Desktop.
Save VitoVan/16e8c7ac31a01c02cebdb33cd896b874 to your computer and use it in GitHub Desktop.
有道翻译 Emacs 快捷方式 C-c C-f
(defvar base-youdao-url "http://fanyi.youdao.com/openapi.do?keyfrom=emacs-yd-pub&key=527593631&type=data&doctype=json&version=1.1&q=")
;; Get yourself an API KEY here: http://fanyi.youdao.com/openapi?path=data-mode
(defun youdao-fanyi ()
"Translate current word (en->cn, cn->en), prompt to input if no word here"
(interactive)
(let* ((word (or (thing-at-point 'word) (read-string "Translate> ")))
(full-url (concat base-youdao-url word)))
(with-current-buffer (url-retrieve-synchronously full-url)
(unwind-protect
(progn
(goto-char (point-min))
(re-search-forward "^$")
(delete-region (point) (point-min)) ;strip headers
(message
(elt (cdar ;we just want the straight one
(json-read-from-string
(decode-coding-string
(buffer-string) 'utf-8)))
0)))
(kill-buffer)))))
(global-set-key "\C-c\ \C-f" 'youdao-fanyi)
;有道翻译API申请成功
;API key:527593631
;keyfrom:emacs-yd-pub
;创建时间:2016-08-16
;网站名称:emacs-yd-pub
;网站地址:https://gist.github.com/VitoVan/16e8c7ac31a01c02cebdb33cd896b874
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment