Class: Translatomatic::CLI

Inherits:
Thor
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/cli.rb

Instance Method Summary collapse

Methods included from Util

#locale, #log, #string

Instance Method Details

#display(file, *keys) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/translatomatic/cli.rb', line 68

def display(file, *keys)
  run do
    source = Translatomatic::ResourceFile.load(file)
    keys = source.properties.keys if keys.empty?
    display_keys(source, keys)

    # TODO: if locales not specified, determine the list of locales from
    # all the files in the resource bundle.

    locales = (options[:locales] || "").split(',').flatten.compact
    locales << Translatomatic::Locale.default.to_s if locales.empty?
    locales.each do |locale|
      display_properties(source, locale)
    end
  end
end

#listObject



97
98
99
# File 'lib/translatomatic/cli.rb', line 97

def list
  run { puts Translatomatic::Translator.list }
end

#strings(*files) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/translatomatic/cli.rb', line 85

def strings(*files)
  run do
    strings = []
    files.each do |file|
      extractor = Translatomatic::Extractor::Base.new(file)
      strings << extractor.extract
    end
    puts strings.join("\n")
  end
end

#translate(file, *locales) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/translatomatic/cli.rb', line 28

def translate(file, *locales)
  run do
    log.info("Dry run: files will not be translated or written") if options[:dry_run]
    raise "One or more locales required" if locales.empty?

    config.logger.level = Logger::DEBUG if options[:debug]

    # load source file

    raise "File not found: #{file}" unless File.exist?(file)
    source = Translatomatic::ResourceFile.load(file, options[:source_locale])
    raise "Unsupported file type #{file}" unless source

    # set up database

    Translatomatic::Database.new(options)

    # select translator

    translator = select_translator(options)
    log.info("Using translator #{translator.name}")
    log.debug("Locales: #{locales}, Properties: #{source.properties.length}")

    # set up converter

    translation_count = calculate_translation_count(source, locales)
    converter_options = options.merge(
      translator: translator, listener: progress_updater(translation_count)
    )
    converter = Translatomatic::Converter.new(converter_options)

    # convert source to locale(s)

    locales.each { |i| converter.translate(source, i) }

    log.info converter.stats
    config.logger.finish

    share_translations(converter) if options[:share]
  end
end

#versionObject



102
103
104
# File 'lib/translatomatic/cli.rb', line 102

def version
  puts "Translatomatic version #{Translatomatic::VERSION}"
end