Sha256: 88ca9feb3bf611b0bcbaff8351e1c60ccb9791e09f68f163f953eb6fafe09fe9

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'deepl'

# The part where the actual translation happens.
module Translation
  @@languages = %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].freeze
  attr_accessor :language, :to_language

  def available_languages
    @@languages
  end

  # Translates self to the desired language. \
  # Sample usage:
  # hello = 'hello'\
  # hello.to_language = 'fi'
  # moi = hello.translate
  # pp moi
  # # 'Hei'
  # @return [String] a new and shiny translated string!
  def translate
    call_deepl(self, language, to_language)
  end

  # Replaces self with the translation. \
  # Sample usage:
  # hello = 'hello'\
  # hello.to_language = 'fi'
  # hello.translate!
  # pp hello
  # # 'Hei'
  # @return [String] self replaced with the new translation
  def translate!
    replace translate
  end

  # Translates the string itself to a language the user wants to translate it to.
  # Sample usage:
  # 'hello'.translate_to_fi
  # # Hei
  #
  # @param [String] the language to translate to, e.g. "fi"
  # @return [String] the contents translated to another language
  @@languages.each do |lan|
    define_method("translate_to_#{lan}") do |language = lan|
      call_deepl(self, self.language, language)
    end
    alias_method "t_to_#{lan}", "translate_to_#{lan}"
  end

  private

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

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