class GetText::PoMessage

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

Constants

PARAMS

Attributes

comment[RW]
msgctxt[RW]
msgid[RW]
msgid_plural[RW]

Options

sources[RW]
type[RW]

Required

Public Class Methods

max_line_length() click to toggle source

Gets the max line length.

# File lib/gettext/tools/pomessage.rb, line 24
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/pomessage.rb, line 19
def self.max_line_length=(len)
  @@max_line_length = len
end
new(type) click to toggle source

Create the object. type should be :normal, :plural, :msgctxt or :msgctxt_plural.

# File lib/gettext/tools/pomessage.rb, line 38
def initialize(type)
  @type = type
  @sources = []
  @param_type = PARAMS[@type]
end
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/pomessage.rb, line 161
def self.new_from_ary(ary)
  ary = ary.dup
  msgid = ary.shift
  sources = ary
  type = :normal
  msgctxt = nil
  msgid_plural = nil

  if msgid.include? "\0004"
    msgctxt, msgid = msgid.split(%r\0004/)
    type = :msgctxt
  end
  if msgid.include? "\0000"
    ids = msgid.split(%r\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.sources = sources
  ret.msgctxt = msgctxt
  ret.msgid_plural = msgid_plural
  ret
end

Public Instance Methods

==(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/pomessage.rb, line 63
def ==(other)
  other && other.msgid == self.msgid && other.msgctxt == self.msgctxt
end
[](number) click to toggle source
# File lib/gettext/tools/pomessage.rb, line 191
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/pomessage.rb, line 46
def add_comment(new_comment)
  if (new_comment and ! new_comment.empty?)
    @comment ||= ""
    @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/pomessage.rb, line 56
def escaped(param_name)
  orig = self.send param_name
  orig.gsub(%r"/, '\"').gsub(%r\r/, '')
end
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/pomessage.rb, line 70
def merge(other)
  return self unless other
  raise ParseError, "Translation targets do not match: \n"        "  self: #{self.inspect}\n  other: '#{other.inspect}'" unless self == other
  if other.msgid_plural && !self.msgid_plural
    res = other
    unless (res.sources.include? self.sources[0])
      res.sources += self.sources
      res.add_comment(self.comment)
    end
  else
    res = self
    unless (res.sources.include? other.sources[0])
      res.sources += other.sources
      res.add_comment(other.comment)
    end
  end
  res
end
msgctxt?() click to toggle source

Returns true if the type is kind of msgctxt. And if this is a kind of msgctxt and msgctxt property is nil, then raise an RuntimeException.

# File lib/gettext/tools/pomessage.rb, line 131
def msgctxt?
  if [:msgctxt, :msgctxt_plural].include? @type
    raise "This PoMessage is a kind of msgctxt but the msgctxt property is nil. msgid: #{msgid}" unless @msgctxt
    true
  end
end
plural?() click to toggle source

Returns true if the type is kind of plural. And if this is a kind of plural and #msgid_plural property is nil, then raise an RuntimeException.

# File lib/gettext/tools/pomessage.rb, line 141
def plural?
  if [:plural, :msgctxt_plural].include? @type
    raise "This PoMessage is a kind of plural but the msgid_plural property is nil. msgid: #{msgid}" unless @msgid_plural
    true
  end
end
to_po_str() click to toggle source

Output the po message for the po-file.

# File lib/gettext/tools/pomessage.rb, line 91
def to_po_str
  raise "msgid is nil." unless @msgid
  raise "sources is nil." unless @sources

  str = ""
  # extracted comments
  if comment
    comment.split("\n").each do |comment_line|
      str << "\n#. #{comment_line.strip}"
    end
  end

  # references
  curr_pos = @@max_line_length
  sources.each do |e|
    if curr_pos + e.size > @@max_line_length
      str << "\n#:"
      curr_pos = 3
    else
      curr_pos += (e.size + 1)
    end
    str << " " << e
  end

  # msgctxt, msgid, msgstr
  str << "\nmsgctxt \"" << msgctxt << "\"" if msgctxt?
  str << "\nmsgid \"" << escaped(:msgid) << "\"\n"
  if plural?
    str << "msgid_plural \"" << escaped(:msgid_plural) << "\"\n"
    str << "msgstr[0] \"\"\n"
    str << "msgstr[1] \"\"\n"
  else
    str << "msgstr \"\"\n"
  end
  str
end