lib/aigu/importer.rb in aigu-0.1.1 vs lib/aigu/importer.rb in aigu-0.2
- old
+ new
@@ -1,12 +1,13 @@
module Aigu
class Importer
ARRAY_REGEX = /___KEY___(?<index>\d+)$/
- def initialize(input_file: nil, output_directory: nil)
+ def initialize(input_file: nil, output_directory: nil, locale: nil)
@input_file = input_file
@output_directory = output_directory
+ @locale = locale
end
def process!
puts "Generating YAML files in `#{@output_directory}` based on Accent-generated `#{@input_file}` file"
puts '---'
@@ -23,15 +24,16 @@
def parse_json
json = File.read(@input_file)
@object = JSON.parse(json)
@object = expand_content_values(@object)
+ @object = localize_content_keys(@object)
end
def write_yaml_files
@blob.each_pair do |file_name, hash|
- file_path = File.join(@output_directory, "#{file_name}.yml")
+ file_path = File.join(@output_directory, "#{file_name}.#{@locale}yml")
puts "Generating #{file_path}"
FileUtils.mkdir_p(File.dirname(file_path))
File.open(file_path, 'w+') do |file|
file << hash.to_yaml(line_width: 100_000_000)
@@ -53,9 +55,16 @@
hash = hash[part]
else
hash[part] = value
end
end
+ end
+ end
+
+ def localized_content_keys(content)
+ content.reduce({}) do |memo, (key, value)|
+ localized_key = key.gsub(/^([^|]+)\|/, "\\1.#{@locale}|#{@locale}.")
+ memo.merge localized_key => value
end
end
def expand_content_values(content)
content.reduce({}) do |memo, (key, value)|