Created
January 5, 2010 12:35
-
-
Save iain/269353 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.join(File.dirname(__FILE__), 'config', 'environment') | |
require 'ya2yaml' | |
module PrettyYaml | |
class << self | |
def write(filename, hash) | |
File.open(filename, "w") do |f| | |
f.write(yaml(hash)) | |
end | |
end | |
def yaml(hash) | |
method = hash.respond_to?(:ya2yaml) ? :ya2yaml : :to_yaml | |
string = hash.deep_stringify_keys.send(method) | |
string.gsub("!ruby/symbol ", ":").sub("---","").split("\n").map(&:rstrip).join("\n").strip | |
end | |
end | |
end | |
class Hash | |
def deep_stringify_keys | |
new_hash = {} | |
self.each do |key, value| | |
new_hash.merge!(key.to_s => (value.is_a?(Hash) ? value.deep_stringify_keys : value)) | |
end | |
new_hash | |
end | |
end | |
class ActiveModelProposal | |
attr_accessor :locale | |
def translations | |
@translations ||= I18n.t(:activerecord, :locale => locale).deep_stringify_keys | |
end | |
def specific | |
@specific ||= {} | |
end | |
def global | |
@global ||= {} | |
end | |
def transform! | |
if translations["models"] | |
translations["models"].each do |name, tr| | |
specific[name] ||= {} | |
specific[name]["name"] = tr | |
end | |
end | |
if translations["attributes"] | |
translations["attributes"].each do |name, attrs| | |
specific[name] ||= {} | |
specific[name]["attributes"] = attrs | |
end | |
end | |
if errors = translations["errors"] | |
if messages = errors["messages"] | |
global["messages"] = messages | |
end | |
if models = errors["models"] | |
models.each do |name, messages| | |
specific[name] ||= {} | |
specific[name]["messages"] = messages | |
end | |
end | |
end | |
end | |
def before | |
{ locale => { "activerecord" => translations } } | |
end | |
def after | |
{ locale => { "models" => { "global" => global, "specific" => specific } } } | |
end | |
end | |
amp = ActiveModelProposal.new | |
amp.locale = "nl" | |
amp.transform! | |
PrettyYaml.write('before.yml', amp.before) | |
PrettyYaml.write('after.yml', amp.after) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment