Sha256: 64f14f7e7ed5a97aa4ef7ec3e67ae940574621b4c683d227e970276c63d52efe

Contents?: true

Size: 1.75 KB

Versions: 5

Compression:

Stored size: 1.75 KB

Contents

module I18n::Tasks
  module Command
    module Options
      module Locales
        include Command::DSL

        cmd_opt :locales, {
            short: :l,
            long:  :locales=,
            desc:  t('i18n_tasks.cmd.args.desc.locales_filter'),
            conf:  {as: Array, delimiter: /\s*[+:,]\s*/, default: 'all', argument: true, optional: false},
            parse: :parse_locales
        }

        cmd_opt :locale, {
            short: :l,
            long:  :locale=,
            desc:  t('i18n_tasks.cmd.args.desc.locale'),
            conf:  {default: 'base', argument: true, optional: false},
            parse: :parse_locale
        }

        cmd_opt :locale_to_translate_from, cmd_opt(:locale).merge(
            short: :f,
            long: :from=,
            desc: t('i18n_tasks.cmd.args.desc.locale_to_translate_from'))

        def parse_locales(opt, key = :locales)
          argv    = Array(opt[:arguments]) + Array(opt[key])
          locales = if argv == ['all'] || argv == 'all' || argv.blank?
                      i18n.locales
                    else
                      explode_list_opt(argv).map { |v| v == 'base' ? base_locale : v }
                    end
          locales.each { |locale| validate_locale!(locale) }
          log_verbose "locales for the command are #{locales.inspect}"
          opt[key] = locales
        end

        def parse_locale(opt, key = :locale)
          val      = opt[key]
          opt[key] = base_locale if val.blank? || val == 'base'
          opt[key]
        end

        VALID_LOCALE_RE = /\A\w[\w\-\.]*\z/i

        def validate_locale!(locale)
          raise CommandError.new(I18n.t('i18n_tasks.cmd.errors.invalid_locale', invalid: locale)) if VALID_LOCALE_RE !~ locale
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
i18n-tasks-0.7.12 lib/i18n/tasks/command/options/locales.rb
i18n-tasks-0.7.11 lib/i18n/tasks/command/options/locales.rb
i18n-tasks-0.7.10 lib/i18n/tasks/command/options/locales.rb
i18n-tasks-0.7.9 lib/i18n/tasks/command/options/locales.rb
i18n-tasks-0.7.8 lib/i18n/tasks/command/options/locales.rb