Skip to content

Instantly share code, notes, and snippets.

@rkgarg
Forked from vraravam/translator.rb
Last active August 29, 2015 14:15
Show Gist options
  • Save rkgarg/1753728337137b79b279 to your computer and use it in GitHub Desktop.
Save rkgarg/1753728337137b79b279 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
if ARGV.size != 2
puts "Usage: #{$0} <from_language> <to_language>"
exit -1
end
require 'rubygems'
require 'ya2yaml' # this gem is needed for this to work!
require 'yaml'
require 'json'
require 'uri'
TRANSLATIONS_TABLE = {:"ja-JP" => :ja}
def translate(string)
command = <<-EOF
curl -s -A "Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0" "http://translate.google.com/translate_a/t?client=t&text=#{URI.escape(string)}&hl=#{@from_language}&sl=auto&tl=#{@googles_to_language}&multires=1&prev=conf&psl=auto&ptl=#{@from_language}&otf=1&it=sel.7123%2Ctgtd.3099&ssel=0&tsel=4&uptl=#{@googles_to_language}&sc=1"
EOF
command.strip!
res = `#{command}`.gsub!(/,+/, ",")
JSON.parse(res).first.first.first.strip
end
def process(hash)
hash.inject({}) do |h, pair|
key, value = pair
h[key] = value.kind_of?(Hash) ? process(value) : translate(value)
h
end
end
@from_language = ARGV[0].to_sym
@users_to_language = ARGV[1].to_sym
@googles_to_language = TRANSLATIONS_TABLE[@users_to_language] || @users_to_language
directory = "config/locales/"
hash = YAML.load_file("#{directory}#{@from_language}.yml")[@from_language.to_s]
result = process(hash)
File.open("#{directory}#{@users_to_language}.yml", 'w') do |out|
out.write({@users_to_language.to_s => result}.ya2yaml)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment