class SayAgain::CLI::Commands::Translate

Public Instance Methods

call(language:, filename:, out: nil, **) click to toggle source
# 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