123456789_123456789_123456789_123456789_123456789_

Class: YARD::I18n::Text

Relationships & Source Files
Inherits: Object
Defined in: lib/yard/i18n/text.rb

Overview

Provides some convenient features for translating a text.

Since:

  • 0.8.0

Class Method Summary

Instance Method Summary

Constructor Details

.new(input, options = {}) ⇒ Text

Creates a text object that has translation related features for the input text.

Parameters:

  • input (#each_line)

    a text to be translated.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :have_header (Boolean) — default: false

    whether the input text has header or not.

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/i18n/text.rb', line 12

def initialize(input, options = {})
  @input = input
  @options = options
end

Instance Method Details

#emit_attribute_event(match_data, line_no) {|part| ... } (private)

Yields:

  • (part)

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/i18n/text.rb', line 134

def emit_attribute_event(match_data, line_no)
  part = {
    :type => :attribute,
    :prefix => match_data[1],
    :name => match_data[2],
    :infix => match_data[3],
    :value => match_data[4],
    :suffix => match_data[5],
    :line_no => line_no
  }
  yield(part)
end

#emit_empty_line_event(line, line_no) {|part| ... } (private)

Yields:

  • (part)

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/i18n/text.rb', line 147

def emit_empty_line_event(line, line_no)
  part = {
    :type => :empty_line,
    :line => line,
    :line_no => line_no
  }
  yield(part)
end

#emit_markup_event(line, line_no) {|part| ... } (private)

Yields:

  • (part)

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/i18n/text.rb', line 125

def emit_markup_event(line, line_no)
  part = {
    :type => :markup,
    :line => line,
    :line_no => line_no
  }
  yield(part)
end

#emit_paragraph_event(paragraph, paragraph_start_line, line_no, &block) (private)

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/i18n/text.rb', line 156

def emit_paragraph_event(paragraph, paragraph_start_line, line_no, &block)
  paragraph_part = {
    :type => :paragraph,
    :line_no => paragraph_start_line
  }
  match_data = /(\s*)\z/.match(paragraph)
  if match_data
    paragraph_part[:paragraph] = match_data.pre_match
    yield(paragraph_part)
    emit_empty_line_event(match_data[1], line_no, &block)
  else
    paragraph_part[:paragraph] = paragraph
    yield(paragraph_part)
  end
end

#extract_messages {|:attribute, name, value, line_no| ... } ⇒ void

This method returns an undefined value.

Extracts translation target messages from @input.

Yields:

  • (:attribute, name, value, line_no)

    the block that receives extracted an attribute in header. It may called many times.

  • (:paragraph, text, start_line_no)

    the block that receives extracted a paragraph in body. Paragraph is a text block separated by one or more empty lines. Empty line is a line that contains only zero or more whitespaces. It may called many times.

Yield Parameters:

  • name (String)

    the name of extracted attribute.

  • value (String)

    the value of extracted attribute.

  • line_no (Integer)

    the defined line number of extracted attribute.

  • text (String)

    the text of extracted paragraph.

  • start_line_no (Integer)

    the start line number of extracted paragraph.

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/i18n/text.rb', line 35

def extract_messages
  parse do |part|
    case part[:type]
    when :markup, :empty_line
      # ignore
    when :attribute
      yield(:attribute, part[:name], part[:value], part[:line_no])
    when :paragraph
      yield(:paragraph, part[:paragraph], part[:line_no])
    end
  end
end

#parse(&block) (private)

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/i18n/text.rb', line 76

def parse(&block)
  paragraph = String.new("")
  paragraph_start_line = 0
  line_no = 0
  in_header = @options[:have_header]

  @input.each_line do |line|
    line_no += 1
    if in_header
      case line
      when /^#!\S+\s*$/
        if line_no == 1
          emit_markup_event(line, line_no, &block)
        else
          in_header = false
        end
      when /^(\s*#\s*@)(\S+)(\s*)(.+?)(\s*)$/
        emit_attribute_event(Regexp.last_match, line_no, &block)
      else
        in_header = false
        if line.strip.empty?
          emit_empty_line_event(line, line_no, &block)
          next
        end
      end
      next if in_header
    end

    case line
    when /^\s*$/
      if paragraph.empty?
        emit_empty_line_event(line, line_no, &block)
      else
        paragraph << line
        emit_paragraph_event(paragraph, paragraph_start_line, line_no,
                             &block)
        paragraph = String.new("")
      end
    else
      paragraph_start_line = line_no if paragraph.empty?
      paragraph << line
    end
  end

  unless paragraph.empty?
    emit_paragraph_event(paragraph, paragraph_start_line, line_no, &block)
  end
end

#translate(locale) ⇒ String

Translates into locale.

Parameters:

  • locale (Locale)

    the translation target locale.

Returns:

  • (String)

    translated text.

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/i18n/text.rb', line 52

def translate(locale)
  translated_text = String.new("")
  parse do |part|
    case part[:type]
    when :markup
      translated_text << part[:line]
    when :attribute
      prefix = "#{part[:prefix]}#{part[:name]}#{part[:infix]}"
      value = locale.translate(part[:value])
      suffix = part[:suffix]
      translated_text << "#{prefix}#{value}#{suffix}"
    when :paragraph
      translated_text << locale.translate(part[:paragraph])
    when :empty_line
      translated_text << part[:line]
    else
      raise "should not reach here: unexpected type: #{type}"
    end
  end
  translated_text
end