Class: GetText::PO
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(&block) ⇒ Object
Calls block once for each POEntry as a block parameter.
-
#empty? ⇒ Bool
true
if there is no entry,false
otherwise. -
#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(options = {}) ⇒ 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.
53 54 55 56 |
# File 'lib/gettext/po.rb', line 53 def initialize(order=nil) @order = order || :reference @entries = {} end |
Instance Attribute Details
#order ⇒ Symbol
The order is used to sort PO entries(objects of GetText::POEntry) in #to_s.
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.
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.
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 #each ⇒ Enumerator
Calls block once for each GetText::POEntry as a block parameter.
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.
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.
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 = "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.
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.
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(={}) po_string = String.new header_entry = @entries[[nil, ""]] unless header_entry.nil? po_string << header_entry.to_s(.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() end if @entries.has_key?([nil, :last]) po_string << "\n" unless po_string.empty? po_string << @entries[[nil, :last]].to_s() end po_string end |