Sha256: 74c6427551aa06ddf5b404c1bc47b3b31cbe32dcc9c1f8594382231ebc5b9036

Contents?: true

Size: 840 Bytes

Versions: 1

Compression:

Stored size: 840 Bytes

Contents

require 'byebug'
require 'deepl'

# The part where the actual translation happens.
module Translation
  attr_accessor :language, :to_language

  def available_languages
    TranslateSelf::AVAILABLE_LANGUAGES
  end

  def translate
    call_deepl(self, language, to_language)
  end

  def translate!
    replace translate
  end

  %w[bg cs da de el en es et fi fr hu it ja lt lv nl pl pt ro ru sk sl sv zh].each do |lan|
    define_method("translate_to_#{lan}") do |language = lan|
      call_deepl(self, self.language, language)
    end
  end

  private

  def call_deepl(text, language = self.language, to_lan = to_language)
    raise 'No language given!' if to_lan.nil?

    response = DeepL.translate text, language, to_lan
    self.language = response.detected_source_language.downcase if self.language.nil?
    response.text
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translate_self-0.1.0 lib/translate_self/translation.rb