Parent

GetText::POEntry

Contains data related to the expression or sentence that is to be translated.

Attributes

comment[RW]
extracted_comment[RW]
flag[RW]
msgctxt[RW]
msgid[RW]
msgid_plural[RW]

Options

msgstr[RW]
previous[RW]
references[RW]
separator[RW]
translator_comment[RW]
type[R]

Required

Public Class Methods

escape(string) click to toggle source
# File lib/gettext/tools/po_entry.rb, line 54
def escape(string)
  string.gsub(/([\\"\n])/) do
    special_character = $1
    if special_character == "\n"
      "\\n"
    else
      "\\#{special_character}"
    end
  end
end
max_line_length() click to toggle source

Gets the max line length.

# File lib/gettext/tools/po_entry.rb, line 74
def self.max_line_length
  @@max_line_length
end
max_line_length=(len) click to toggle source

Sets the max line length.

# File lib/gettext/tools/po_entry.rb, line 69
def self.max_line_length=(len)
  @@max_line_length = len
end
new(type) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 103
def initialize(type)
  initialize_old(type)
  init_param
end
Also aliased as: initialize_old
new_from_ary(ary) click to toggle source

For backward comatibility. This doesn't support "comment". ary = [msgid1, "file1:line1", "file2:line"]

# File lib/gettext/tools/po_entry.rb, line 340
def self.new_from_ary(ary)
  ary = ary.dup
  msgid = ary.shift
  references = ary
  type = :normal
  msgctxt = nil
  msgid_plural = nil

  if msgid.include? "\0004"
    msgctxt, msgid = msgid.split(/\0004/)
    type = :msgctxt
  end
  if msgid.include? "\0000"
    ids = msgid.split(/\0000/)
    msgid = ids[0]
    msgid_plural = ids[1]
    if type == :msgctxt
      type = :msgctxt_plural
    else
      type = :plural
    end
  end
  ret = self.new(type)
  ret.msgid = msgid
  ret.references = references
  ret.msgctxt = msgctxt
  ret.msgid_plural = msgid_plural
  ret
end

Public Instance Methods

==(other) click to toggle source

Checks if the self has same attributes as other.

# File lib/gettext/tools/po_entry.rb, line 124
def ==(other)
  not other.nil? and
    type == other.type and
    msgid == other.msgid and
    msgstr == other.msgstr and
    msgid_plural == other.msgid_plural and
    separator == other.separator and
    msgctxt == other.msgctxt and
    translator_comment == other.translator_comment and
    extracted_comment == other.extracted_comment and
    references == other.references and
    flag == other.flag and
    previous == other.previous and
    comment == other.comment
end
[](number) click to toggle source
# File lib/gettext/tools/po_entry.rb, line 370
def [](number)
  param = @param_type[number]
  raise ParseError, 'no more string parameters expected' unless param
  send param
end
add_comment(new_comment) click to toggle source

Support for extracted comments. Explanation s. www.gnu.org/software/gettext/manual/gettext.html#Names

# File lib/gettext/tools/po_entry.rb, line 109
def add_comment(new_comment)
  if (new_comment and ! new_comment.empty?)
    @extracted_comment ||= ""
    @extracted_comment += new_comment
  end
  to_s
end
escaped(param_name) click to toggle source

Returns a parameter representation suitable for po-files and other purposes.

# File lib/gettext/tools/po_entry.rb, line 119
def escaped(param_name)
  escape(send(param_name))
end
format_comment(mark, comment) click to toggle source
# File lib/gettext/tools/po_entry.rb, line 269
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_extracted_comment() click to toggle source
# File lib/gettext/tools/po_entry.rb, line 235
def format_extracted_comment
  format_comment(EXTRACTED_COMMENT_MARK, extracted_comment)
end
format_flag_comment() click to toggle source
# File lib/gettext/tools/po_entry.rb, line 261
def format_flag_comment
  format_comment(FLAG_MARK, flag)
end
format_message(message) click to toggle source
# File lib/gettext/tools/po_entry.rb, line 300
def format_message(message)
  formatted_message = ""
  if not message.nil? and message.include?("\n")
    formatted_message << "\"\"\n"
    message.each_line.each do |line|
      formatted_message << "\"#{escape(line)}\"\n"
    end
  else
    formatted_message << "\"#{escape(message)}\"\n"
  end
  formatted_message
end
format_obsolete_comment(comment) click to toggle source
# File lib/gettext/tools/po_entry.rb, line 283
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
    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/tools/po_entry.rb, line 265
def format_previous_comment
  format_comment(PREVIOUS_COMMENT_MARK, previous)
end
format_reference_comment() click to toggle source
# File lib/gettext/tools/po_entry.rb, line 239
def format_reference_comment
  max_line_length = 70
  formatted_reference = ""
  if not references.nil? and not references.empty?
    formatted_reference << REFERENCE_COMMENT_MARK
    line_size = 2
    references.each do |reference|
      if line_size + reference.size > max_line_length
        formatted_reference << "\n"
        formatted_reference <<  "#{REFERENCE_COMMENT_MARK} #{reference}"
        line_size = 3 + reference.size
      else
        formatted_reference << " #{reference}"
        line_size += 1 + reference.size
      end
    end

    formatted_reference << "\n"
  end
  formatted_reference
end
format_translator_comment() click to toggle source
# File lib/gettext/tools/po_entry.rb, line 231
def format_translator_comment
  format_comment("#", translator_comment)
end
initialize_old(type) click to toggle source
Alias for: new
merge(other) click to toggle source

Merges two translation targets with the same msgid and returns the merged result. If one is declared as plural and the other not, then the one with the plural wins.

# File lib/gettext/tools/po_entry.rb, line 157
def merge(other)
  return self unless other
  raise ParseError, "Translation targets do not match: \n"        "  self: #{self.inspect}\n  other: '#{other.inspect}'" unless self.mergeable?(other)
  if other.msgid_plural && !self.msgid_plural
    res = other
    unless (res.references.include? self.references[0])
      res.references += self.references
      res.add_comment(self.extracted_comment)
    end
  else
    res = self
    unless (res.references.include? other.references[0])
      res.references += other.references
      res.add_comment(other.extracted_comment)
    end
  end
  res
end
mergeable?(other) click to toggle source

Checks if the other translation target is mergeable with the current one. Relevant are msgid and translation context (msgctxt).

# File lib/gettext/tools/po_entry.rb, line 150
def mergeable?(other)
  other && other.msgid == self.msgid && other.msgctxt == self.msgctxt
end
msgctxt?() click to toggle source

Returns true if the type is kind of msgctxt.

# File lib/gettext/tools/po_entry.rb, line 314
def msgctxt?
  [:msgctxt, :msgctxt_plural].include?(@type)
end
plural?() click to toggle source

Returns true if the type is kind of plural.

# File lib/gettext/tools/po_entry.rb, line 319
def plural?
  [:plural, :msgctxt_plural].include?(@type)
end
to_s() click to toggle source

Output the po entry for the po-file.

# File lib/gettext/tools/po_entry.rb, line 178
def to_s
  raise(NoMsgidError, "msgid is nil.") unless @msgid

  str = ""
  # extracted comments
  if @msgid == :last
    return format_obsolete_comment(comment)
  end

  str << format_translator_comment
  str << format_extracted_comment
  str << format_reference_comment
  str << format_flag_comment
  str << format_previous_comment

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

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

    str << "msgid_plural " << format_message(msgid_plural)

    if msgstr.nil?
      str << "msgstr[0] \"\"\n"
      str << "msgstr[1] \"\"\n"
    else
      msgstrs = msgstr.split("\0000", -1)
      msgstrs.each_with_index do |msgstr, index|
        str << "msgstr[#{index}] " << format_message(msgstr)
      end
    end
  else
    str << "msgstr "
    str << format_message(msgstr)
  end
  str
end
type=(type) click to toggle source
# File lib/gettext/tools/po_entry.rb, line 140
def type=(type)
  unless PARAMS.has_key?(type)
    raise(InvalidTypeError, "\"%s\" is invalid type." % type)
  end
  @type = type
  @param_type = PARAMS[@type]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.