Created
          July 12, 2019 06:00 
        
      - 
      
 - 
        
Save tbilous/7b6e4a0066cc3bbd1155fb2fefc8ec65 to your computer and use it in GitHub Desktop.  
    Part 2
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/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) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | --- | |
| en: | |
| pets: | |
| types: | |
| cat: Cat | |
| dog: Dog | |
| title: My lovely pets | |
| actions: | |
| add: Add | |
| remove: Remove | |
| language: "<strong>Language</strong>" | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | '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