lib/aigu/exporter.rb in aigu-0.1.1 vs lib/aigu/exporter.rb in aigu-0.2

- old
+ new

@@ -1,10 +1,11 @@ module Aigu class Exporter - def initialize(output_file: nil, input_directory: nil) + def initialize(output_file: nil, input_directory: nil, locale: nil) @output_file = output_file @input_directory = input_directory + @locale = locale end def process! puts "Generating Accent JSON file `#{@output_file}` based on YAML localization files in `#{@input_directory}` directory" puts '---' @@ -19,26 +20,34 @@ protected def build_output @output = {} - Dir[File.join(@input_directory, '**', '*.yml')].each do |file| + Dir[File.join(@input_directory, '**', "*.#{@locale}.yml")].each do |file| content = YAML.load_file(file) base_key = file.gsub(@input_directory, '').gsub(/^\/*/, '').gsub(/\.yml$/, '|') - content = flattenize_hash(content) + content = flattenize_hash(content, base_key) content = flattenize_content_values(content) - @output.merge! flattenize_hash(content, base_key) + content = globalize_content_keys(content) + @output.merge! content end end def write_json_file file_path = @output_file puts "Generating #{file_path}" FileUtils.mkdir_p(File.dirname(file_path)) File.open(file_path, 'w+') do |file| file << @output.to_json + end + end + + def globalize_content_keys(content) + content.reduce({}) do |memo, (key, value)| + globalized_key = key.gsub(/\.#{@locale}\|#{@locale}\./, '|') + memo.merge globalized_key => value end end def flattenize_content_values(hash) result = {}