Inspired by immersive-translate/immersive-translate, I have implemented similar functionality in Emacs.
- You need to install curl on your device first. If you want to use the translate-shell backend, you also need to install translate-shell.
M-x package-install RET gptel RET
(require 'immersive-translate)
(add-hook 'elfeed-show-mode-hook #'immersive-translate-setup)
(add-hook 'nov-pre-html-render-hook #'immersive-translate-setup)
;; use Baidu Translation
(setq immersive-translate-backend 'baidu
immersive-translate-baidu-appid "your-appid")
;; use ChatGPT
;; (setq immersive-translate-backend 'chatgpt
;; immersive-translate-chatgpt-host "api.openai.com")
;; use translate-shell
;; (setq immersive-translate-backend 'trans)
immersive-translate-buffer
Translate the current buffer. Note: This may consume a large number of tokens.
immersive-translate-paragraph
Translate the current paragraph.
immersive-translate-clear
Clear all translated text.
immersive-translate-auto-mode
Translate, when enabled, automatically translate the visible portion of the current window.
Note: Some translation services (e.g. baidu translation) has QPS(Queries Per Second) limits, so it is recommended to use
immersive-translate-auto-mode
instead ofimmersive-translate-buffer
while using these services.immersive-translate-abort
Stop all ongoing translation processes in the current buffer.
immersive-translate-backend
The translation backend used. See API setting for details.
Currently available translation services:
- chatgpt (https://openai.com/api/)
- baidu (https://fanyi-api.baidu.com/)
- translate-shell (https://github.com/soimort/translate-shell)
- DeepL (https://www.deepl.com/docs-api/translate-text/translate-text)
immersive-translate-auto-idle
After enabling
immersive-translate-auto-mode
, the Emacs idle time required for the next translation to be executed.immersive-translate-exclude-shr-tag
The text contained in the tags of this variable will not be translated. This variable applies to all buffers generated by
shr.el
, such as buffers inelfeed-show-mode
,nov-mode
, andmu4e-view-mode
.immersive-translate-chatgpt-system-prompt
System prompt used by ChatGPT.
immersive-translate-chatgpt-user-prompt
User prompt used by ChatGPT.
immersive-translate-disable-predicates
When the current paragraph does not need to be translated, return
t
. These functions in this list do not accept any parameters.immersive-translate-pending-message
Text displayed before the translation results are returned.
immersive-translate-failed-message
Text displayed when translation fails.
See M-x customize-group RET immersive-translate RET
for details.
This plugin retrieves the user’s key through the built-in auth-source.el
.
- ChatGPT
Assuming your
immersive-translate-chatgpt-host
is “api.openai.com” and the API key is 123456, add the following text to~/.authinfo
:machine api.openai.com login apikey password 123456
- Baidu Translate
Assuming your
immersive-translate-baidu-appid
is “foobar” and the API key is 123456, add the following text to~/.authinfo
:machine fanyi-api.baidu.com login foobar password 123456
- DeepL
Assuming your API key is 123456, add the following text to
~/.authinfo
:machine deepl.com login apikey password 123456
If you want to add new translation services, you can check the variables immersive-translate-curl-get-translation-alist
and immersive-translate-curl-get-args-alist
.
The function in the variable immersive-translate-curl-get-args-alist
is used to construct the input parameters required when making a request.
The function in the variable immersive-translate-curl-get-translation-alist
is used to parse the translation results returned by curl
.