Skip to content

Instantly share code, notes, and snippets.

@yohfee
Created January 21, 2011 02:06
Show Gist options
  • Save yohfee/789126 to your computer and use it in GitHub Desktop.
Save yohfee/789126 to your computer and use it in GitHub Desktop.
google_language.rb
# coding: utf-8
require 'open-uri'
require 'json'
module GoogleLanguage
extend self
URL = "http://ajax.googleapis.com/ajax/services/language/translate"
def translate(file)
IO.foreach(file) do |line|
query = URI.encode(line)
uri = "#{URL}?v=1.0&q=#{query}&langpair=en%7Cja"
json = open(uri)
data = JSON.parse(json.string)
puts data['responseData']['translatedText']
end
end
end
if $0 == __FILE__
puts GoogleLanguage.translate(ARGV[0])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment