Sha256: 41f73a38a5df6760a90d2581bf79c863daee23724a6675a60fcf272d77861597

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

module Aigu
  class EmberEngineImporter < 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))

        javascript_object = JSON.pretty_generate(translations)
        content = "export default #{javascript_object};\n"

        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

3 entries across 3 versions & 1 rubygems

Version Path
aigu-1.2 lib/aigu/ember_engine_importer.rb
aigu-1.1.1 lib/aigu/ember_engine_importer.rb
aigu-1.1 lib/aigu/ember_engine_importer.rb