Created
August 2, 2011 08:07
-
-
Save avishai/1119787 to your computer and use it in GitHub Desktop.
Milamix Application
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
# Milamix app - Translate phrases into multiple languages at once | |
# http://milamix.heroku.com | |
# Copyright (c) 2011 Avishai Weiss (avishai dot weiss at gmail dot com) | |
require "rubygems" | |
require "sinatra" | |
require "open-uri" | |
require "json" | |
API_KEY = "GOOGLE API KEY GOES HERE" | |
BASE_URL = "https://www.googleapis.com/language/translate/v2?key=#{API_KEY}" | |
def translate(text, source = 'en', target = 'es') | |
url = BASE_URL + "&q=#{text}&source=#{source}&target=#{target}" | |
results = JSON::parse open(URI.parse(URI.encode(url.strip))).read | |
results['data']['translations'][0]['translatedText'] | |
end | |
get '/' do | |
erb :index | |
end | |
post '/translate.json' do | |
content_type "application/json" | |
text = params[:q] | |
source = params[:source] || 'en' | |
targets = params[:targets] || ['es', 'pt'] | |
@translations = {} | |
targets.each do |target| | |
@translations[target] = translate(text, source, target) | |
end | |
@translations.to_json | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment