class Twine::Processors::OutputProcessor

Public Class Methods

new(twine_file, options) click to toggle source
# File lib/twine/output_processor.rb, line 5
def initialize(twine_file, options)
  @twine_file = twine_file
  @options = options
end

Public Instance Methods

default_language() click to toggle source
# File lib/twine/output_processor.rb, line 10
def default_language
  @options[:developer_language] || @twine_file.language_codes[0]
end
fallback_languages(language) click to toggle source
# 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) click to toggle source
# 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