Using Google Translate From The Command Line

Want to support HowtoForge? Become a subscriber!
 
Submitted by mrashad (Contact Author) (Forums) on Fri, 2011-06-10 18:30. :: Linux

Using Google Translate From The Command Line

 1- Preliminary Note

- I'm using "Ojuba 4" Linux ("Fedora 13" based distribution).
- You don't need to be a root user to use this script.

 

2- What is the situation

I have found an article in one of the most famous Linux Arabic forums, it describes how to use Google Translate from the command line.

 

3- What is the problem

It is a good script but it is needed to write the language pairs every time and I used to make the computers work for me not to work for computers, so I didn't like to do that write every time I want to use the script, usually I translate from English to Arabic, sometimes from Arabic to English and fewer times form some other languages to Arabic or to English if I found the direct translation from that language to Arabic is not clear enough.

 

4- The solution

I have edited the script so now it checks the command arguments number, if it is 3 arguments it will use the first as the source language and the second as the destination language and the third is the words to translate.

But if it finds just one argument it will deal with is as the words to translate, and send to Google two times.

First to find out the source language and translate it into Arabic.

Then to find out the source language and translate it into English.

Then it ends.

 

5- The code itself

Here is the script after my modifications, create a new text file and call it "gtranslate" then copy and paste this code into it:

#!/bin/bash

if [ $# == 3 ]
then
echo "From: $1 To: $2"
lynx -dump "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$3&langpair=$1|$2"|awk -F'"' '{print $6}'

else
lynx -dump "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=|ar"|awk -F'"' '{print "From: "$10" To: ar \n"$6}';echo

lynx -dump "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=|en"|awk -F'"' '{print "From: "$10" To: en \n"$6}';echo

fi

Go to the terminal -command line window- and move to the same location that you saved the script in, then make it executable by doing this:

chmod a+x gtranslate

Then move it to the folder "/usr/bin/" if you have root access, or you can move it to your local bin folder "~/bin/" if your system supports it, otherwise you can move it to your home folder and call it from there directly like this - replace "rashad" with your Linux login name:

/home/rahsad/gtranslate

As you can see, it uses the "lynx" to dump the web page so you will need to install it if you don't have it installed but in that case you will need root user access, like this:

su -c "yum install lynx"

 

6- A test drive

And here are some examples:

The long/old/original way of use:

gtranslate ar fr "مجتمع لينوكس العربي"

From: ar To: fr
Société arabe pour Linux

The short/new/my way of use:

gtranslate "Société arabe pour Linux"

From: fr To: ar
الجمعية العربية لينكس

From: fr To: en
Arab Society for Linux

It works :), but if you can read Arabic you will find that the phrase that I start with "مجتمع لينوكس العربي" is not the same phrase that I got at the end: "الجمعية العربية لينكس" even that I just translate the same result he gave to me, that is one of the reasons that I don't trust the computer translation for all the time.

 

7- Thank you

Thank you for reading this and thanks for HowToForge to publish.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Sat, 2011-06-11 20:55.

Now that Google is shutting down its API, it'd be a good idea to try the Free and Open Source Apertium.

Find released language pairs:

Ubuntu/Debian:

apt-cache search apertium

 Arch Linux:

yaourt -Ss apertium 

Install English<->Spanish:

Ubuntu/Debian:

sudo apt-get install apertium-en-es

Arch Linux:

yaourt -S apertium-en-es

Translate:

echo "Use the Force, Luke" | apertium en-es

Or:

apertium es-en spanish_document.odt english_translation.odt

 

Submitted by Jogai (not registered) on Sat, 2011-06-11 10:18.
The Google Translate API has been officially deprecated as of May 26, 2011. the API will be shut off completely on December 1, 2011.
Submitted by Anonymous (not registered) on Fri, 2011-06-10 20:06.

I had some similar improvements in mind when I came across a one-liner over @ commandlinefu.com. I can't claim any credit for what's in google_translate function. 

Main improvement was the simple while loop. My primary use is when I'm supporting systems in a foreign language where getting the gist is more important than the nuance.

#!/bin/bash
SRC_LANG=$1
TGT_LANG=$2
divider="---"
function long_lang {
case $1 in
en) echo english;;
de) echo german;;
fr) echo french;;
es) echo spanish;;
esac
}
function google_translate { 
wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; 
}

echo Translating `long_lang $1` to `long_lang $2`
echo $divider
while read; do
google_translate "$REPLY" $SRC_LANG $TGT_LANG
echo $divider
done 
Submitted by Anonymous (not registered) on Fri, 2011-06-10 19:54.

no need to give such amount permission to the script; "chmod u+x gtranslate" is sufficient in order a personnal use of the script ;)

Submitted by Anonymous (not registered) on Sun, 2012-08-12 20:58.
  1. # access translate.google.com from terminal
  2. # mirror http://pastebin.com/kPTYjY6W
  3. # mirror https://github.com/Anoncheg1/Command-line-translator
  4. # requiments: curl, Mozilla js shell, forvo.com account
  5. # + output: translated text, fixed text with highlight, dictionary, transcription, audio pronunciation
  6. # + cache for all words
  7. # + saving words to file for learning
Submitted by Monsoft (not registered) on Wed, 2012-11-21 15:36.
Or you can use tiny script which I wrote in 10 minutes:
#!/bin/bash
# Command line translator based on google translate engine
# by Monsoft
# (c) 2012 Ascot

# Check if curl is installed
curl -V >/dev/null 2>&1|| { echo "I require curl but it's not installed. Aborting." >&2; exit 1;} 

# Check internet connectivity
curl -v www.google.co.uk 2>&1|grep -m1 "HTTP/1.1" >/dev/null 2>&1|| { echo "I require internet connection. Aborting." >&2; exit 1;}
# Main

if [ $# -ne 3 ]
	then
		echo -e "Command line language translator by Monsoft"
		echo -e "\nAvailable languages:"
		echo -e "---------------------\n"
		curl -s --user-agent "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.34 (KHTML, like Gecko) QupZilla/1.3.1 Safari/534.34" "http://translate.google.com/m?&mui=sl"|grep -Eo 'sl=[a-z]{2}">[^>]*<'|sed 's/sl=//g;s/">/ -> /g;s/<!--/g'|tr "\n" ","|awk -F, '{ for ( i=1; i<=NF;i=i+2) print $i "\t\t\t"$(i+1)}'
               	echo -e "\nUsage: `basename $0` From To Sentence"
               	echo -e "------------------------------------"
		echo -e "Example:\n`basename $0` en pl how"
		echo -e "`basename $0` en pl \"How they do it\""

		exit 
fi




FROM=$1
TO=$2
SENTENCE=`echo $3|tr " " "+"`

echo -e "Command line language translator by Monsoft"
echo -e "--------------------------------------------\n"
echo "${FROM} ---> ${TO}"
curl -s --user-agent "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.34 (KHTML, like Gecko) QupZilla/1.3.1 Safari/534.34" "http://translate.google.com/m?hl=en&sl=${FROM}&tl=${TO}&ie=UTF-8&prev=_m&q=${SENTENCE}"|sed -n 's/.*class="t0">//;s/<.*$//p'
echo -e "\n"
Submitted by Anonymous (not registered) on Wed, 2013-12-11 20:08.
Excellent script ! Please modify the script for sending a text file and writing the translated result back in a file....
Submitted by Monsoft (not registered) on Fri, 2014-10-31 12:22.
Added some time ago http://pastebin.com/SVs62qPH
Submitted by zenny (registered user) on Sat, 2014-11-01 08:46.
Very useful script. Thanks, Monsoft!