Class: I18n::TranslationGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/i18n/translation/translation_generator.rb

Instance Method Summary collapse

Instance Method Details

#mainObject

option: include_timestamps



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/i18n/translation/translation_generator.rb', line 7

def main
  unless file_name =~ /^[a-zA-Z]{2}([-_][a-zA-Z]+)?$/
    log 'ERROR: Wrong locale format. Please input in ?? or ??-?? format.'
    exit
  end
  log "translating models to #{locale_name}..."
  I18n.locale = locale_name
  Rails.application.eager_load!

  keys = aggregate_keys
  translations = translate_all(keys)

  yaml = I27r::YamlDocument.load_yml_file "config/locales/translation.#{locale_name}.yml"
  each_value [], translations do |parents, value|
    if value.is_a? String
      yaml[[locale_name.to_s] + parents] = value
    else
      value.each do |key, val|
        yaml[[locale_name.to_s] + parents + [key]] = val
      end
    end
  end

  unless (yaml_string = yaml.to_s(true)).blank?
    create_file "config/locales/translation.#{locale_name}.yml", yaml_string
  end
end