Class: Twine::Processors::OutputProcessor

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

Instance Method Summary collapse

Constructor Details

#initialize(strings, options) ⇒ OutputProcessor

Returns a new instance of OutputProcessor.



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

def initialize(strings, options)
  @strings = strings
  @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] || @strings.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 = StringsFile.new

  result.language_codes.concat @strings.language_codes
  @strings.sections.each do |section|
    new_section = StringsSection.new section.name

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

      value = row.translated_string_for_lang(language)

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

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

      next unless value

      new_row = row.dup
      new_row.translations[language] = value

      new_section.rows << new_row
      result.strings_map[new_row.key] = new_row
    end

    result.sections << new_section
  end

  return result
end