Class: I18nYaml::Generators::ModelGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
I18nYamlGenerator::Helpers, Rails::Generators::ResourceHelpers
Defined in:
lib/generators/i18n_yaml/model/model_generator.rb

Overview

:nodoc:

Constant Summary collapse

SUPPORTED_ORMS =
%w(active_record active_model mongoid)

Instance Method Summary collapse

Methods included from I18nYamlGenerator::Helpers

#comment_yaml_body, #wrap_hash

Instance Method Details

#model_i18n_yaml_fileObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generators/i18n_yaml/model/model_generator.rb', line 13

def model_i18n_yaml_file
  current_orm = options.orm.to_s

  unless SUPPORTED_ORMS.include? current_orm
    say "Not creating translation file - '#{current_orm}' not supported"
    return
  end

  config = Rails.application.config.i18n_yaml_generator

  I18n.available_locales.each do |locale|
    hash_for_yaml = {}

    # Model name
    hash_for_yaml.deep_merge! wrap_hash(human_name, [current_orm, 'models'] + i18n_scope.split('.'))
    
    # Attributes
    hash_for_yaml.deep_merge! wrap_hash(attributes_hash, [current_orm, 'attributes'] + i18n_scope.split('.'))

    # Errors
    hash_for_yaml.deep_merge! wrap_hash({ singular_name => nil }, [current_orm, 'errors'])

    # Helpers
    hash_for_yaml.deep_merge! wrap_hash({ singular_name => nil }, ['helpers'])

    yaml_content = { locale.to_s => hash_for_yaml }.to_yaml

    destination_path = File.join('config/locales/models', "#{file_path}.#{locale}.yml")

    create_file destination_path, options.commented ? comment_yaml_body(yaml_content) : yaml_content
  end
end