text-translator.elで英語・日本語を自動判別しつつすべての翻訳エンジンで検索するパッチ
久々にtext-translator.elをアップデートしたら、いろいろ便利機能が追加されていた。とりわけこの2つは強力だ。
- text-trantext-translator-allで登録されている全部の翻訳エンジンで翻訳する
- text-translator-translate-by-auto-selectionで英語・日本語を自動判別して適切な翻訳エンジンを使う
しかし、この2つが両立していないところが不便だ。text-trantext-translator-allを使おうにも、毎回「enja」だの「jaen」だの打たないといけないのは至極面倒だ。せっかく英語・日本語自動判別機能があるなら、使わない手はない。というわけでパッチ。
あ、インストールはauto-install.elからM-x auto-install-batch text translatorでできるよ!auto-install.elはauto-install.elでEmacsLispパッケージを自動インストール→anything.el関連を一括インストール - http://rubikitch.com/に移転しましたからどうぞ。
ついでにちょこっとリファクタリング。もっと言えば、インターフェース部分と処理部分は分けるべきだと思うが、まだ手を出していない。
diff --git a/lisp/text-translator.el b/lisp/text-translator.el index 71fccaf..389ad09 100755 --- a/lisp/text-translator.el +++ b/lisp/text-translator.el @@ -95,10 +97,8 @@ Use Excite, Google and so translation site. (cond (last text-translator-last-string) - (mark-active - (buffer-substring-no-properties (region-beginning) (region-end))) (t - (read-string + (text-translator-region-or-read-string (format "Enter string translated by %s: " engine))))) (text-translator-client (text-translator-check-valid-translation-engine @@ -132,17 +132,22 @@ I choose with the character string that I translated in the last time." (interactive) (text-translator nil t)) -(defun text-translator-all (arg) + +(defun text-translator-region-or-read-string (&optional prompt) + "If mark is active, return the region, otherwise, read string with PROMPT." + (cond + (mark-active + (buffer-substring-no-properties (region-beginning) (region-end))) + (t + (read-string (or prompt "Enter string: "))))) + +(defun text-translator-all (arg &optional key str) "The function to translate in all of translate sites that matches the selected type." (interactive "P") (let ((hash text-translator-sitedata-hash) - (str (cond - (mark-active - (buffer-substring-no-properties (region-beginning) (region-end))) - (t - (read-string "Enter string: ")))) - keys key) + keys) + (setq str (or str (text-translator-region-or-read-string))) (when (or (null hash) arg) (setq text-translator-sitedata-hash @@ -151,7 +156,7 @@ the selected type." (maphash '(lambda (x y) (setq keys (cons x keys))) hash) - (setq key (completing-read "Select type: " keys nil t)) + (setq key (or key (completing-read "Select type: " keys nil t))) (when key (save-selected-window (pop-to-buffer text-translator-buffer) @@ -165,6 +170,16 @@ the selected type." (dolist (i sites) (text-translator-client i str t)))))) +(defun text-translator-all-by-auto-selection (arg) + "The function to translate in all of translate sites, whose translation engine is selected automatically. +The selection function is the value of `text-translator-auto-selection-func'." + (interactive "P") + (let ((str (text-translator-region-or-read-string))) + (text-translator-all + arg + (substring (funcall text-translator-auto-selection-func "" str) 1) + str))) + (defun text-translator-client (engine str &optional all) "Function that throws out words and phrases that want to translate into specified site, and receives translation result."
これさえ当てれば、以下の設定だけで日本語←→英語の翻訳は万全だ。
(require 'text-translator) (setq text-translator-auto-selection-func 'text-translator-translate-by-auto-selection-enja)
で、いつも M-x text-translator-all-by-auto-selection で翻訳できる!