Installing Google Translate to Vim

TL;DR

Things to do:
* Install Google Translate to Vim

My environment:
* macOS Catalina 10.15.7

Google Translate

soimort/translate-shell

Dependencies

Installation

$ which gawk
$ brew install gawk
  • translate-shell
$ cd /usr/local/share
$ git clone https://github.com/soimort/translate-shell
$ cd translate-shell/
$ make
$ sudo make install
$ which trans
/usr/local/bin/trans
Plug 'echuraev/translate-shell.vim'
  • Creating key mapping for these commands e.g
nnoremap <silent> <leader>t :Trans<CR>
vnoremap <silent> <leader>t :Trans<CR>
nnoremap <silent> <leader>tc :Trans :zh<CR>
vnoremap <silent> <leader>tc :Trans :zh<CR>

Translation

In Bash

action example explanation
basic usage $ trans -s=zh -t=ja "你好吗?" same with $ trans zh:ja "你好吗?""
auto identify $ trans :zh "How are you!" en to zh
brief mode $ trans -b :ja "How are you!" お元気ですか?
dictionary mode $ trans :en word used as a dictionary
language identification $ trans -id 世界你好 简体中文
Text-to-Speech $ trans -b -p :zh "Saluton, Mondo" -play (-p) option to listen to the translation
Text-to-Speech $ trans -sp "你好,世界" -speak (-sp) option to listen to the original text
Terminal Paging $ trans -d -v word -view (-v) option to view the translation in a terminal pager such as less or more
Pipeline, Input and Output echo "Hello world" | trans -b :zh or $ trans -b -i input.txt :ja
Translate a File $ trans :ja file://input.txt Brief mode is used when translating from file URI schemes
Translate a Web Page $ trans :zh https://www.yahoo.co.jp Default broswer is launched and contents are translated while browsing
Translate a Web Page $ trans -browser firefox :ja http://www.w3.org/ The specified broswer is launched and contents are translated while browsing
Interactive Translate Shell (REPL) $ trans -shell [en:zh] Start an interactive shell using the -shell (or -I) option

In Vim

  • Translate a word under cursor
    Using <Leader>t likes

f:id:lgx:20210122230643p:plain:w400

  • Translate some lines in visual mode
    The following example is using <Leader>tc to translate to Chinese.

f:id:lgx:20210122231058p:plain:w500

  • Translate something via shell mode in Vim Command :!trans -b "Peace begins with a smile." will show the following result
平和は笑顔から始まります

続けるにはENTERを押すかコマンドを入力してください

Summary

It is easy to install Google Translate to bash and Vim, and it's convenience to translate a word or some lines to any language you like, either in bash or Vim. It will make anyone more efficiently to to their work.

I like Vim and Google!