class RDoc::I18n::Text

一个支持 i18n 的文本。

此对象提供以下两个功能

* Extracts translation messages from wrapped raw text.
* Translates wrapped raw text in specified locale.

包装的原始文本可以是 String、RDoc::Comment 或它们的数组。

公共类方法

new(raw) 点击切换源代码

raw 文本创建一个新的支持 i18n 的文本。

# File rdoc/i18n/text.rb, line 17
def initialize(raw)
  @raw = raw
end

公共实例方法

extract_messages() { |part| ... } 点击切换源代码

提取翻译目标消息并产生每个消息。

每个产生的消息都是一个哈希。它由以下部分组成

:type

:paragraph

:paragraph

String (翻译目标消息本身。)

:line_no

Integer ( :paragraph 开始的行号。)

以上内容可能会在未来添加。

# File rdoc/i18n/text.rb, line 32
def extract_messages
  parse do |part|
    case part[:type]
    when :empty_line
      # ignore
    when :paragraph
      yield(part)
    end
  end
end
translate(locale) 点击切换源代码

将原始文本翻译成 locale

# File rdoc/i18n/text.rb, line 44
def translate(locale)
  translated_text = ''
  parse do |part|
    case part[:type]
    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

私有实例方法

each_line(raw, &block) 点击切换源代码
# File rdoc/i18n/text.rb, line 88
def each_line(raw, &block)
  case raw
  when RDoc::Comment
    raw.text.each_line(&block)
  when Array
    raw.each do |comment, location|
      each_line(comment, &block)
    end
  else
    raw.each_line(&block)
  end
end
emit_empty_line_event(line, line_no) { |part| ... } 点击切换源代码
# File rdoc/i18n/text.rb, line 101
def emit_empty_line_event(line, line_no)
  part = {
    :type => :empty_line,
    :line => line,
    :line_no => line_no,
  }
  yield(part)
end
emit_paragraph_event(paragraph, paragraph_start_line, line_no) { |paragraph_part| ... } 点击切换源代码
# File rdoc/i18n/text.rb, line 110
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
parse(&block) 点击切换源代码
# File rdoc/i18n/text.rb, line 60
def parse(&block)
  paragraph = ''
  paragraph_start_line = 0
  line_no = 0

  each_line(@raw) do |line|
    line_no += 1
    case line
    when /\A\s*\z/
      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 = ''
      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