Class: YARD::I18n::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/i18n/text.rb

Overview

Provides some convenient features for translating a text.

Since:

  • 0.8.0

Instance Method Summary collapse

Constructor Details

#initialize(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



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

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

Instance Method Details

#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



35
36
37
38
39
40
41
42
43
44
45
46
# 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

#translate(locale) ⇒ String

Translates into locale.

Parameters:

  • locale (Locale)

    the translation target locale.

Returns:

  • (String)

    translated text.

Since:

  • 0.8.0



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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