class GetText::POEntry::Formatter

Constants

DEFAULT_MAX_LINE_WIDTH

Public Class Methods

escape(string) click to toggle source
# File lib/gettext/po_entry.rb, line 230
def escape(string)
  return "" if string.nil?

  string.gsub(/([\"\t\n])/) do
    special_character = $1
    case special_character
    when "\t"
      "\\t"
    when "\n"
      "\\n"
    else
      "\\#{special_character}"
    end
  end
end
new(entry, options={}) click to toggle source

@param [POEntry] entry The entry to be formatted. @param [Hash] options @option options [Bool] :include_translator_comment (true)

Includes translator comments in formatted string if true.

@option options [Bool] :include_extracted_comment (true)

Includes extracted comments in formatted string if true.

@option options [Bool] :include_reference_comment (true)

Includes reference comments in formatted string if true.

@option options [Bool] :include_flag_comment (true)

Includes flag comments in formatted string if true.

@option options [Bool] :include_previous_comment (true)

Includes previous comments in formatted string if true.

@option options [Bool] :include_all_comments (true)

Includes all comments in formatted string if true.
Other specific `:include_XXX` options get preference over
this option.
You can remove all comments by specifying this option as
false and omitting other `:include_XXX` options.

@option options [Integer] :max_line_width (78)

Wraps long lines that is longer than the `:max_line_width`.
Don't break long lines if `:max_line_width` is less than 0
such as `-1`.

@option options [Encoding] :encoding (nil)

Encodes to the specific encoding.
# File lib/gettext/po_entry.rb, line 275
def initialize(entry, options={})
  @entry = entry
  @options = normalize_options(options)
end

Public Instance Methods

format() click to toggle source
# File lib/gettext/po_entry.rb, line 280
def format
  if @entry.obsolete?
    return format_obsolete_comment(@entry.comment)
  end

  str = format_comments

  # msgctxt, msgid, msgstr
  if @entry.msgctxt?
    if @entry.msgctxt.nil?
      no_msgctxt_message = "This POEntry is a kind of msgctxt " +
                             "but the msgctxt property is nil. " +
                             "msgid: #{@entry.msgid}"
      raise(NoMsgctxtError, no_msgctxt_message)
    end
    str << "msgctxt " << format_message(@entry.msgctxt)
  end

  str << "msgid " << format_message(@entry.msgid)
  if @entry.plural?
    if @entry.msgid_plural.nil?
      no_plural_message = "This POEntry is a kind of plural " +
                            "but the msgid_plural property is nil. " +
                            "msgid: #{@entry.msgid}"
      raise(NoMsgidPluralError, no_plural_message)
    end

    str << "msgid_plural " << format_message(@entry.msgid_plural)

    if @entry.msgstr.nil?
      str << "msgstr[0] \"\"\n"
      str << "msgstr[1] \"\"\n"
    else
      msgstrs = @entry.msgstr.split("\000", -1)
      msgstrs.each_with_index do |msgstr, index|
        str << "msgstr[#{index}] " << format_message(msgstr)
      end
    end
  else
    str << "msgstr "
    str << format_message(@entry.msgstr)
  end

  encode(str)
end

Private Instance Methods

encode(string) click to toggle source
# File lib/gettext/po_entry.rb, line 501
def encode(string)
  encoding = @options[:encoding]
  return string if encoding.nil?
  string.encode(encoding)
end
escape(string) click to toggle source
# File lib/gettext/po_entry.rb, line 478
def escape(string)
  self.class.escape(string)
end
format_comment(mark, comment) click to toggle source
# File lib/gettext/po_entry.rb, line 430
def format_comment(mark, comment)
  return "" if comment.nil?

  formatted_comment = ""
  comment.each_line do |comment_line|
    if comment_line == "\n"
      formatted_comment << "#{mark}\n"
    else
      formatted_comment << "#{mark} #{comment_line.strip}\n"
    end
  end
  formatted_comment
end
format_comments() click to toggle source
# File lib/gettext/po_entry.rb, line 367
def format_comments
  formatted_comment = ""
  if include_translator_comment?
    formatted_comment << format_translator_comment
  end
  if include_extracted_comment?
    formatted_comment << format_extracted_comment
  end
  if include_reference_comment?
    formatted_comment << format_reference_comment
  end
  if include_flag_comment?
    formatted_comment << format_flag_comment
  end
  if include_previous_comment?
    formatted_comment << format_previous_comment
  end
  formatted_comment
end
format_extracted_comment() click to toggle source
# File lib/gettext/po_entry.rb, line 391
def format_extracted_comment
  format_comment(EXTRACTED_COMMENT_MARK, @entry.extracted_comment)
end
format_flag_comment() click to toggle source
# File lib/gettext/po_entry.rb, line 418
def format_flag_comment
  formatted_flags = ""
  @entry.flags.each do |flag|
    formatted_flags << format_comment(FLAG_MARK, flag)
  end
  formatted_flags
end
format_message(message) click to toggle source
# File lib/gettext/po_entry.rb, line 461
def format_message(message)
  empty_formatted_message = "\"\"\n"
  return empty_formatted_message if message.nil?

  chunks = wrap_message(message)
  return empty_formatted_message if chunks.empty?

  formatted_message = ""
  if chunks.size > 1 or chunks.first.end_with?("\n")
    formatted_message << empty_formatted_message
  end
  chunks.each do |chunk|
    formatted_message << "\"#{escape(chunk)}\"\n"
  end
  formatted_message
end
format_obsolete_comment(comment) click to toggle source
# File lib/gettext/po_entry.rb, line 444
def format_obsolete_comment(comment)
  mark = "#~"
  return "" if comment.nil?

  formatted_comment = ""
  comment.each_line do |comment_line|
    if /\A#[^~]/ =~ comment_line or comment_line.start_with?(mark)
      formatted_comment << "#{comment_line.chomp}\n"
    elsif comment_line == "\n"
      formatted_comment << "\n"
    else
      formatted_comment << "#{mark} #{comment_line.strip}\n"
    end
  end
  formatted_comment
end
format_previous_comment() click to toggle source
# File lib/gettext/po_entry.rb, line 426
def format_previous_comment
  format_comment(PREVIOUS_COMMENT_MARK, @entry.previous)
end
format_reference_comment() click to toggle source
# File lib/gettext/po_entry.rb, line 395
def format_reference_comment
  max_line_width = @options[:max_line_width]
  formatted_reference = ""
  if not @entry.references.nil? and not @entry.references.empty?
    formatted_reference << REFERENCE_COMMENT_MARK
    line_width = 2
    @entry.references.each do |reference|
      if max_line_width > 0 and
          line_width + reference.size > max_line_width
        formatted_reference << "\n"
        formatted_reference <<  "#{REFERENCE_COMMENT_MARK} #{reference}"
        line_width = 3 + reference.size
      else
        formatted_reference << " #{reference}"
        line_width += 1 + reference.size
      end
    end

    formatted_reference << "\n"
  end
  formatted_reference
end
format_translator_comment() click to toggle source
# File lib/gettext/po_entry.rb, line 387
def format_translator_comment
  format_comment("#", @entry.translator_comment)
end
include_extracted_comment?() click to toggle source
# File lib/gettext/po_entry.rb, line 351
def include_extracted_comment?
  @options[:include_extracted_comment]
end
include_flag_comment?() click to toggle source
# File lib/gettext/po_entry.rb, line 359
def include_flag_comment?
  @options[:include_flag_comment]
end
include_previous_comment?() click to toggle source
# File lib/gettext/po_entry.rb, line 363
def include_previous_comment?
  @options[:include_previous_comment]
end
include_reference_comment?() click to toggle source
# File lib/gettext/po_entry.rb, line 355
def include_reference_comment?
  @options[:include_reference_comment]
end
include_translator_comment?() click to toggle source
# File lib/gettext/po_entry.rb, line 347
def include_translator_comment?
  @options[:include_translator_comment]
end
normalize_options(options) click to toggle source
# File lib/gettext/po_entry.rb, line 327
def normalize_options(options)
  options = options.dup
  include_comment_keys = [
    :include_translator_comment,
    :include_extracted_comment,
    :include_reference_comment,
    :include_flag_comment,
    :include_previous_comment,
  ]
  if options[:include_all_comments].nil?
    options[:include_all_comments] = true
  end
  default_include_comment_value = options[:include_all_comments]
  include_comment_keys.each do |key|
    options[key] = default_include_comment_value if options[key].nil?
  end
  options[:max_line_width] ||= DEFAULT_MAX_LINE_WIDTH
  options
end
wrap_message(message) click to toggle source
# File lib/gettext/po_entry.rb, line 482
def wrap_message(message)
  return [message] if message.empty?

  max_line_width = @options[:max_line_width]

  chunks = []
  message.each_line do |line|
    if max_line_width <= 0
      chunks << line
    else
      # TODO: use character width instead of the number of characters
      line.scan(/.{1,#{max_line_width}}/m) do |chunk|
        chunks << chunk
      end
    end
  end
  chunks
end