Created
August 22, 2012 09:21
-
-
Save jugyo/3423990 to your computer and use it in GitHub Desktop.
Using Google Translate API in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
import json | |
url = 'https://www.googleapis.com/language/translate/v2?key={0}&q={1}&source={2}&target={3}' | |
def translate(api_key, text, sourcelang, targetlang): | |
request = urllib2.Request(url.format(api_key.encode('utf-8'), text.encode('utf-8'), sourcelang.encode('utf-8'), targetlang.encode('utf-8'))) | |
response = urllib2.urlopen(request).read() | |
data = json.loads(response) | |
return data['data']['translations'][0]['translatedText'].encode('utf-8') | |
print translate('API-KEY', 'Hello World', 'en', 'ja') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice