def translate text langpair import urllib import simplejson query urll

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def translate(text,langpair):
import urllib
import simplejson
query = urllib.urlencode({'q' : text.encode("utf-8"),'langpair':langpair})
url = u'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&%s'.encode("utf-8") \
% (query)
search_results = urllib.urlopen(url)
json = simplejson.loads(search_results.read())
mess = json['responseData']['translatedText']
return mess
translated = translate("I am feeling lucky","en|ru")
print "Translated:", translated