Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ryicoh committed Jan 9, 2022
0 parents commit 1ddb588
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# deepl.vim

## Demo

## Configuration
```vim
let g:deepl#endpoint = "https://api-free.deepl.com/v2/translate"
let g:deepl#auth_key = "00000000-0000-0000-0000-000000000000:fx"
vmap t<C-e> <Cmd>call deepl#v("EN")<CR>
vmap t<C-j> <Cmd>call deepl#v("JA")<CR>
```
32 changes: 32 additions & 0 deletions autoload/deepl.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
" Send a translation request to deepl using curl
function! deepl#translate(input, lang)
let cmd = "curl -sS ".g:deepl#endpoint
let cmd = cmd.' -d "auth_key='.g:deepl#auth_key.'"'
let cmd = cmd.' -d "text='.a:input.'"'
let cmd = cmd.' -d "target_lang='.a:lang.'"'

try
const res = json_decode(system(cmd))
return res["translations"][0]["text"]

catch /.*/
echoerr "error: " . v:exception
endtry
endfunction


" Replace visual selection
" ref: https://github.com/christianrondeau/vim-base64/blob/d15253105f6a329cd0632bf9dcbf2591fb5944b8/autoload/base64.vim#L29
function! deepl#v(lang)
" Preserve line breaks
let l:paste = &paste
set paste
" Reselect the visual mode text
normal! gv
" Apply transformation to the text
execute "normal! c\<c-r>=deepl#translate(@\", '".a:lang."')\<cr>\<esc>"
" Select the new text
normal! `[v`]h
" Revert to previous mode
let &paste = l:paste
endfunction

0 comments on commit 1ddb588

Please sign in to comment.