Skip to content

Commit

Permalink
Create trans.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
limingth committed Aug 7, 2013
1 parent dd6172b commit 5c67bfc
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions prj-dict/trans.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env ruby

require 'uri'
require 'net/http'
require 'pp'
require 'json'
require 'fileutils'

def translate(line, sl, tl)
url = "http://translate.google.cn/translate_a/t?client=t&hl=#{tl}&sl=#{sl}&tl=#{tl}&ie=UTF-8&oe=UTF-8&trs=1&sc=1&q=#{line}"
uri = URI.parse URI::encode(url)
r = Net::HTTP.get(uri)
JSON::parse(r.gsub(/,+/, ',')).first.first.first
end

fail 'no filename' if not File.file? ARGV[0].to_s

FileUtils.cp ARGV[0], ARGV[0]+".out"
of = File.open ARGV[0]+".out", "w:UTF-8"
File.open ARGV[0], "r:UTF-8" do |infile|
infile.each_line do |line|
line.chomp!
STDOUT.puts line
of.puts line
if line =~ /\/\/(.+)$/
tr = translate $1, 'en', 'ch'
of.puts "//TRANSLATE: #{tr}"
end
end
end

0 comments on commit 5c67bfc

Please sign in to comment.