Created
February 28, 2013 06:35
-
-
Save kachok/5054715 to your computer and use it in GitHub Desktop.
Sample google translate code. Put your Google API key into settings.py. Sample.txt format is tab separated language 2 letter code and word to translate, one pair per line. Get your key at https://code.google.com/apis/console/b/0/ It is $20 per 1,000,000 characters. Our words average 8 char per word. There is 8M chars per day limit.
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
from apiclient.discovery import build | |
from settings import settings | |
f=open("sample.txt","r") | |
for line in f: | |
lang, word = line.strip().split(" ") | |
# Build a service object for interacting with the API. Visit | |
# the Google APIs Console <http://code.google.com/apis/console> | |
# to get an API key for your own application. | |
service = build('translate', 'v2', | |
developerKey=settings["google_translate_key"]) | |
word=unicode(word, 'utf-8') | |
translation= service.translations().list( | |
source=lang, | |
target='en', | |
q=[word] | |
).execute()['translations'][0]['translatedText'] | |
print word, translation | |
This file contains hidden or 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
ru перестройка | |
ru спутник | |
ru демократия | |
ru бабушка | |
ru гласность | |
es burrito |
This file contains hidden or 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
# This is an example settings file. Customize these values for your | |
# app and rename the file as settings.py. | |
settings = { | |
"google_translate_key":"YOUR_KEY_HERE", | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment