Hi,
I found the Google translate is pretty cool. I would like to use it to translate some text from my computer in batch(in script).
Do you know any way that I could use it easily from shell?
Hi,
I found the Google translate is pretty cool. I would like to use it to translate some text from my computer in batch(in script).
Do you know any way that I could use it easily from shell?
Hi mag_dex
Interesting idea. I never tried it myself but if I were to, my starting point might be using a commandline driven textbased web browser such as Lynx maybe (?)
http://lynx.isc.org/
http://en.wikipedia.org/wiki/Lynx_(web_browser)
Save this in ~/bin/translate
Make it executable:PHP Code:
#!/usr/bin/env python
from urllib2 import urlopen
from urllib import urlencode
import sys
# The google translate API can be found here:
# http://code.google.com/apis/ajaxlanguage/documentation/#Examples
lang1=sys.argv[1]
lang2=sys.argv[2]
langpair='%s|%s'%(lang1,lang2)
text=' '.join(sys.argv[3:])
base_url='http://ajax.googleapis.com/ajax/services/language/translate?'
params=urlencode( (('v',1.0),
('q',text),
('langpair',langpair),) )
url=base_url+params
content=urlopen(url).read()
start_idx=content.find('"translatedText":"')+18
translation=content[start_idx:]
end_idx=translation.find('"}, "')
translation=translation[:end_idx]
print translation
Use it like this:Code:chmod +x ~/bin/translate
en (English) is the source languageCode:translate en es where are you
es (Spanish) is the target language
"where are you" is the text to be translated
Output:Code:dónde estás
Last edited by unutbu; March 10th, 2009 at 11:50 AM.
Thanks! The code is great and works!
I'm glad you like it
excellent! works perfect
i'm not a python guru, but know php. in php, to parse that data, you generally use json_decode.
i googled a bit, and found this:
http://docs.python.org/library/json.html
but wouldn' that be better if this is used?
just my two cents though
Thanks again for the great script!
Thank you very much for the feedback and information, SoulSmasher.
I didn't know about JSON until now.
Happily, it appears to be quite easy to use json. Below is an updated version of the script.
To use the script with Ubuntu, you'll need to have the python-json package installed. (It requires about 160 KiB to install).
PHP Code:
#!/usr/bin/env python
from urllib2 import urlopen
from urllib import urlencode
import sys
try:
import json
except ImportError:
print('You need to install the python-json package')
sys.exit(1)
# The google translate API can be found here:
# http://code.google.com/apis/ajaxlanguage/documentation/#Examples
lang1=sys.argv[1]
lang2=sys.argv[2]
langpair='%s|%s'%(lang1,lang2)
text=' '.join(sys.argv[3:])
base_url='http://ajax.googleapis.com/ajax/services/language/translate?'
params=urlencode( (('v',1.0),
('q',text),
('langpair',langpair),) )
url=base_url+params
content=urlopen(url).read()
try:
trans_dict=json.loads(content)
except AttributeError:
trans_dict=json.read(content)
print(trans_dict['responseData']['translatedText'])
Last edited by unutbu; June 15th, 2009 at 05:36 AM.
Hi, thanks for the quick reply unutbu
I tried your new code, as far as i can say it works flawlessly
thank you again for the script, saves me from huge pain
works like a charm
thanks mate
i found a bug.
when there's ' character inside the text, it won't translate
i.e: try this
translate fr en n'est-ce pas
nothing shows and i've to close it with ctrl+c combination
Last edited by SoulSmasher; June 17th, 2009 at 07:09 AM. Reason: typo
Bookmarks