Class: Twine::Processors::OutputProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/twine/output_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(twine_file, options) ⇒ OutputProcessor

Returns a new instance of OutputProcessor.



5
6
7
8
# File 'lib/twine/output_processor.rb', line 5

def initialize(twine_file, options)
  @twine_file = twine_file
  @options = options
end

Instance Method Details

#default_languageObject



10
11
12
# File 'lib/twine/output_processor.rb', line 10

def default_language
  @options[:developer_language] || @twine_file.language_codes[0]
end

#fallback_languages(language) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/twine/output_processor.rb', line 14

def fallback_languages(language)
  fallback_mapping = {
    'zh-TW' => 'zh-Hant' # if we don't have a zh-TW translation, try zh-Hant before en
  }

  [fallback_mapping[language], default_language].flatten.compact
end

#process(language) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/twine/output_processor.rb', line 22

def process(language)
  result = TwineFile.new

  result.language_codes.concat @twine_file.language_codes
  @twine_file.sections.each do |section|
    new_section = TwineSection.new section.name

    section.definitions.each do |definition|
      next unless definition.matches_tags?(@options[:tags], @options[:untagged])

      value = definition.translation_for_lang(language)

      next if value && @options[:include] == :untranslated

      if value.nil? && @options[:include] != :translated
        value = definition.translation_for_lang(fallback_languages(language))
      end

      next unless value

      new_definition = definition.dup
      new_definition.translations[language] = value

      new_section.definitions << new_definition
      result.definitions_by_key[new_definition.key] = new_definition
    end

    result.sections << new_section
  end

  return result
end