Class: ExtractI18n::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/extract_i18n/cli.rb

Overview

Cli Class

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



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
45
46
47
48
49
50
# 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

Instance Method Details

#process_file(file_path) ⇒ Object



66
67
68
69
70
71
72
73
74
# 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

#runObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# 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