Save this in ~/bin/translate

<code><span style="color:#000000;"><span style="color:#ff8000;">#!/usr/bin/env python<br /> </span><span style="color:#0000bb;">from urllib2 import urlopen<br /> from urllib import urlencode<br /> import sys</span></span></code>

<code><span style="color:#ff8000;"># The google translate API can be found here:<br />

http://code.google.com/apis/ajaxlanguage/documentation/#Examples</span></p>

<p></code></code>

<code><span style="color:#0000bb;">lang1</span><span style="color:#007700;">=</span><span style="color:#0000bb;">sys</span><span style="color:#007700;">.</span><span style="color:#0000bb;">argv</span><span style="color:#007700;">[</span><span style="color:#0000bb;">1</span><span style="color:#007700;">] </span><span style="color:#0000bb;">lang2</span><span style="color:#007700;">=</span><span style="color:#0000bb;">sys</span><span style="color:#007700;">.</span><span style="color:#0000bb;">argv</span><span style="color:#007700;">[</span><span style="color:#0000bb;">2</span><span style="color:#007700;">] </span><span style="color:#0000bb;">langpair</span><span style="color:#007700;">=</span><span style="color:#dd0000;">'%s|%s'</span><span style="color:#007700;">%(</span><span style="color:#0000bb;">lang1</span><span style="color:#007700;">,</span><span style="color:#0000bb;">lang2</span><span style="color:#007700;">)<br /> </span><span style="color:#0000bb;">text</span><span style="color:#007700;">=</span><span style="color:#dd0000;">' '</span><span style="color:#007700;">.</span><span style="color:#0000bb;">join</span><span style="color:#007700;">(</span><span style="color:#0000bb;">sys</span><span style="color:#007700;">.</span><span style="color:#0000bb;">argv</span><span style="color:#007700;">[</span><span style="color:#0000bb;">3</span><span style="color:#007700;">:])<br /> </span><span style="color:#0000bb;">base_url</span><span style="color:#007700;">=</span><span style="color:#dd0000;">'http://ajax.googleapis.com/ajax/services/language/translate?'<br /> </span><span style="color:#0000bb;">params</span><span style="color:#007700;">=</span><span style="color:#0000bb;">urlencode</span><span style="color:#007700;">( ((</span><span style="color:#dd0000;">'v'</span><span style="color:#007700;">,</span><span style="color:#0000bb;">1.0</span><span style="color:#007700;">),<br /> (</span><span style="color:#dd0000;">'q'</span><span style="color:#007700;">,</span><span style="color:#0000bb;">text</span><span style="color:#007700;">),<br /> (</span><span style="color:#dd0000;">'langpair'</span><span style="color:#007700;">,</span><span style="color:#0000bb;">langpair</span><span style="color:#007700;">),) )<br /> </span><span style="color:#0000bb;">url</span><span style="color:#007700;">=</span><span style="color:#0000bb;">base_url</span><span style="color:#007700;">+</span><span style="color:#0000bb;">params<br /> content</span><span style="color:#007700;">=</span><span style="color:#0000bb;">urlopen</span><span style="color:#007700;">(</span><span style="color:#0000bb;">url</span><span style="color:#007700;">).</span><span style="color:#0000bb;">read</span><span style="color:#007700;">()<br /> </span><span style="color:#0000bb;">start_idx</span><span style="color:#007700;">=</span><span style="color:#0000bb;">content</span><span style="color:#007700;">.</span><span style="color:#0000bb;">find</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'"translatedText":"'</span><span style="color:#007700;">)+</span><span style="color:#0000bb;">18<br /> translation</span><span style="color:#007700;">=</span><span style="color:#0000bb;">content</span><span style="color:#007700;">[</span><span style="color:#0000bb;">start_idx</span><span style="color:#007700;">:] </span><span style="color:#0000bb;">end_idx</span><span style="color:#007700;">=</span><span style="color:#0000bb;">translation</span><span style="color:#007700;">.</span><span style="color:#0000bb;">find</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'"}, "'</span><span style="color:#007700;">)<br /> </span><span style="color:#0000bb;">translation</span><span style="color:#007700;">=</span><span style="color:#0000bb;">translation</span><span style="color:#007700;">[:</span><span style="color:#0000bb;">end_idx</span><span style="color:#007700;">] print </span><span style="color:#0000bb;">translation </span></code>