代码在这: https://gist.github.com/VitoVan/16e8c7ac31a01c02cebdb33cd896b874
拷贝粘贴到你的 .emacs 里就好了。
默认快捷键: C-c C-f
效果图:
给打不开 gist 的朋友:
(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 KEY: http://fanyi.youdao.com/openapi?path=data-mode
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.