Class: GetText::POEntry
- Inherits:
-
Object
- Object
- GetText::POEntry
- Includes:
- POEntryForRubyParser
- Defined in:
- lib/gettext/po_entry.rb,
lib/gettext/tools/parser/ruby.rb
Overview
Contains data related to the expression or sentence that is to be translated.
Defined Under Namespace
Classes: InvalidTypeError, NoMsgctxtError, NoMsgidError, NoMsgidPluralError
Constant Summary collapse
- PARAMS =
{ :normal => [:msgid, :separator, :msgstr], :plural => [:msgid, :msgid_plural, :separator, :msgstr], :msgctxt => [:msgctxt, :msgid, :msgstr], :msgctxt_plural => [:msgctxt, :msgid, :msgid_plural, :msgstr] }
- TRANSLATOR_COMMENT_MARK =
"# "- EXTRACTED_COMMENT_MARK =
"#."- FLAG_MARK =
"#,"- PREVIOUS_COMMENT_MARK =
"#|"- REFERENCE_COMMENT_MARK =
"#:"- @@max_line_length =
70
Instance Attribute Summary collapse
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#extracted_comment ⇒ Object
Returns the value of attribute extracted_comment.
-
#flag ⇒ Object
Returns the value of attribute flag.
-
#msgctxt ⇒ Object
Returns the value of attribute msgctxt.
-
#msgid ⇒ Object
Returns the value of attribute msgid.
-
#msgid_plural ⇒ Object
Options.
-
#msgstr ⇒ Object
Returns the value of attribute msgstr.
-
#previous ⇒ Object
Returns the value of attribute previous.
-
#references ⇒ Object
["file1:line1", "file2:line2", ...].
-
#separator ⇒ Object
Returns the value of attribute separator.
-
#translator_comment ⇒ Object
Returns the value of attribute translator_comment.
-
#type ⇒ Object
Required.
Class Method Summary collapse
- .escape(string) ⇒ Object
-
.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 self has same attributes as other.
- #[](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.
- #format_comment(mark, comment) ⇒ Object
- #format_extracted_comment ⇒ Object
- #format_flag_comment ⇒ Object
- #format_message(message) ⇒ Object
- #format_obsolete_comment(comment) ⇒ Object
- #format_previous_comment ⇒ Object
- #format_reference_comment ⇒ Object
- #format_translator_comment ⇒ Object
-
#initialize(type) ⇒ POEntry
constructor
Create the object.
-
#initialize_old ⇒ POEntry
Create the object.
-
#merge(other) ⇒ Object
Merges two translation targets with the same msgid and returns the merged result.
-
#mergeable?(other) ⇒ Boolean
Checks if the other translation target is mergeable with the current one.
-
#msgctxt? ⇒ Boolean
Returns true if the type is kind of msgctxt.
-
#plural? ⇒ Boolean
Returns true if the type is kind of plural.
-
#to_s ⇒ Object
Output the po entry for the po-file.
Methods included from POEntryForRubyParser
#advance_to_next_attribute, #init_param, #set_current_attribute
Constructor Details
#initialize(type) ⇒ POEntry
Create the object. +type+ should be :normal, :plural, :msgctxt or :msgctxt_plural.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gettext/po_entry.rb', line 98 def initialize(type) self.type = type @translator_comment = nil @extracted_comment = nil @references = [] @flag = nil @previous = nil @msgctxt = nil @msgid = nil @msgid_plural = nil @msgstr = nil end |
Instance Attribute Details
#comment ⇒ Object
Returns the value of attribute comment.
95 96 97 |
# File 'lib/gettext/po_entry.rb', line 95 def comment @comment end |
#extracted_comment ⇒ Object
Returns the value of attribute extracted_comment.
92 93 94 |
# File 'lib/gettext/po_entry.rb', line 92 def extracted_comment @extracted_comment end |
#flag ⇒ Object
Returns the value of attribute flag.
93 94 95 |
# File 'lib/gettext/po_entry.rb', line 93 def flag @flag end |
#msgctxt ⇒ Object
Returns the value of attribute msgctxt.
89 90 91 |
# File 'lib/gettext/po_entry.rb', line 89 def msgctxt @msgctxt end |
#msgid ⇒ Object
Returns the value of attribute msgid.
84 85 86 |
# File 'lib/gettext/po_entry.rb', line 84 def msgid @msgid end |
#msgid_plural ⇒ Object
Options
87 88 89 |
# File 'lib/gettext/po_entry.rb', line 87 def msgid_plural @msgid_plural end |
#msgstr ⇒ Object
Returns the value of attribute msgstr.
85 86 87 |
# File 'lib/gettext/po_entry.rb', line 85 def msgstr @msgstr end |
#previous ⇒ Object
Returns the value of attribute previous.
94 95 96 |
# File 'lib/gettext/po_entry.rb', line 94 def previous @previous end |
#references ⇒ Object
["file1:line1", "file2:line2", ...]
90 91 92 |
# File 'lib/gettext/po_entry.rb', line 90 def references @references end |
#separator ⇒ Object
Returns the value of attribute separator.
88 89 90 |
# File 'lib/gettext/po_entry.rb', line 88 def separator @separator end |
#translator_comment ⇒ Object
Returns the value of attribute translator_comment.
91 92 93 |
# File 'lib/gettext/po_entry.rb', line 91 def translator_comment @translator_comment end |
#type ⇒ Object
Required
83 84 85 |
# File 'lib/gettext/po_entry.rb', line 83 def type @type end |
Class Method Details
.escape(string) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/gettext/po_entry.rb', line 55 def escape(string) string.gsub(/([\\"\t\n])/) do special_character = $1 case special_character when "\t" "\\t" when "\n" "\\n" else "\\#{special_character}" end end end |
.max_line_length ⇒ Object
Gets the max line length.
78 79 80 |
# File 'lib/gettext/po_entry.rb', line 78 def self.max_line_length @@max_line_length end |
.max_line_length=(len) ⇒ Object
Sets the max line length.
73 74 75 |
# File 'lib/gettext/po_entry.rb', line 73 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"]
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/gettext/po_entry.rb', line 345 def self.new_from_ary(ary) ary = ary.dup msgid = ary.shift references = 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.references = references ret.msgctxt = msgctxt ret.msgid_plural = msgid_plural ret end |
Instance Method Details
#==(other) ⇒ Object
Checks if the self has same attributes as other.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/gettext/po_entry.rb', line 129 def ==(other) not other.nil? and type == other.type and msgid == other.msgid and msgstr == other.msgstr and msgid_plural == other.msgid_plural and separator == other.separator and msgctxt == other.msgctxt and translator_comment == other.translator_comment and extracted_comment == other.extracted_comment and references == other.references and flag == other.flag and previous == other.previous and comment == other.comment end |
#[](number) ⇒ Object
375 376 377 378 379 |
# File 'lib/gettext/po_entry.rb', line 375 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. http://www.gnu.org/software/gettext/manual/gettext.html#Names
113 114 115 116 117 118 119 120 |
# File 'lib/gettext/po_entry.rb', line 113 def add_comment(new_comment) if (new_comment and ! new_comment.empty?) @extracted_comment ||= "" @extracted_comment << "\n" unless @extracted_comment.empty? @extracted_comment << new_comment end to_s end |
#escaped(param_name) ⇒ Object
Returns a parameter representation suitable for po-files and other purposes.
124 125 126 |
# File 'lib/gettext/po_entry.rb', line 124 def escaped(param_name) escape(send(param_name)) end |
#format_comment(mark, comment) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/gettext/po_entry.rb', line 274 def format_comment(mark, comment) return "" if comment.nil? formatted_comment = "" comment.each_line do |comment_line| if comment_line == "\n" formatted_comment << "#{mark}\n" else formatted_comment << "#{mark} #{comment_line.strip}\n" end end formatted_comment end |
#format_extracted_comment ⇒ Object
240 241 242 |
# File 'lib/gettext/po_entry.rb', line 240 def format_extracted_comment format_comment(EXTRACTED_COMMENT_MARK, extracted_comment) end |
#format_flag_comment ⇒ Object
266 267 268 |
# File 'lib/gettext/po_entry.rb', line 266 def format_flag_comment format_comment(FLAG_MARK, flag) end |
#format_message(message) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/gettext/po_entry.rb', line 305 def () = "" if not .nil? and .include?("\n") << "\"\"\n" .each_line.each do |line| << "\"#{escape(line)}\"\n" end else << "\"#{escape()}\"\n" end end |
#format_obsolete_comment(comment) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/gettext/po_entry.rb', line 288 def format_obsolete_comment(comment) mark = "#~" return "" if comment.nil? formatted_comment = "" comment.each_line do |comment_line| if /\A#[^~]/ =~ comment_line or comment_line.start_with?(mark) formatted_comment << comment_line elsif comment_line == "\n" formatted_comment << "\n" else formatted_comment << "#{mark} #{comment_line.strip}\n" end end formatted_comment end |
#format_previous_comment ⇒ Object
270 271 272 |
# File 'lib/gettext/po_entry.rb', line 270 def format_previous_comment format_comment(PREVIOUS_COMMENT_MARK, previous) end |
#format_reference_comment ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/gettext/po_entry.rb', line 244 def format_reference_comment max_line_length = 70 formatted_reference = "" if not references.nil? and not references.empty? formatted_reference << REFERENCE_COMMENT_MARK line_size = 2 references.each do |reference| if line_size + reference.size > max_line_length formatted_reference << "\n" formatted_reference << "#{REFERENCE_COMMENT_MARK} #{reference}" line_size = 3 + reference.size else formatted_reference << " #{reference}" line_size += 1 + reference.size end end formatted_reference << "\n" end formatted_reference end |
#format_translator_comment ⇒ Object
236 237 238 |
# File 'lib/gettext/po_entry.rb', line 236 def format_translator_comment format_comment("#", translator_comment) end |
#initialize_old ⇒ POEntry
Create the object. +type+ should be :normal, :plural, :msgctxt or :msgctxt_plural.
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/gettext/tools/parser/ruby.rb', line 104 def initialize(type) self.type = type @translator_comment = nil @extracted_comment = nil @references = [] @flag = nil @previous = nil @msgctxt = nil @msgid = nil @msgid_plural = nil @msgstr = nil 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.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/gettext/po_entry.rb', line 162 def merge(other) return self unless other raise ParseError, "Translation targets do not match: \n" \ " self: #{self.inspect}\n other: '#{other.inspect}'" unless self.mergeable?(other) if other.msgid_plural && !self.msgid_plural res = other unless (res.references.include? self.references[0]) res.references += self.references res.add_comment(self.extracted_comment) end else res = self unless (res.references.include? other.references[0]) res.references += other.references res.add_comment(other.extracted_comment) end end res end |
#mergeable?(other) ⇒ Boolean
Checks if the other translation target is mergeable with the current one. Relevant are msgid and translation context (msgctxt).
155 156 157 |
# File 'lib/gettext/po_entry.rb', line 155 def mergeable?(other) other && other.msgid == self.msgid && other.msgctxt == self.msgctxt end |
#msgctxt? ⇒ Boolean
Returns true if the type is kind of msgctxt.
319 320 321 |
# File 'lib/gettext/po_entry.rb', line 319 def msgctxt? [:msgctxt, :msgctxt_plural].include?(@type) end |
#plural? ⇒ Boolean
Returns true if the type is kind of plural.
324 325 326 |
# File 'lib/gettext/po_entry.rb', line 324 def plural? [:plural, :msgctxt_plural].include?(@type) end |
#to_s ⇒ Object
Output the po entry for the po-file.
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/gettext/po_entry.rb', line 183 def to_s raise(NoMsgidError, "msgid is nil.") unless @msgid str = "" # extracted comments if @msgid == :last return format_obsolete_comment(comment) end str << format_translator_comment str << format_extracted_comment str << format_reference_comment str << format_flag_comment str << format_previous_comment # msgctxt, msgid, msgstr if msgctxt? if @msgctxt.nil? = "This POEntry is a kind of msgctxt " + "but the msgctxt property is nil. " + "msgid: #{msgid}" raise(NoMsgctxtError, ) end str << "msgctxt " << (msgctxt) end str << "msgid " << (msgid) if plural? if @msgid_plural.nil? = "This POEntry is a kind of plural " + "but the msgid_plural property is nil. " + "msgid: #{msgid}" raise(NoMsgidPluralError, ) end str << "msgid_plural " << (msgid_plural) if msgstr.nil? str << "msgstr[0] \"\"\n" str << "msgstr[1] \"\"\n" else msgstrs = msgstr.split("\000", -1) msgstrs.each_with_index do |msgstr, index| str << "msgstr[#{index}] " << (msgstr) end end else str << "msgstr " str << (msgstr) end str end |