class ExtractI18n::CLI

Cli Class

Public Class Methods

new() click to toggle source
# File lib/extract_i18n/cli.rb, line 14
def initialize
  @options = {}
  ARGV << '-h' if ARGV.empty?
  OptionParser.new do |opts|
    opts.banner = "Usage: extract-i18n -l <locale> -w <target-yml> [path*]"

    opts.on('--version', 'Print version number') do
      puts ExtractI18n::VERSION
      exit 1
    end

    opts.on('-lLOCALE', '--locale=LOCALE', 'default locale for extraction (Default = en)') do |f|
      @options[:locale] = f || 'en'
    end

    opts.on('-nNAMESPACE', '--namespace=NAMESPACE', 'Locale base key to wrap locations in') do |f|
      @options[:namespace] = f
    end

    opts.on('-r', '--slim-relative', 'When activated, will use relative keys like t(".title")') do |f|
      @options[:relative] = f
    end

    opts.on('-yYAML', '--yaml=YAML-FILE', 'Write extracted keys to YAML file (default = config/locales/unsorted.LOCALE.yml)') do |f|
      @options[:write_to] = f || "config/locales/unsorted.#{@options[:locale]}"
    end

    opts.on('-h', '--help', 'Prints this help') do
      puts opts
      exit 1
    end
  end.parse!

  @options[:write_to] ||= "config/locales/unsorted.#{@options[:locale]}.yml"
  @options[:locale] ||= 'en'
  @files = ARGV
end

Public Instance Methods

process_file(file_path) click to toggle source
# File lib/extract_i18n/cli.rb, line 66
def process_file(file_path)
  puts "Processing: #{file_path}"
  ExtractI18n::FileProcessor.new(
    file_path: file_path,
    write_to: @options[:write_to],
    locale: @options[:locale],
    options: @options
  ).run
end
run() click to toggle source
# File lib/extract_i18n/cli.rb, line 52
def run
  paths = @files.empty? ? [] : @files
  paths.each do |path|
    if File.directory?(path)
      glob_path = File.join(path, '**', '*.rb')
      Dir.glob(glob_path) do |file_path|
        process_file file_path
      end
    else
      process_file path
    end
  end
end