Improved Google Translate interface with asynchronous request and better user experience.
I have used translation frequently in recent days.
I tried Google Translate, it is cool, but something troubled me:
- It's synchronous, every query will block anything, and the user experience is not good enough, especially when the network is poor
- The result buffer is not formatted enough, and the operation is less user-friendly
- The code is too long too old, make it hard to hack and extend new functions
So I wrote this plugin. It borrows some ideas and codes from Google Translate, and its main features are:
- Completely asynchronous, without any blocking problems, so very smoothly
- Optimize the display of translation results, the interface is more beautiful and natural
- It is convenient for switch between different languages, and support many interactive operations
- The code is clean, and easy to expand. New functions can be constructed very simply
With MELPA and package.el:
M-x package-install go-translate RET
(require 'go-translate)
Or manually download go-translate.el
then put into /some-path
, then add this to .emacs
:
(add-to-list 'load-path "/some-path")
(require 'go-translate)
First, config the native language and your default target language:
(setq go-translate-native-language "en")
(setq go-translate-target-language "fr")
If you have any other translation plans, then put them into go-translate-extra-directions
:
(add-to-list go-translate-extra-directions (cons "ja" "ru")) ; from japanese to russian
(add-to-list go-translate-extra-directions (cons "en" "fr")) ; from english to french
;; or
(setq go-translate-extra-directions '(("ja" . "ru") ("en" . "fr")))
Other customizations you can look into custom-group
- go-translate
, eg:
(setq go-translate-buffer-follow-p t) ; focus the result window
(setq go-translate-buffer-window-config ..) ; config the result window as your wish
The core command is go-translate
, and there is a extended command go-translate-popup
.
You can choose to bind them. such as:
(global-set-key "\C-ct" 'go-translate)
(global-set-key "\C-cT" 'go-translate-popup)
go-translate
will use a buffer to display translation results. In the result buffer, there are several shortcut keys:
g
refreshq
exitx
exchangessource language
andtarget language
and refresh the translationM-n
andM-p
, switch to the next/prev available translation direction, and refresh
The go-translate
will take the currently selected text or word-at-point
as the default input.
In the pop-up read-from-minibuffer
interface, you can modify it. With C-n
and C-p
you can switch translation direcitons.
These direcitons are those configured in go-translate-extra-directions
above.
Another command go-translate-popup
is to display a short translation by popping up a posframe
at the cursor.
The implementation is relatively simple, but also more practical.
If you want to expand your own command, you only need to overwrite or let-binding the variables below:
go-translate-input-function
is used to process user input and select translation languagesgo-translate-url-function
is used to generate the request urlgo-translate-prepare-function
some preparatory work done before the request is send. For example, create a buffer and render somethinggo-translate-request-function
asynchronously request to the server to get the translation contentgo-translate-render-function
render the returned result
You can take the source code of go-translate-popup
for example.
In addition, a number of go-translate-result-*
methods are also encapsulated to extract relevant content from the request result.
They are useful when customizing the render function.
Too many things to be added, but not today.