class DeeplAPI::CLI

This class implements the deepl command line utility.

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/deepl_api/cli.rb, line 9
def self.exit_on_failure?
  # Exit in case of missing arguments etc.
  true
end

Public Instance Methods

languages() click to toggle source
# File lib/deepl_api/cli.rb, line 22
def languages
  deepl = instance
  source_langs = deepl.source_languages
  target_langs = deepl.target_languages

  puts("DeepL can translate from the following source languages:")
  source_langs.each { |language, name| puts("  #{language.ljust(5)} (#{name})") }
  puts
  puts("DeepL can translate to the following target languages:")
  target_langs.each { |language, name| puts("  #{language.ljust(5)} (#{name})") }
end
translate() click to toggle source
# File lib/deepl_api/cli.rb, line 43
def translate
  formality = DeeplAPI::Formality::DEFAULT
  formality = DeeplAPI::Formality::LESS if options["formality-less"]
  formality = DeeplAPI::Formality::MORE if options["formality-more"]

  text = if options["input-file"]
           File.read(options["input-file"])
         else
           $stdin.read
         end

  result = instance.translate(
    source_language: options["source-language"],
    target_language: options["target-language"],
    preserve_formatting: options["preserve-formatting"],
    formality: formality,
    texts: [text]
  )

  text = result.map { |entry| entry["text"] }.join

  if options["output-file"]
    File.write(options["output-file"], text)
  else
    puts text
  end
end
usage_information() click to toggle source
# File lib/deepl_api/cli.rb, line 15
def usage_information
  usage = instance.usage_information
  puts("Available characters per billing period: #{usage.character_limit}")
  puts("Characters already translated in the current billing period: #{usage.character_count}")
end