Skip to content

Instantly share code, notes, and snippets.

@agustinf
Created November 12, 2013 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agustinf/7431101 to your computer and use it in GitHub Desktop.
Save agustinf/7431101 to your computer and use it in GitHub Desktop.
Moving yaml locale to database
# unnest will let me flatten a Hash
class Hash
def unnest
new_hash = {}
each do |key,val|
if val.is_a?(Hash)
new_hash.merge!(val.prefix_keys("#{key}."))
else
new_hash[key] = val
end
end
new_hash
end
def prefix_keys(prefix)
Hash[map{|key,val| [prefix + key, val]}].unnest
end
end
#Use to add a locale
def addLocale(locale)
a = YAML.load(File.open("config/locales/#{locale}.yml"))[locale]
Array(a.unnest).each do |t|
Translation.create! :locale => locale, :key => t[0], :value => t[1]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment