Skip to content

Instantly share code, notes, and snippets.

@kachok
Created February 28, 2013 06:35
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 kachok/5054715 to your computer and use it in GitHub Desktop.
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.
# -*- 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
ru перестройка
ru спутник
ru демократия
ru бабушка
ru гласность
es burrito
# 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