Class: GetText::PO

### Home Classes Methods

### In Files

- lib/gettext/po.rb

### Parent

Object

### Namespace

- CLASS GetText::PO::NonExistentEntryError

### Methods

- ::new
- #[]
- #[]=
- #each
- #empty?
- #has_key?
- #set_comment
- #to_s

### Included Modules

- Enumerable

### 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::Formatter
- GetText::POEntry::InvalidTypeError
- GetText::POEntry::NoMsgctxtError
- GetText::POEntry::NoMsgidError
- GetText::POEntry::NoMsgidPluralError
- GetText::POEntryForRubyParser
- GetText::POFormat
- GetText::POParser
- GetText::ParseError
- GetText::RubyParser
- GetText::TextDomain
- GetText::TextDomainGroup
- GetText::TextDomainManager
- GetText::Tools
- GetText::Tools::MsgCat
- GetText::Tools::MsgCat::Config
- GetText::Tools::MsgCat::Merger
- GetText::Tools::MsgInit
- GetText::Tools::MsgInit::ArgumentError
- GetText::Tools::MsgInit::Error
- GetText::Tools::MsgInit::ValidationError
- GetText::Tools::MsgMerge
- GetText::Tools::MsgMerge::Config
- GetText::Tools::MsgMerge::FuzzyEntryFinder
- GetText::Tools::MsgMerge::Merger
- GetText::Tools::Task
- GetText::Tools::Task::Error
- GetText::Tools::Task::Path
- GetText::Tools::Task::ValidationError
- GetText::Tools::XGetText

No matching classes.

# GetText::PO

PO stores PO entries like Hash. Each key of {POEntry} is msgctxt and msgid. PO[msgctxt, msgid] returns the {POEntry} containing msgctxt and msgid. If you specify msgid only, msgctxt is treated as nonexistent.

@since 2.3.4

### Attributes

order[RW]

@!attribute [rw] order

    The order is used to sort PO entries(objects of {POEntry}) in
    {#to_s}.
    @param [:reference, :msgid] order (:reference) The sort key.

      Use `:reference` for sorting by location that message is placed.

      Use `:msgid` for sorting by msgid alphabetical order.

      `:references` is deprecated since 3.0.4. It will be removed
      at 4.0.0. Use `:reference` instead.

    @return [Symbol] the name as order by sort.

### Public Class Methods

new(order=nil) click to toggle source

    # File lib/gettext/po.rb, line 52
    def initialize(order=nil)
      @order = order || :reference
      @entries = {}
    end

### Public Instance Methods

[](msgctxt, msgid=nil) click to toggle source

Returns {POEntry} containing msgctxt and msgid. If you specify one argument, it is treated as msgid. @overload [](msgid)

    @!macro [new] po.[].argument
      @param [String] msgid msgid contained returning {POEntry}.
      @return [POEntry]
    @!macro po.[].argument

@overload [](msgctxt, msgid)

    @param [String] msgctxt msgctxt contained returning {POEntry}.
    @!macro po.[].argument

    # File lib/gettext/po.rb, line 67
    def [](msgctxt, msgid=nil)
      if msgid.nil?
        msgid = msgctxt
        msgctxt = nil
      end

      @entries[[msgctxt, msgid]]
    end

[]=(*arguments) click to toggle source

Stores {POEntry} or msgstr binding msgctxt and msgid. If you specify msgstr, this method creates {POEntry} containing it. If you specify the two argument, the first argument is treated as msgid.

@overload []=(msgid, po_entry)

    @!macro [new] po.store.entry.arguments
      @param [String] msgid msgid binded {POEntry}.
      @param [POEntry] po_entry stored {POEntry}.
    @!macro po.store.entry.arguments

@overload []=(msgctxt, msgid, po_entry)

    @param [String] msgctxt msgctxt binded {POEntry}.
    @!macro po.store.entry.arguments

@overload []=(msgid, msgstr)

    @!macro [new] po.store.msgstr.arguments
      @param [String] msgid msgid binded {POEntry}.
      @param [String] msgstr msgstr contained {POEntry} stored PO.
        This {POEntry} is generated in this method.
    @!macro po.store.msgstr.arguments

@overload []=(msgctxt, msgid, msgstr)

    @param [String] msgctxt msgctxt binded {POEntry}.
    @!macro po.store.msgstr.arguments

    # File lib/gettext/po.rb, line 98
    def []=(*arguments)
      case arguments.size
      when 2
        msgctxt = nil
        msgid = arguments[0]
        value = arguments[1]
      when 3
        msgctxt = arguments[0]
        msgid = arguments[1]
        value = arguments[2]
      else
        raise(ArgumentError,
              "[]=: wrong number of arguments(#{arguments.size} for 2..3)")
      end

      id = [msgctxt, msgid]
      if value.instance_of?(POEntry)
        @entries[id] = value
        return(value)
      end

      msgstr = value
      if @entries.has_key?(id)
        entry = @entries[id]
      else
        if msgctxt.nil?
          entry = POEntry.new(:normal)
        else
          entry = POEntry.new(:msgctxt)
        end
        @entries[id] = entry
      end
      entry.msgctxt = msgctxt
      entry.msgid = msgid
      entry.msgstr = msgstr
      entry
    end

each(&block) click to toggle source

Calls block once for each {POEntry} as a block parameter. @overload each(&block)

    @yield [entry]
    @yieldparam [POEntry] entry {POEntry} in PO.

@overload each

    @return [Enumerator] Returns Enumerator for {POEntry}.

    # File lib/gettext/po.rb, line 172
    def each(&block)
      @entries.each_value(&block)
    end

empty?() click to toggle source

@return [Bool] `true` if there is no entry, `false` otherwise.

    # File lib/gettext/po.rb, line 177
    def empty?
      @entries.empty?
    end

has_key?(*arguments) click to toggle source

Returns if PO stores {POEntry} containing msgctxt and msgid. If you specify one argument, it is treated as msgid and msgctxt is nil.

@overload has_key?(msgid)

    @!macro [new] po.has_key?.arguments
      @param [String] msgid msgid contained {POEntry} checked if it be
        stored PO.
    @!macro po.has_key?.arguments

@overload has_key?(msgctxt, msgid)

    @param [String] msgctxt msgctxt contained {POEntry} checked if
      it be stored PO.
    @!macro po.has_key?.arguments

    # File lib/gettext/po.rb, line 149
    def has_key?(*arguments)
      case arguments.size
      when 1
        msgctxt = nil
        msgid = arguments[0]
      when 2
        msgctxt = arguments[0]
        msgid = arguments[1]
      else
        message = "has_key?: wrong number of arguments " +
                    "(#{arguments.size} for 1..2)"
        raise(ArgumentError, message)
      end
      id = [msgctxt, msgid]
      @entries.has_key?(id)
    end

set_comment(msgid, comment, msgctxt=nil) click to toggle source

For {PoParer}.

    # File lib/gettext/po.rb, line 182
    def set_comment(msgid, comment, msgctxt=nil)
      id = [msgctxt, msgid]
      self[*id] = nil unless @entries.has_key?(id)
      self[*id].comment = comment
    end

to_s(options={}) click to toggle source

Formats each {POEntry} to the format of PO files and returns joined them. @see www.gnu.org/software/gettext/manual/html_node/PO-Files.html#PO-Files

    The description for Format of PO in GNU gettext manual

@param (see POEntry#to_s) @return [String] Formatted and joined PO entries. It is used for

    creating .po files.

    # File lib/gettext/po.rb, line 195
    def to_s(options={})
      po_string = ""

      header_entry = @entries[[nil, ""]]
      unless header_entry.nil?
        po_string << header_entry.to_s(options.merge(:max_line_width => nil))
      end

      content_entries = @entries.reject do |(_, msgid), _|
        msgid == :last or msgid.empty?
      end

      sort(content_entries).each do |msgid, entry|
        po_string << "\n" unless po_string.empty?
        po_string << entry.to_s(options)
      end

      if @entries.has_key?([nil, :last])
        po_string << "\n" unless po_string.empty?
        po_string << @entries[[nil, :last]].to_s(options)
      end

      po_string
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.