Class: GetText::PO

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gettext/po.rb

Overview

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

Defined Under Namespace

Classes: NonExistentEntryError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order = nil) ⇒ PO

Returns a new instance of PO.

Since:

  • 2.3.4



53
54
55
56
# File 'lib/gettext/po.rb', line 53

def initialize(order=nil)
  @order = order || :reference
  @entries = {}
end

Instance Attribute Details

#orderSymbol

The order is used to sort PO entries(objects of GetText::POEntry) in #to_s.

Parameters:

  • order (:reference, :msgid)

    (: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.

Returns:

  • (Symbol)

    the name as order by sort.



51
52
53
# File 'lib/gettext/po.rb', line 51

def order
  @order
end

Instance Method Details

#[](msgid) ⇒ POEntry #[](msgctxt, msgid) ⇒ POEntry

Returns GetText::POEntry containing msgctxt and msgid. If you specify one argument, it is treated as msgid.

Overloads:

Since:

  • 2.3.4



68
69
70
71
72
73
74
75
# File 'lib/gettext/po.rb', line 68

def [](msgctxt, msgid=nil)
  if msgid.nil?
    msgid = msgctxt
    msgctxt = nil
  end

  @entries[[msgctxt, msgid]]
end

#[]=(msgid, po_entry) ⇒ Object #[]=(msgctxt, msgid, po_entry) ⇒ Object #[]=(msgid, msgstr) ⇒ Object #[]=(msgctxt, msgid, msgstr) ⇒ Object

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

Overloads:

Since:

  • 2.3.4



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gettext/po.rb', line 99

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 {|entry| ... } ⇒ Object #eachEnumerator

Calls block once for each GetText::POEntry as a block parameter.

Overloads:

Since:

  • 2.3.4



173
174
175
# File 'lib/gettext/po.rb', line 173

def each(&block)
  @entries.each_value(&block)
end

#empty?Bool

Returns true if there is no entry, false otherwise.

Returns:

  • (Bool)

    true if there is no entry, false otherwise.

Since:

  • 2.3.4



178
179
180
# File 'lib/gettext/po.rb', line 178

def empty?
  @entries.empty?
end

#has_key?(msgid) ⇒ Boolean #has_key?(msgctxt, msgid) ⇒ Boolean

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

Overloads:

  • #has_key?(msgid) ⇒ Boolean

    Parameters:

    • msgid (String)

      msgid contained GetText::POEntry checked if it be stored PO.

  • #has_key?(msgctxt, msgid) ⇒ Boolean

    Parameters:

    • msgctxt (String)

      msgctxt contained GetText::POEntry checked if it be stored PO.

    • msgid (String)

      msgid contained GetText::POEntry checked if it be stored PO.

Returns:

  • (Boolean)

Since:

  • 2.3.4



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gettext/po.rb', line 150

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) ⇒ Object

For PoParer.

Since:

  • 2.3.4



183
184
185
186
187
# File 'lib/gettext/po.rb', line 183

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 = {}) ⇒ String

Formats each GetText::POEntry to the format of PO files and returns joined them.

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)

    Formatted and joined PO entries. It is used for creating .po files.

See Also:

Since:

  • 2.3.4



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/gettext/po.rb', line 196

def to_s(options={})
  po_string = String.new

  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