Skip to content

Instantly share code, notes, and snippets.

@jmccartie
Created October 12, 2011 02:55
Show Gist options
  • Save jmccartie/1280130 to your computer and use it in GitHub Desktop.
Save jmccartie/1280130 to your computer and use it in GitHub Desktop.
Use the Google Translate API to translate Babelwith.me strings
#
# Not currently dynamic for a i18n file -- it spits out "en/txt and en/errors"
#
require 'cgi'
require 'yaml'
require 'httparty'
@strings = YAML.load(File.read("")).to_hash
@api_key = ''
@language = 'hi'
def translate(string)
response = HTTParty.get("https://www.googleapis.com/language/translate/v2?key=#{@api_key}&q=#{CGI::escape(string)}&source=en&target=#{@language}")
response['data']['translations'].first['translatedText']
end
puts "#{@language}:"
puts "\s\stxt:"
@strings['en']['txt'].each do |key, value|
puts "\s\s\s\s#{key}: \"#{translate(value)}\""
end
puts "\s\serrors:"
@strings['en']['error'].each do |key, value|
puts "\s\s\s\s#{key}: \"#{translate(value)}\""
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment