Class: Any2Tmx::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/any2tmx/transformer.rb

Constant Summary collapse

EXTRACTOR_EXTENSIONS =
{
  '.yml'  => 'yaml/rails',
  '.json' => 'json/key-value',
  '.xml'  => 'android/xml',
  '.txt'  => 'txt/lines'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Transformer

Returns a new instance of Transformer.



14
15
16
# File 'lib/any2tmx/transformer.rb', line 14

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/any2tmx/transformer.rb', line 12

def options
  @options
end

Instance Method Details

#transformObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/any2tmx/transformer.rb', line 18

def transform
  source_phrases = extract_from(options.source[:file])
  result = source_phrases.each_with_object({}) do |(k, v), result|
    result[k] = { options.source[:locale] => v.to_s }
  end

  options.targets.each do |target|
    target_phrases = extract_from(target[:file])

    source_phrases.each_pair do |source_key, _|
      translation = target_phrases[source_key]

      if !translation.nil?
        result[source_key][target[:locale]] = translation.to_s
      end
    end
  end

  result
end