Class: Konjak::Translator

Inherits:
Object
  • Object
show all
Includes:
Mem
Defined in:
lib/konjak/translator.rb,
lib/konjak/translator/translated_string.rb

Defined Under Namespace

Classes: TranslatedString

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tmx, src_lang, target_lang, **options) ⇒ Translator

Returns a new instance of Translator.



12
13
14
15
16
17
# File 'lib/konjak/translator.rb', line 12

def initialize(tmx, src_lang, target_lang, **options)
  @tmx         = tmx
  @src_lang    = src_lang
  @target_lang = target_lang
  @options     = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/konjak/translator.rb', line 10

def options
  @options
end

#src_langObject (readonly)

Returns the value of attribute src_lang.



10
11
12
# File 'lib/konjak/translator.rb', line 10

def src_lang
  @src_lang
end

#target_langObject (readonly)

Returns the value of attribute target_lang.



10
11
12
# File 'lib/konjak/translator.rb', line 10

def target_lang
  @target_lang
end

#tmxObject (readonly)

Returns the value of attribute tmx.



10
11
12
# File 'lib/konjak/translator.rb', line 10

def tmx
  @tmx
end

Instance Method Details

#translate(content) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/konjak/translator.rb', line 19

def translate(content)
  segmentor(content).segments.map do |text|
    next text unless text.is_a?(TmxSegmentor::SegmentString)
    source_segment = text.segment
    target_segment = source_segment.translation_unit.variant(target_lang).segment

    if format == :gtt_html
      gtt_tags          = source_segment.extract_gtt_tags_from(text)
      translated_string = target_segment.interpolate_gtt_tags(gtt_tags)
      TranslatedString.new(translated_string, text)
    else
      TranslatedString.new(target_segment.text, text)
    end
  end
end