I think you are talking about the ajax api http://code.google.com/apis/ajaxlanguage/ that should be used from javascript, so I don't understand what you mean by "google translate from python"
Alternatively, if you need to use the translation functionality from python, you can directly request the translation page and parse it using xml / html libs, for example. beautiful soup, html5lib
Actually I did it once and the beautiful soup didn’t work on google translate, but html5lib ( http://code.google.com/p/html5lib/ ) did
you will need to do something like this (copied from my larger code base)
def translate(text, tlan, slan="en"): opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'translate.py/0.1')] htmlPage = opener.open( "http://translate.google.com/translate_t?" + urllib.urlencode({'sl': slan, 'tl':tlan}), data=urllib.urlencode({'hl': 'en', 'ie': 'UTF8', 'text': text.encode('utf-8'), 'sl': slan, 'tl': tlan}) ) parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("etree", cElementTree)) etree_document = parser.parse(htmlPage) return _getResult(etree_document)
Anurag Uniyal Jul 16 '09 at 11:16 2009-07-16 11:16
source share