Class: SayAgain::CLI::Commands::Translate

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/sayagain/cli/commands.rb

Instance Method Summary collapse

Instance Method Details

#call(language:, filename:, out: nil) ⇒ Object



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
# File 'lib/sayagain/cli/commands.rb', line 19

def call(language:, filename:, out: nil, **)
    file = File.open(filename)
    puts "Translating #{filename} to #{language.capitalize}.."

    file_data = file.read

    parsed_file = Nokogiri::HTML(file_data)

    parsed_file.traverse do |x|
        if x.text? && !x.content.strip.empty?
            x.content = EasyTranslate.translate(x.content, to: language)
        end
    end


    if !out.nil?
        File.open(out, "w") { |f| f.write parsed_file }
    else
        puts parsed_file
    end

    puts "Successfully translated!"

    file.close
end