Skip to content

Instantly share code, notes, and snippets.

@ku
Created December 11, 2014 06:18
Show Gist options
  • Select an option

  • Save ku/e17e3d65ec4c37b60149 to your computer and use it in GitHub Desktop.

Select an option

Save ku/e17e3d65ec4c37b60149 to your computer and use it in GitHub Desktop.
convert rails locale files into one json
#!/usr/bin/env ruby
#run in RAILS_ROOT directory
require 'json'
require 'yaml'
# http://stackoverflow.com/questions/9381553/ruby-merge-nested-hash
class ::Hash
def deep_merge(second)
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
self.merge(second, &merger)
end
end
#dir = File.dirname __FILE__
dict = nil
Dir.glob("./config/locales/**/*.yml").each do |yml|
yml = YAML.load File.open(yml).read
dict = dict ? dict.deep_merge(yml) : yml
end
def translatePlaceholder(v)
if v.respond_to? :each
if v.is_a? Array
v.each {|v| translatePlaceholder(v)}
else
v.each {|k, v| translatePlaceholder(v)}
end
else
v && v.is_a?(String) && v.gsub!(/%{(\w+)}/, '{{\1}}')
end
end
translatePlaceholder(dict)
locale = ARGV.shift
if locale
puts JSON.pretty_generate dict[locale]
else
puts JSON.pretty_generate dict
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment