- You can use any command you like for translation.
- Google translate API (default)
- translate-shell
- DeepL API Pro/Free
- The results of the translation can be output in a variety of ways.
- Floating window
- Split window
- Insert to the current buffer
- Replace the original text
- Set the register
- The translation command and output method can be specified as command arguments.
- In addition to the above presets, you can add your own functions.
- neovim 0.8+
The default Google Translate requires nothing but curl.
If you use translate-shell, you need to install trans
command.
If you use DeepL API Pro/Free, you need the authorization key for DeepL API Pro/Free. In addition, you need curl to send the request.
With any plugin manager you like (e.g. vim-plug, packer.nvim, dein.vim)
This plugin has default settings, so there is no need to call setup if you want to use it as is.
This is my setting.
let g:deepl_api_auth_key = 'MY_AUTH_KEY'
lua <<EOL
require("translate").setup({
default = {
command = "deepl_pro",
},
preset = {
output = {
split = {
append = true,
},
},
},
})
EOL
See help for available options.
This plugin provides :Translate
.
I put the quote from the help in the fold.
:Translate
:[range]Translate {target-lang} [{-options}...]
{target-lang}: Required. The language into which the text should be
translated. The format varies depending on the external command used.
|:Translate| can take |:range|. |v|, |V| and |CTRL-V| are supported. If it was
not given, |:Translate| treats current cursor line.
available options:
- '-source='
The language of the text to be translated.
- '-parse_before='
The functions to format texts of selection. You can
use a comma-separated string. If omitted,
|translate-nvim-option-default-parse-before|.
- '-command='
The extermal command to use translation. If omitted,
|translate-nvim-option-default-command| is used.
- '-parse_after='
The functions to format the result of extermal
command. You can use a comma-separated string.
If omitted, |translate-nvim-option-default-parse-after|.
- '-output='
The function to pass the translation result. If
omitted, |translate-nvim-option-default-output|.
- '-comment'
Special option, used as a flag. If this flag is set
and the cursor is over a comment, whole comment is
treated as a selection.
Use <Cmd> for mapping.
If you cannot use it, you must change the format with nmap and xmap.
nnoremap me <Cmd>Translate EN<CR>
xnoremap me <Cmd>Translate EN<CR>
Another way.
nnoremap me :<C-u>Translate EN<CR>
xnoremap me :Translate EN<CR>
You can use this mapping.
nnoremap <space>tw viw:Translate ZH<CR>