Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

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 Kreationnext to publish.

Comments

comments