class Translatomatic::CLI::Main

Main command line interface

Public Instance Methods

convert(source, target) click to toggle source

Convert a resource file from one format to another @param source [String] An existing resource file @param target [String] The name of a target resource file @return [void]

# File lib/translatomatic/cli/main.rb, line 68
def convert(source, target)
  run do
    converter = Translatomatic::Converter.new
    converter.convert(source, target)
  end
end
display(file = nil, *keys) click to toggle source

Display values from a resource bundle @param file [String] Path to resource file @param keys [Array<String>] Optional list of locales @return [void]

# File lib/translatomatic/cli/main.rb, line 29
def display(file = nil, *keys)
  run do
    source_files = parse_list(file, conf.get(:source_files))
    locales = conf.get(:target_locales, for_file: file)
    source_files.each do |path|
      raise t('file.not_found', file: path) unless File.exist?(path)
      source = Translatomatic::ResourceFile.load(path)
      display_properties(source, keys)
      locales.each do |locale|
        path = source.locale_path(locale)
        next if path == source.path || !path.exist?
        resource = Translatomatic::ResourceFile.load(path)
        display_properties(resource, keys)
      end
    end
  end
end
providers() click to toggle source

List available translation providers @return [void]

# File lib/translatomatic/cli/main.rb, line 79
def providers
  run { display_providers }
end
strings(*files) click to toggle source

Extract strings from non-resource files @param files [Array<String>] List of paths to files @return [void]

# File lib/translatomatic/cli/main.rb, line 52
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
version() click to toggle source

Display version number @return [void]

# File lib/translatomatic/cli/main.rb, line 87
def version
  puts "Translatomatic v#{Translatomatic::VERSION}"
end

Private Instance Methods

available_providers() click to toggle source
# File lib/translatomatic/cli/main.rb, line 140
def available_providers
  config_all = Translatomatic.config.all
  available = {}
  configured = Translatomatic::Provider.available(config_all)
  configured.each { |i| available[i.class.name] = true }
  available
end
display_properties(source, keys) click to toggle source
# File lib/translatomatic/cli/main.rb, line 158
def display_properties(source, keys)
  puts t('cli.file_source', file: source)
  rows = []
  keys = source.properties.keys if keys.empty?
  keys.each do |key|
    value = source.get(key)
    rows << [key + ':', value]
  end
  print_table(rows, indent: 2)

  if options[:sentences]
    puts t('cli.sentences')
    source.sentences.each do |sentence|
      puts '- ' + sentence.to_s
    end
  end

  puts
end
display_provider_options() click to toggle source
# File lib/translatomatic/cli/main.rb, line 104
def display_provider_options
  rows = []
  Translatomatic::Provider.types.each do |klass|
    rows += provider_option_rows(klass)
  end
  headers = %i[name option description env]
  heading = headers.collect { |i| t("cli.provider.#{i}") }
  print_table(add_table_heading(rows, heading), indent: 2)
end
display_provider_status() click to toggle source
# File lib/translatomatic/cli/main.rb, line 129
def display_provider_status
  types = Translatomatic::Provider.types
  available = available_providers
  rows = types.sort_by { |i| available[i.name] ? 0 : 1 }.map do |klass|
    provider_status_row(klass, available)
  end
  headers = %i[name available]
  heading = headers.collect { |i| t("cli.provider.#{i}") }
  print_table(add_table_heading(rows, heading), indent: 2)
end
display_providers() click to toggle source

@return [String] A description of all providers and options

# File lib/translatomatic/cli/main.rb, line 94
def display_providers
  puts t('provider.options') + "\n\n"
  display_provider_options
  puts

  puts t('provider.status') + "\n\n"
  display_provider_status
  puts
end
provider_option_row(name, opt) click to toggle source
# File lib/translatomatic/cli/main.rb, line 120
def provider_option_row(name, opt)
  args = []
  args << name
  args << '--' + opt.name.to_s.tr('_', '-')
  args << opt.description
  args << opt.env_name ? "ENV[#{opt.env_name}]" : ''
  args
end
provider_option_rows(klass) click to toggle source
# File lib/translatomatic/cli/main.rb, line 114
def provider_option_rows(klass)
  name = klass.name.demodulize
  opts = klass.options || []
  opts.collect { |i| provider_option_row(name, i) }
end
provider_status_row(klass, available) click to toggle source
# File lib/translatomatic/cli/main.rb, line 148
def provider_status_row(klass, available)
  name = klass.name.demodulize
  avail = available[klass.name] ? 'yes' : 'no'
  args = []
  args << name
  args << t('cli.provider.available_' + avail)
  args << Translatomatic::Provider.get_error(name)
  args
end