Skip to content

Instantly share code, notes, and snippets.

@AlexAltea
Created November 24, 2014 06:45
Show Gist options
  • Save AlexAltea/3f978d8c3f6938a60f3b to your computer and use it in GitHub Desktop.
Save AlexAltea/3f978d8c3f6938a60f3b to your computer and use it in GitHub Desktop.
Simple Google Translate access from Python
import httplib, urllib, string
# Just a quick, dirty function I made before a proper API appeared (e.g. goslate).
def translate(IN, OUT, text):
text = urllib.quote(text)
conn = httplib.HTTPConnection("translate.google.com")
conn.request("GET", "/translate_a/t?client=t&text="+text+"&hl="+IN+"&tl="+OUT)
res = conn.getresponse().read()
res = res[4:string.find(res,",\"\",\"\"]]")]
res = string.split(res, "],[")
for i in range(len(res)):
if i >= 1: res[i] = res[i][1:string.index(res[i], "\",\"")]
else: res[i] = res[i][:string.index(res[i], "\",\"")]
return "".join(res)
print translate("en", "es", "this really works!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment