Module: Translation

Included in:
String
Defined in:
lib/translate_self/translation.rb

Overview

The part where the actual translation happens.

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#languageObject

Returns the value of attribute language.



6
7
8
# File 'lib/translate_self/translation.rb', line 6

def language
  @language
end

Instance Method Details

#available_languagesObject



17
18
19
# File 'lib/translate_self/translation.rb', line 17

def available_languages
  LANGUAGES
end

#lanString

Translates the string itself to a language the user wants to translate it to. Sample usage: ‘hello’.translate_to_fi # Hei

Parameters:

  • the (String)

    language to translate to, e.g. “fi”

Returns:

  • (String)

    the contents translated to another language



53
54
55
56
57
58
# File 'lib/translate_self/translation.rb', line 53

LANGUAGES.each do |lan|
  define_method("translate_to_#{lan}".to_sym) do |language = lan|
    call_deepl(self.language, language)
  end
  alias_method "to_#{lan}".to_sym, "translate_to_#{lan}".to_sym
end

#to_languageObject



8
9
10
# File 'lib/translate_self/translation.rb', line 8

def to_language
  @to_language || 'fi'
end

#to_language=(language) ⇒ Object



12
13
14
15
# File 'lib/translate_self/translation.rb', line 12

def to_language=(language)
  defroster if needs_defrosting?
  @to_language = LANGUAGES.include?(language) ? language : 'fi'
end

#translateString

Translates self to the desired language. \ Sample usage: hello = ‘hello’\ hello.to_language = ‘fi’ moi = hello.translate pp moi # ‘Hei’

Returns:

  • (String)

    a new and shiny translated string!



29
30
31
# File 'lib/translate_self/translation.rb', line 29

def translate
  call_deepl(language, to_language)
end

#translate!String

Replaces self with the translation. \ Sample usage: hello = ‘hello’\ hello.to_language = ‘fi’ hello.translate! pp hello # ‘Hei’

Returns:

  • (String)

    self replaced with the new translation



41
42
43
44
# File 'lib/translate_self/translation.rb', line 41

def translate!
  defroster if needs_defrosting?
  replace translate
end