Parent

Class/Module Index [+]

Quicksearch

YARD::I18n::Text

Provides some convenient features for translating a text.

Public Class Methods

new(input, options={}) click to toggle source

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

@param [each_line] input a text to be translated. @option options [Boolean] :have_header (false) whether the

input text has header or not.
# File lib/yard/i18n/text.rb, line 11
def initialize(input, options={})
  @input = input
  @options = options
end

Public Instance Methods

extract_messages() click to toggle source

Extracts translation target messages from +@input+.

@yield [:attribute, name, value, line_no] the block that

recieves extracted an attribute in header. It may called many
times.

@yieldparam [String] name the name of extracted attribute. @yieldparam [String] value the value of extracted attribute. @yieldparam [Integer] line_no the defined line number of extracted

attribute.

@yield [:paragraph, text, start_line_no] the block that

recieves 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.

@yieldparam [String] text the text of extracted paragraph. @yieldparam [Integer] start_line_no the start line number of

extracted paragraph.

@return [void]

# File lib/yard/i18n/text.rb, line 34
def extract_messages
  parse do |part|
    line_no = part[:line_no]
    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) click to toggle source

Translates into locale.

@param [Locale] locale the translation target locale. @return [String] translated text.

# File lib/yard/i18n/text.rb, line 52
def translate(locale)
  translated_text = ""
  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

[Validate]

Generated with the Darkfish Rdoc Generator 2.