Class: GetText::PoMessage
- Inherits:
-
Object
- Object
- GetText::PoMessage
- Includes:
- PoMessageForRubyParser
- Defined in:
- lib/gettext/tools/pomessage.rb,
lib/gettext/tools/parser/ruby.rb
Overview
Contains data related to the expression or sentence that is to be translated.
Constant Summary collapse
- PARAMS =
{ :normal => [:msgid], :plural => [:msgid, :msgid_plural], :msgctxt => [:msgctxt, :msgid], :msgctxt_plural => [:msgctxt, :msgid, :msgid_plural] }
- @@max_line_length =
70
Instance Attribute Summary collapse
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#msgctxt ⇒ Object
Returns the value of attribute msgctxt.
-
#msgid ⇒ Object
Returns the value of attribute msgid.
-
#msgid_plural ⇒ Object
Options.
-
#sources ⇒ Object
[“file1:line1”, “file2:line2”, …].
-
#type ⇒ Object
Required.
Class Method Summary collapse
-
.max_line_length ⇒ Object
Gets the max line length.
-
.max_line_length=(len) ⇒ Object
Sets the max line length.
-
.new_from_ary(ary) ⇒ Object
For backward comatibility.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Checks if the other translation target is mergeable with the current one.
- #[](number) ⇒ Object
-
#add_comment(new_comment) ⇒ Object
Support for extracted comments.
-
#escaped(param_name) ⇒ Object
Returns a parameter representation suitable for po-files and other purposes.
-
#initialize(type) ⇒ PoMessage
constructor
Create the object.
-
#initialize_old ⇒ PoMessage
Create the object.
-
#merge(other) ⇒ Object
Merges two translation targets with the same msgid and returns the merged result.
-
#msgctxt? ⇒ Boolean
Returns true if the type is kind of msgctxt.
-
#plural? ⇒ Boolean
Returns true if the type is kind of plural.
-
#to_po_str ⇒ Object
Output the po message for the po-file.
Methods included from PoMessageForRubyParser
#advance_to_next_attribute, #init_param, #set_current_attribute
Constructor Details
Instance Attribute Details
#comment ⇒ Object
Returns the value of attribute comment.
34 35 36 |
# File 'lib/gettext/tools/pomessage.rb', line 34 def comment @comment end |
#msgctxt ⇒ Object
Returns the value of attribute msgctxt.
32 33 34 |
# File 'lib/gettext/tools/pomessage.rb', line 32 def msgctxt @msgctxt end |
#msgid ⇒ Object
Returns the value of attribute msgid.
29 30 31 |
# File 'lib/gettext/tools/pomessage.rb', line 29 def msgid @msgid end |
#msgid_plural ⇒ Object
Options
31 32 33 |
# File 'lib/gettext/tools/pomessage.rb', line 31 def msgid_plural @msgid_plural end |
#sources ⇒ Object
- “file1:line1”, “file2:line2”, …
33 34 35 |
# File 'lib/gettext/tools/pomessage.rb', line 33 def sources @sources end |
#type ⇒ Object
Required
28 29 30 |
# File 'lib/gettext/tools/pomessage.rb', line 28 def type @type end |
Class Method Details
.max_line_length ⇒ Object
Gets the max line length.
23 24 25 |
# File 'lib/gettext/tools/pomessage.rb', line 23 def self.max_line_length @@max_line_length end |
.max_line_length=(len) ⇒ Object
Sets the max line length.
18 19 20 |
# File 'lib/gettext/tools/pomessage.rb', line 18 def self.max_line_length=(len) @@max_line_length = len end |
.new_from_ary(ary) ⇒ Object
For backward comatibility. This doesn’t support “comment”. ary = [msgid1, “file1:line1”, “file2:line”]
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/gettext/tools/pomessage.rb', line 183 def self.new_from_ary(ary) ary = ary.dup msgid = ary.shift sources = ary type = :normal msgctxt = nil msgid_plural = nil if msgid.include? "\004" msgctxt, msgid = msgid.split(/\004/) type = :msgctxt end if msgid.include? "\000" ids = msgid.split(/\000/) 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.sources = sources ret.msgctxt = msgctxt ret.msgid_plural = msgid_plural ret end |
Instance Method Details
#==(other) ⇒ Object
Checks if the other translation target is mergeable with the current one. Relevant are msgid and translation context (msgctxt).
62 63 64 |
# File 'lib/gettext/tools/pomessage.rb', line 62 def ==(other) other.msgid == self.msgid && other.msgctxt == self.msgctxt end |
#[](number) ⇒ Object
213 214 215 216 217 |
# File 'lib/gettext/tools/pomessage.rb', line 213 def [](number) param = @param_type[number] raise ParseError, 'no more string parameters expected' unless param send param end |
#add_comment(new_comment) ⇒ Object
Support for extracted comments. Explanation s. www.gnu.org/software/gettext/manual/gettext.html#Names
45 46 47 48 49 50 51 |
# File 'lib/gettext/tools/pomessage.rb', line 45 def add_comment(new_comment) if (new_comment and ! new_comment.empty?) @comment ||= "" @comment += new_comment end to_s end |
#escaped(param_name) ⇒ Object
Returns a parameter representation suitable for po-files and other purposes.
55 56 57 58 |
# File 'lib/gettext/tools/pomessage.rb', line 55 def escaped(param_name) orig = self.send param_name orig.gsub(/"/, '\"').gsub(/\r/, '') end |
#initialize_old ⇒ PoMessage
Create the object. type
should be :normal, :plural, :msgctxt or :msgctxt_plural.
103 104 105 106 107 |
# File 'lib/gettext/tools/parser/ruby.rb', line 103 def initialize(type) @type = type @sources = [] @param_type = PARAMS[@type] end |
#merge(other) ⇒ Object
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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/gettext/tools/pomessage.rb', line 69 def merge(other) return self unless other raise ParseError, "Translation targets do not match: \n" \ " self: #{self.inspect}\n other: '#{other.inspect}'" unless self == other if other.msgid_plural && !self.msgid_plural res = other unless (res.sources.include? self.sources[0]) res.sources += self.sources res.add_comment(self.comment) end else res = self unless (res.sources.include? other.sources[0]) res.sources += other.sources res.add_comment(other.comment) end end res end |
#msgctxt? ⇒ Boolean
Returns true if the type is kind of msgctxt. And if this is a kind of msgctxt and msgctxt property is nil, then raise an RuntimeException.
153 154 155 156 157 158 |
# File 'lib/gettext/tools/pomessage.rb', line 153 def msgctxt? if [:msgctxt, :msgctxt_plural].include? @type raise "This PoMessage is a kind of msgctxt but the msgctxt property is nil. msgid: #{msgid}" unless @msgctxt true end end |
#plural? ⇒ Boolean
Returns true if the type is kind of plural. And if this is a kind of plural and msgid_plural property is nil, then raise an RuntimeException.
163 164 165 166 167 168 |
# File 'lib/gettext/tools/pomessage.rb', line 163 def plural? if [:plural, :msgctxt_plural].include? @type raise "This PoMessage is a kind of plural but the msgid_plural property is nil. msgid: #{msgid}" unless @msgid_plural true end end |
#to_po_str ⇒ Object
Output the po message for the po-file.
90 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/gettext/tools/pomessage.rb', line 90 def to_po_str raise "msgid is nil." unless @msgid raise "sources is nil." unless @sources str = "" # extracted comments if comment comment.split("\n").each do |comment_line| str << "\n#. #{comment_line.strip}" end end # references curr_pos = @@max_line_length sources.each do |e| e = 'Countries' if e == "config/countries.yml" e = 'Regions' if e == "config/regions.yml" e = 'Genres' if e == "config/genres.yml" e = 'Flash' if e.include? "app/flash/" e = 'Javascript' if e.include? "public/javascripts/combined" if e.include? "app/views/" e = e.gsub("app/views/", '') e = e.match(/(^\w+)/)[0] end if e.include? "app/controllers/" e = e.gsub("app/controllers/", '') e = e.gsub("_controller.rb", '') end if e.include? "app/models/" e = e.gsub("app/models/", '') e = e.gsub(".rb", 's') end if curr_pos + e.size > @@max_line_length str << "\n#:" curr_pos = 3 else curr_pos += (e.size + 1) end str << " " << e end # msgctxt, msgid, msgstr str << "\nmsgctxt \"" << msgctxt << "\"" if msgctxt? str << "\nmsgid \"" << escaped(:msgid) << "\"\n" if plural? str << "msgid_plural \"" << escaped(:msgid_plural) << "\"\n" str << "msgstr[0] \"\"\n" str << "msgstr[1] \"\"\n" else str << "msgstr \"\"\n" end str end |