Created
January 1, 2009 14:56
-
-
Save ledermann/42287 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'ya2yaml' # Better than to_yaml, because it saves as UTF-8 and sorts hash by keys | |
namespace :locales do | |
desc "Parses ActiveRecord models and updates locale files" | |
task :update => :environment do | |
Dir.glob(File.join(RAILS_ROOT, 'config', 'locales', '*.yml') ).each do |locale_file| | |
# Read single locale file | |
locale_hash = YAML.load(File.read(locale_file)) | |
language = locale_hash.keys.first | |
# Find all ActiveRecord models | |
all_models = Dir.glob(File.join(RAILS_ROOT, 'app', 'models', '*.rb')).map { |path| path[/.+\/(.+).rb/,1] } | |
ar_models = all_models.select { |model| model.camelize.constantize < ActiveRecord::Base }.collect { |model| model.camelize.constantize } | |
ar_models.each do |model| | |
# Model.human_name | |
locale_hash[language]['activerecord']['models'][model.name.underscore] = model.human_name | |
# Model.human_attribute_name | |
model.column_names.each do |column_name| | |
unless column_name.match /(_id\z|\Aid\z|_type\z)/ | |
(locale_hash[language]['activerecord']['attributes'][model.name.underscore] ||= {})[column_name] = model.human_attribute_name(column_name) | |
end | |
end | |
end | |
# Write updated locale file | |
File.open(locale_file, 'w') do |file| | |
file.write(locale_hash.ya2yaml) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment