Skip to content

Instantly share code, notes, and snippets.

@aryondev
Last active September 4, 2018 11:25
Show Gist options
  • Save aryondev/3bcc4df552da8d0686c7bc3fe24ede5d to your computer and use it in GitHub Desktop.
Save aryondev/3bcc4df552da8d0686c7bc3fe24ede5d to your computer and use it in GitHub Desktop.
require 'json'
require 'bing_translator'
translator = BingTranslator.new('COGNITIVE_SUBSCRIPTION_KEY')
content = File.read('content.json')
json = JSON.parse(content)
parse_hash = lambda do |key, value|
if value.is_a?(Hash)
{ key => value.map { |k,v| parse_hash.call(k,v) } }.reduce({}, &:merge)
else
{ key => translator.translate(value, from: 'en', to: 'ru') }
end
end
new_json = json.map do |key, value|
new_json[key] = parse_hash.call(value)
end.reduce({}, &:merge)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment