Skip to content

Instantly share code, notes, and snippets.

@tbilous
Created July 12, 2019 06:00
Show Gist options
  • Save tbilous/7b6e4a0066cc3bbd1155fb2fefc8ec65 to your computer and use it in GitHub Desktop.
Save tbilous/7b6e4a0066cc3bbd1155fb2fefc8ec65 to your computer and use it in GitHub Desktop.
Part 2
#!/usr/bin/env ruby
# example: ruby task_p2.rb
require 'json'
require 'yaml'
def set(hash, path, val)
parts = path.split('.')
scope = hash
parts[0..-2].each do |hop|
scope[hop] ||= {}
scope = scope.fetch(hop)
raise "Could not add value into scalar property #{path}" unless scope.is_a?(Hash)
end
scope[parts[-1]] = val
end
def build_tree(data)
res = {}
data.each do |k, v|
set(res, k, v)
end
res
end
incoming_file = JSON.pretty_generate(YAML.load_file('translations_simple.yml'))
hash = JSON.parse(incoming_file)
res = build_tree(hash).to_yaml
puts res
File.write('./translations.yml', res)
---
en:
pets:
types:
cat: Cat
dog: Dog
title: My lovely pets
actions:
add: Add
remove: Remove
language: "<strong>Language</strong>"
'en.pets.types.cat': Cat
'en.pets.types.dog': Dog
'en.pets.title': My lovely pets
'en.actions.add': Add
'en.actions.remove': Remove
'en.language': <strong>Language</strong>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment