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.
Class Method Summary
-
.new(input, options = {}) ⇒ Text
constructor
Creates a text object that has translation related features for the input text.
Instance Method Summary
-
#extract_messages {|:attribute, name, value, line_no| ... } ⇒ void
Extracts translation target messages from
@input
. -
#translate(locale) ⇒ String
Translates into
locale
. - #emit_attribute_event(match_data, line_no) {|part| ... } private
- #emit_empty_line_event(line, line_no) {|part| ... } private
- #emit_markup_event(line, line_no) {|part| ... } private
- #emit_paragraph_event(paragraph, paragraph_start_line, line_no, &block) private
- #parse(&block) private
Constructor Details
.new(input, options = {}) ⇒ Text
Creates a text object that has translation related features for the input text.
# File 'lib/yard/i18n/text.rb', line 12
def initialize(input, = {}) @input = input @options = end
Instance Method Details
#emit_attribute_event(match_data, line_no) {|part| ... } (private)
# 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)
# 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)
# 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)
# 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
.
# File 'lib/yard/i18n/text.rb', line 35
def 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)
# 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
.
# 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