Translate Shell: Google Translate from terminal

hebrewCame across a word or sentence in a language you do not understand? For most of us, the one-stop webservice is Google Translate. Thanks to Translate Shell, a new utility to access Google Translate, you don’t need to fire a browser to translate text any more. The tool, essentially a shell script, provides various options to translate text or webpages, even interactively.

Translate Shell detects your locale automatically and uses it as the target language to translate text to. However, there are options to specify the target language. As for the input text language, it depends on Google Translate to detect it automatically (if input language is not mentioned specifically in the cmdline options).

Installation

Translate Shell needs gawk. To install gawk on Ubuntu:

$ sudo apt-get install gawk

The easiest option is to download the latest version directly and use it:

$ wget git.io/trans
$ chmod +x ./trans
$ sudo mv ./trans /usr/bin/

Usage

Using UTF-8 codeset as default locale is advisable, as it potentially supports all languages. You can check whether your codeset is UTF-8 using:

$ locale
OR
$ echo $LC_CTYPE

In my case the locale is en_US.UTF-8.

Some sample usage examples:

  • Translate some text
    $ trans 'Saluton, Mondo'
  • Show a brief output (just the translation)
    $ trans -b 'Saluton, Mondo'
  • Translate briefly and interactively (one line per input)
    $ trans -b -I
  • Show list of language codes
    $ trans -R
  • Translate something into French
    $ trans :fr 'Hello World!'
  • Translate something into multiple languages (use + for multiple)
    $ trans :zh+ja 'Hello World!'
    OR use -t option
    $ trans -t zh_ja 'Hello World!'
  • Specify input language (if Google Translate auto-detection fails) in in:out format
    $ trans ja:en 手紙
    OR use the -s option
    $ trans -s ja -t en 手紙
  • To translate multiple words individually do not use quotes
    $ trans en:zh freedom of speech
  • Single quotes are preferable if the text has symbols like <!> so that it’s not interpreted by the shell. However, in cases like the input text contains a single quote, use double quotes.
    $ trans :zh "I'm lovin' it! McDonald's"
  • Use the play (-p) option to listen to a translation (needs MPlayer, mplayer2, mpg123, or eSpeak)
    $ trans :ja -b -p "Saluton, Mondo"
  • Translate a text file
    $ trans :fr file://input.txt
  • Translate a webpage
    $ trans :fr http://www.w3.org/
  • Translate a webpage and open in browser
    $ trans :fr -browser firefox http://www.w3.org/
  • Show detailed help
    $ trans -H

Environment Variables

The following environment variables are supported:

BROWSER        : for option -browser
PLAYER         : for option -player
HTTP_PROXY     : for option -proxy
TRANS_PS       : for option -prompt
TRANS_PS_COLOR : for option -prompt-color
HOME_LANG      : for option -l
SOURCE_LANG    : for option -s
TARGET_LANG    : for option -t

vim integration

Add the following line to your ~/.vimrc:

set keywordprg=trans\ :en

Use <Shift-k> to view the translation of the word under the cursor.

GoogleCL is another utility that let you access many of the Google services.

Webpage: Translate Shell

One thought on “Translate Shell: Google Translate from terminal”

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s