class I18n::Magic::Commands::Add

Public Instance Methods

execute() click to toggle source
# File lib/i18n/magic/commands/add.rb, line 11
def execute
  abort('missing key operand') unless @options[:key].present?
  if @options[:values].size.positive?
    add_values_from_args
  else
    add_values_from_input
  end
end

Private Instance Methods

add_values_from_args() click to toggle source
# File lib/i18n/magic/commands/add.rb, line 34
def add_values_from_args
  @options[:values].each do |value|
    next unless value.present?
    record = I18n::Magic::Entity::TranslationRecord.new(@options[:key], value)
    abort('invalid key,value pair !') unless record.valid?
    locale = I18n::Magic::Helpers::StringOps.locale(value, @options[:locale_files_path])
    translation_file = I18n::Magic::Entity::TranslationFile.new(locale, @options[:locale_files_path])
    abort('translation file does not exist !') unless translation_file.exists?
    translation_file.add(record)
  end
end
add_values_from_input() click to toggle source
# File lib/i18n/magic/commands/add.rb, line 22
def add_values_from_input
  I18n::Magic::Helpers::Environment.locales(@options[:locale_files_path]).each do |locale|
    print "#{locale} value for #{@options[:key]} (empty to skip) : "
    value = STDIN.gets
    record = I18n::Magic::Entity::TranslationRecord.new(@options[:key], value)
    next unless record.valid?
    translation_file = I18n::Magic::Entity::TranslationFile.new(locale, @options[:locale_files_path])
    abort('translation file does not exist !') unless translation_file.exists?
    translation_file.add(record)
  end
end