Skip to content

Instantly share code, notes, and snippets.

@branch14
Created June 6, 2011 14:45

Revisions

  1. branch14 revised this gist Jun 6, 2011. No changes.
  2. branch14 revised this gist Jun 6, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions merge_locales.rb
    Original file line number Diff line number Diff line change
    @@ -48,8 +48,8 @@ def deep_merge(other, &bloc)
    res[key]
    else
    warn "Conflict on key: '#{key}'"
    warn " 1. #{res[key]}"
    warn " 2. #{other[key]}"
    warn " 1. #{res[key].inspect}"
    warn " 2. #{other[key].inspect}"
    select = ask('')
    case select
    when '1' then res[key]
  3. branch14 created this gist Jun 6, 2011.
    63 changes: 63 additions & 0 deletions merge_locales.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/usr/bin/env ruby
    #
    # synopsis
    #
    # ruby merge_locales.rb config/locales translations.yml

    require 'yaml'
    require 'rubygems'
    require 'highline/import'

    ::Hash.class_eval do
    class MergeConflict < StandardError; end
    def deep_merge(other, &bloc)
    other.keys.inject(dup) do |result, key|
    begin
    case result[key]
    when Hash
    if other[key].is_a?(Hash)
    result[key] = result[key].deep_merge(other[key], &bloc)
    result
    else
    raise MergeConflict
    end
    when nil then result.merge key => other[key]
    else
    raise MergeConflict
    end
    rescue MergeConflict
    if bloc.nil?
    result[key] = other[key]
    else
    result[key] = bloc.call(result, other, key)
    end
    result
    end
    end
    end
    end

    result = ARGV.inject({}) do |result, path|
    files = [path] if File.file?(path) && path.match(/\.yml$/)
    files ||= Dir.glob( File.join(path, '**', '*.yml')).to_a
    files.inject(result) do |inner_result, file|
    warn "loading #{file}"
    yaml = File.open(file) { |yf| YAML::load(yf) }
    inner_result.deep_merge(yaml) do |res, other, key|
    if res[key] == other[key]
    res[key]
    else
    warn "Conflict on key: '#{key}'"
    warn " 1. #{res[key]}"
    warn " 2. #{other[key]}"
    select = ask('')
    case select
    when '1' then res[key]
    when '2' then other[key]
    end
    end
    end
    end
    end

    YAML.dump(result, STDOUT)