class Makeloc::DoGenerator

Public Instance Methods

do() click to toggle source
# File lib/makeloc/generators/do/do_generator.rb, line 11
def do
  ref_locale_fp = Pathname.new(source_file)

  raise "File #{ref_locale_fp} not found" unless ref_locale_fp.exist?

  ref_lang = locale_fn2lang(ref_locale_fp)
  ref_data = YAML.load(File.open(File.expand_path(ref_locale_fp)))[ref_lang]

  target_fp = ref_locale_fp.to_s.gsub(".#{ref_lang}.",".#{target_lang}.")
  
  # initializing target data with ref data keys and nil values
  target_data = ref_data.deep_dup.update_leaves!{|k,v| nil }
  
  # updating target data with original values if they exists
  if File.exist? target_fp
    existing_target_data = YAML.load(File.open(File.expand_path(target_fp)))[target_lang]
    
    # delete extra keys present in original target data
    (existing_target_data.flatten_keys - target_data.flatten_keys).each{|extra_key| existing_target_data.delete_at(extra_key)} if options[:strict]

    target_data.deep_merge!(existing_target_data)
    
  end
  
  create_file(target_fp){ {target_lang => target_data}.to_yaml(:line_width => -1) } # line_width => -1 to disable indentation

end

Private Instance Methods

locale_fn2lang(fn) click to toggle source
# File lib/makeloc/generators/do/do_generator.rb, line 41
def locale_fn2lang(fn)
  File.basename(fn,'.yml').split('.').last
end