Class: GetText::PO
- Inherits:
-
Object
- Object
- GetText::PO
- Includes:
- Enumerable
- Defined in:
- lib/gettext/tools/po.rb
Overview
Defined Under Namespace
Classes: NonExistentEntryError
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#[](msgctxt, msgid = nil) ⇒ Object
Returns POEntry containing msgctxt and msgid.
-
#[]=(*arguments) ⇒ Object
Stores POEntry or msgstr binding msgctxt and msgid.
-
#each ⇒ Object
Calls block once for each POEntry as a block parameter.
-
#has_key?(*arguments) ⇒ Boolean
Returns if PO stores POEntry containing msgctxt and msgid.
-
#initialize(order = nil) ⇒ PO
constructor
A new instance of PO.
-
#set_comment(msgid, comment, msgctxt = nil) ⇒ Object
For PoParer.
-
#to_s ⇒ String
Formats each POEntry to the format of PO files and returns joined them.
Constructor Details
#initialize(order = nil) ⇒ PO
Returns a new instance of PO.
45 46 47 48 |
# File 'lib/gettext/tools/po.rb', line 45 def initialize(order=nil) @order = order || :references @entries = {} end |
Instance Attribute Details
#order ⇒ Symbol
The order is used to sort PO entries(objects of GetText::POEntry) in #to_s.
43 44 45 |
# File 'lib/gettext/tools/po.rb', line 43 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.
60 61 62 63 64 65 66 67 |
# File 'lib/gettext/tools/po.rb', line 60 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.
91 92 93 94 95 96 97 98 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 |
# File 'lib/gettext/tools/po.rb', line 91 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 #each ⇒ Enumerator
Calls block once for each GetText::POEntry as a block parameter.
165 166 167 168 169 170 171 172 173 |
# File 'lib/gettext/tools/po.rb', line 165 def each if block_given? @entries.each do |_, entry| yield(entry) end else @entries.each_value end 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.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/gettext/tools/po.rb', line 142 def has_key?(*arguments) case arguments.size when 1 msgctxt = nil msgid = arguments[0] when 2 msgctxt = arguments[0] msgid = arguments[1] else = "has_key?: wrong number of arguments " + "(#{arguments.size} for 1..2)" raise(ArgumentError, ) end id = [msgctxt, msgid] @entries.has_key?(id) end |
#set_comment(msgid, comment, msgctxt = nil) ⇒ Object
For PoParer.
176 177 178 179 180 |
# File 'lib/gettext/tools/po.rb', line 176 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 ⇒ String
Formats each GetText::POEntry to the format of PO files and returns joined them.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/gettext/tools/po.rb', line 188 def to_s po_string = "" header_entry = @entries[[nil, ""]] po_string << header_entry.to_s unless header_entry.nil? content_entries = @entries.reject do |(_, msgid), _| msgid == :last or msgid.empty? end sort_by_order(content_entries).each do |msgid, entry| po_string << "\n" << entry.to_s end if @entries.has_key?([nil, :last]) po_string << "\n" << @entries[[nil, :last]].to_s end po_string end |