Skip to content

Instantly share code, notes, and snippets.

@yurenju
Created January 29, 2011 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurenju/801339 to your computer and use it in GitHub Desktop.
Save yurenju/801339 to your computer and use it in GitHub Desktop.
gtr.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib2 import urlopen
from urllib import urlencode
import simplejson
import sys
def translate(text):
sl="zh-tw"
tl="en"
langpair='%s|%s'%(tl,sl)
base_url = 'http://ajax.googleapis.com/ajax/services/language/translate?'
data = urlencode({'v':1.0,'ie': 'UTF8', 'q': text,
'langpair':langpair})
url = base_url+data
urlres = urlopen(url)
json = simplejson.loads(urlres.read())
result = json['responseData']['translatedText']
return result
if __name__ == "__main__":
print translate (sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment