Sha256: 22670c3a455554d957a8ce7d885cad8bcbb93d0759cf073ea669db9aa0719138

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

module Aigu
  class EmberPodImporter < Importer
    def process!
      puts "Generating JSON files in `#{@output_directory}` based on Accent-generated `#{@input_file}` file"
      puts '---'

      object = sorted_object_from_json(@input_file)
      blob = build_blob(object)
      write_json_files(blob, @output_directory)

      puts '---'
      puts 'Done'
    end

  protected

    def sorted_object_from_json(input_file)
      json = File.read(input_file)
      JSON.parse(json).sort_with_keys
    end

    def write_json_files(blob, output_directory)
      blob.each_pair do |file_name, translations|
        file_path = File.join(output_directory, file_name.gsub('__LOCALE__', @locale))
        puts "Generating #{file_path}"
        FileUtils.mkdir_p(File.dirname(file_path))

        content = JSON.pretty_generate(translations)

        File.open(file_path, 'w+') do |file|
          file << content
        end
      end
    end

    def build_blob(object)
      blob = Hash.recursive

      object.each_pair do |key, value|
        filename, flat_key = key.split('|')

        parts = flat_key.split('.')
        hash = blob[filename]

        parts.each_with_index do |part, index|
          if index + 1 < parts.length
            hash = hash[part]
          else
            hash[part] = value
          end
        end
      end

      blob
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
aigu-1.1 lib/aigu/ember_pod_importer.rb
aigu-1.0.3 lib/aigu/ember_pod_importer.rb
aigu-1.0.2 lib/aigu/ember_pod_importer.rb
aigu-1.0.1 lib/aigu/ember_pod_importer.rb
aigu-1.0 lib/aigu/ember_pod_importer.rb
aigu-0.7 lib/aigu/ember_pod_importer.rb