Class: GetText::POEntry

### Home Classes Methods

### In Files

- lib/gettext/tools/parser/ruby.rb
- lib/gettext/po_entry.rb

### Parent

Object

### Namespace

- CLASS GetText::POEntry::InvalidTypeError
- CLASS GetText::POEntry::NoMsgctxtError
- CLASS GetText::POEntry::NoMsgidError
- CLASS GetText::POEntry::NoMsgidPluralError

### Methods

- ::escape
- ::max_line_length
- ::max_line_length=
- ::new
- ::new_from_ary
- #==
- #[]
- #add_comment
- #escaped
- #format_comment
- #format_extracted_comment
- #format_flag_comment
- #format_message
- #format_obsolete_comment
- #format_previous_comment
- #format_reference_comment
- #format_translator_comment
- #initialize_old
- #merge
- #mergeable?
- #msgctxt?
- #plural?
- #to_s
- #type=

### Included Modules

- GetText::POEntryForRubyParser

### Class/Module Index

Quicksearch

- GetText
- GetText::ClassInfo
- GetText::ErbParser
- GetText::GladeParser
- GetText::LocalePath
- GetText::MO
- GetText::MO::HeaderRev1
- GetText::MO::InvalidFormat
- GetText::NoboundTextDomainError
- GetText::PO
- GetText::PO::NonExistentEntryError
- GetText::POEntry
- GetText::POEntry::InvalidTypeError
- GetText::POEntry::NoMsgctxtError
- GetText::POEntry::NoMsgidError
- GetText::POEntry::NoMsgidPluralError
- GetText::POEntryForRubyParser
- GetText::POParser
- GetText::ParseError
- GetText::RubyParser
- GetText::TextDomain
- GetText::TextDomainGroup
- GetText::TextDomainManager
- GetText::Tools
- GetText::Tools::MsgInit
- GetText::Tools::MsgInit::ArgumentError
- GetText::Tools::MsgInit::Error
- GetText::Tools::MsgInit::ValidationError
- GetText::Tools::MsgMerge
- GetText::Tools::Task
- GetText::Tools::XGetText

No matching classes.

# GetText::POEntry

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

### Constants

EXTRACTED_COMMENT_MARK
FLAG_MARK
PARAMS
PREVIOUS_COMMENT_MARK
REFERENCE_COMMENT_MARK
TRANSLATOR_COMMENT_MARK

### 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/po_entry.rb, line 54
    def escape(string)
      string.gsub(/([\\"\t\n])/) do
        special_character = $1
        case special_character
        when "\t"
          "\\t"
        when "\n"
          "\\n"
        else
          "\\#{special_character}"
        end
      end
    end

max_line_length() click to toggle source

Gets the max line length.

    # File lib/gettext/po_entry.rb, line 77
    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/po_entry.rb, line 72
    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 104
    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/po_entry.rb, line 344
    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/po_entry.rb, line 128
    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/po_entry.rb, line 374
    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/po_entry.rb, line 112
    def add_comment(new_comment)
      if (new_comment and ! new_comment.empty?)
        @extracted_comment ||= ""
        @extracted_comment << "\n" unless @extracted_comment.empty?
        @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/po_entry.rb, line 123
    def escaped(param_name)
      escape(send(param_name))
    end

format_comment(mark, comment) click to toggle source

    # File lib/gettext/po_entry.rb, line 273
    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/po_entry.rb, line 239
    def format_extracted_comment
      format_comment(EXTRACTED_COMMENT_MARK, extracted_comment)
    end

format_flag_comment() click to toggle source

    # File lib/gettext/po_entry.rb, line 265
    def format_flag_comment
      format_comment(FLAG_MARK, flag)
    end

format_message(message) click to toggle source

    # File lib/gettext/po_entry.rb, line 304
    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/po_entry.rb, line 287
    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/po_entry.rb, line 269
    def format_previous_comment
      format_comment(PREVIOUS_COMMENT_MARK, previous)
    end

format_reference_comment() click to toggle source

    # File lib/gettext/po_entry.rb, line 243
    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/po_entry.rb, line 235
    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/po_entry.rb, line 161
    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/po_entry.rb, line 154
    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/po_entry.rb, line 318
    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/po_entry.rb, line 323
    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/po_entry.rb, line 182
    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/po_entry.rb, line 144
    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.