-
-
Save youpy/330334 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# Usage: ./translator.rb -t yi あれ、今日日食? | |
require 'ubygems' | |
require 'grope' | |
require 'optparse' | |
@env = Grope::Env.new | |
@env.load 'http://translate.google.com/' | |
def translate words, to, from | |
exit if(words.empty? || to.nil?) | |
from ||= 'auto' | |
@env.xpath('id("old_sl")/option[@value="%s"]' % from)[0].selected = true | |
@env.xpath('id("old_tl")/option[@value="%s"]' % to)[0].selected = true | |
id('source').value = words | |
id('text_form').submit | |
@env.wait(1) | |
puts id('result_box').textContent | |
end | |
def id(id) | |
@env.document.getElementById(id) | |
end | |
def list_languages | |
list = @env.xpath 'id("old_tl")/option[not(@disabled)]' | |
list.each do |x| | |
puts "#{x.value} : #{x.innerText}" | |
end | |
end | |
#---------------- | |
to, from = () | |
ARGV.options do |opt| | |
opt.on('-t LANG', '--to=LANG') {|v| to = v } | |
opt.on('-f LANG', '--from=LANG') {|v| from = v } | |
opt.on('-l', '--list') {|v| list_languages; exit } | |
opt.parse! | |
end | |
translate ARGV.join(' '), to, from |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment