Sha256: d584cc234d699e6a05dce513f92eccc318788951195504847d636b42f539237b

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

require 'rails_generator'
require 'rails_generator/commands'
require File.join(File.dirname(__FILE__), 'lib/translator')
include I18nModelsGeneratorModule

module I18nGenerator::Generator
  module Commands #:nodoc:
    module Create
      def models_yaml
        I18n.locale = locale_name
        models = model_filenames.map do |model_name|
          model = begin
            m = model_name.capitalize.constantize
            next unless m.ancestors.include? ActiveRecord::Base
            m
          rescue
            next
          end
          registered_t_name = I18n.t("activerecord.models.#{model_name}", :default => model_name)

          model.class_eval <<-END
  def self.english_name
    "#{model_name}"
  end

  def self.translated_name
    "#{registered_t_name != model_name ? registered_t_name : self.translator.translate(model_name)}"
  end
END
          model.content_columns.each do |col|
            next if %w[created_at updated_at].include? col.name
            registered_t_name = I18n.t("activerecord.attributes.#{model_name}.#{col.name}", :default => col.name)
            col.class_eval <<-END
  def translated_name
    "#{registered_t_name != col.name ? registered_t_name : self.translator.translate(col.name)}"
  end
END
          end
          model
        end.compact
        generate_yaml(locale_name, models)
      end

      private
      def model_filenames
        Dir.chdir("#{RAILS_ROOT}/app/models/") do
          Dir["**/*.rb"].map {|m| m.sub(/\.rb$/, '')}
        end
      end

      def generate_yaml(locale_name, models)
        template 'i18n:models.yml', "config/locales/models_#{locale_name}.yml", :assigns => {:locale_name => locale_name, :models => models}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amatsuda-i18n_generators-0.0.8 generators/i18n_models/i18n_models_command.rb