Last active
February 8, 2017 13:40
-
-
Save liiker/3ee7fd02c9694def8124579b92b6225f to your computer and use it in GitHub Desktop.
use youdao api tranlate English to chinese
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; send http get request | |
| (defun get-url (url) | |
| (with-current-buffer (url-retrieve-synchronously url) (buffer-string))) | |
| ;; get word translate origin data | |
| (defun get-translate-info (word) | |
| (decode-coding-string (get-url (format "http://fanyi.youdao.com/openapi.do?keyfrom=emacs-fanyi&key=830729599&type=data&doctype=json&version=1.1&q=%s" word)) 'utf-8)) | |
| ;; get chinese explain and send to the minibuffer | |
| (defun translate (word) | |
| (let (me-data pos-json json-str info) | |
| (setq me-data (get-translate-info word)) | |
| (setq pos-json (string-match-p "{.*}" me-data)) | |
| (setq json-str (substring me-data pos-json)) | |
| (setq info (json-read-from-string json-str)) | |
| (print (assoc 'explains (assoc 'basic info))))) | |
| ;; 绑定翻译快捷键 | |
| (global-set-key | |
| (kbd "C-c t n") | |
| (lambda () | |
| (interactive) | |
| (let ((word (read-string "要查询的单词: "))) | |
| (translate word)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment