Class: GetText::POEntry

Inherits:
Object
  • Object
show all
Includes:
POEntryForRubyParser
Defined in:
lib/gettext/tools/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

Class Method Summary collapse

Instance Method Summary collapse

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.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gettext/tools/po_entry.rb', line 95

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

#commentObject

Returns the value of attribute comment.



92
93
94
# File 'lib/gettext/tools/po_entry.rb', line 92

def comment
  @comment
end

#extracted_commentObject

Returns the value of attribute extracted_comment.



89
90
91
# File 'lib/gettext/tools/po_entry.rb', line 89

def extracted_comment
  @extracted_comment
end

#flagObject

Returns the value of attribute flag.



90
91
92
# File 'lib/gettext/tools/po_entry.rb', line 90

def flag
  @flag
end

#msgctxtObject

Returns the value of attribute msgctxt.



86
87
88
# File 'lib/gettext/tools/po_entry.rb', line 86

def msgctxt
  @msgctxt
end

#msgidObject

Returns the value of attribute msgid.



81
82
83
# File 'lib/gettext/tools/po_entry.rb', line 81

def msgid
  @msgid
end

#msgid_pluralObject

Options



84
85
86
# File 'lib/gettext/tools/po_entry.rb', line 84

def msgid_plural
  @msgid_plural
end

#msgstrObject

Returns the value of attribute msgstr.



82
83
84
# File 'lib/gettext/tools/po_entry.rb', line 82

def msgstr
  @msgstr
end

#previousObject

Returns the value of attribute previous.



91
92
93
# File 'lib/gettext/tools/po_entry.rb', line 91

def previous
  @previous
end

#referencesObject

“file1:line1”, “file2:line2”, …


87
88
89
# File 'lib/gettext/tools/po_entry.rb', line 87

def references
  @references
end

#separatorObject

Returns the value of attribute separator.



85
86
87
# File 'lib/gettext/tools/po_entry.rb', line 85

def separator
  @separator
end

#translator_commentObject

Returns the value of attribute translator_comment.



88
89
90
# File 'lib/gettext/tools/po_entry.rb', line 88

def translator_comment
  @translator_comment
end

#typeObject

Required



80
81
82
# File 'lib/gettext/tools/po_entry.rb', line 80

def type
  @type
end

Class Method Details

.escape(string) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/gettext/tools/po_entry.rb', line 55

def escape(string)
  string.gsub(/([\\"\n])/) do
    special_character = $1
    if special_character == "\n"
      "\\n"
    else
      "\\#{special_character}"
    end
  end
end

.max_line_lengthObject

Gets the max line length.



75
76
77
# File 'lib/gettext/tools/po_entry.rb', line 75

def self.max_line_length
  @@max_line_length
end

.max_line_length=(len) ⇒ Object

Sets the max line length.



70
71
72
# File 'lib/gettext/tools/po_entry.rb', line 70

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”]



341
342
343
344
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
# File 'lib/gettext/tools/po_entry.rb', line 341

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.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gettext/tools/po_entry.rb', line 125

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

Raises:



371
372
373
374
375
# File 'lib/gettext/tools/po_entry.rb', line 371

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



110
111
112
113
114
115
116
# File 'lib/gettext/tools/po_entry.rb', line 110

def add_comment(new_comment)
  if (new_comment and ! new_comment.empty?)
    @extracted_comment ||= ""
    @extracted_comment += new_comment
  end
  to_s
end

#escaped(param_name) ⇒ Object

Returns a parameter representation suitable for po-files and other purposes.



120
121
122
# File 'lib/gettext/tools/po_entry.rb', line 120

def escaped(param_name)
  escape(send(param_name))
end

#format_comment(mark, comment) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/gettext/tools/po_entry.rb', line 270

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_commentObject



236
237
238
# File 'lib/gettext/tools/po_entry.rb', line 236

def format_extracted_comment
  format_comment(EXTRACTED_COMMENT_MARK, extracted_comment)
end

#format_flag_commentObject



262
263
264
# File 'lib/gettext/tools/po_entry.rb', line 262

def format_flag_comment
  format_comment(FLAG_MARK, flag)
end

#format_message(message) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/gettext/tools/po_entry.rb', line 301

def format_message(message)
  formatted_message = ""
  if not message.nil? and message.include?("\n")
    formatted_message << "\"\"\n"
    message.each_line.each do |line|
      formatted_message << "\"#{escape(line)}\"\n"
    end
  else
    formatted_message << "\"#{escape(message)}\"\n"
  end
  formatted_message
end

#format_obsolete_comment(comment) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/gettext/tools/po_entry.rb', line 284

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_commentObject



266
267
268
# File 'lib/gettext/tools/po_entry.rb', line 266

def format_previous_comment
  format_comment(PREVIOUS_COMMENT_MARK, previous)
end

#format_reference_commentObject



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/gettext/tools/po_entry.rb', line 240

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_commentObject



232
233
234
# File 'lib/gettext/tools/po_entry.rb', line 232

def format_translator_comment
  format_comment("#", translator_comment)
end

#initialize_oldPOEntry

Create the object. type should be :normal, :plural, :msgctxt or :msgctxt_plural.

Returns:

  • (POEntry)

    a new instance of POEntry



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gettext/tools/parser/ruby.rb', line 103

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.

Raises:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/gettext/tools/po_entry.rb', line 158

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).

Returns:

  • (Boolean)


151
152
153
# File 'lib/gettext/tools/po_entry.rb', line 151

def mergeable?(other)
  other && other.msgid == self.msgid && other.msgctxt == self.msgctxt
end

#msgctxt?Boolean

Returns true if the type is kind of msgctxt.

Returns:

  • (Boolean)


315
316
317
# File 'lib/gettext/tools/po_entry.rb', line 315

def msgctxt?
  [:msgctxt, :msgctxt_plural].include?(@type)
end

#plural?Boolean

Returns true if the type is kind of plural.

Returns:

  • (Boolean)


320
321
322
# File 'lib/gettext/tools/po_entry.rb', line 320

def plural?
  [:plural, :msgctxt_plural].include?(@type)
end

#to_sObject

Output the po entry for the po-file.

Raises:



179
180
181
182
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
# File 'lib/gettext/tools/po_entry.rb', line 179

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?
      no_msgctxt_message = "This POEntry is a kind of msgctxt " +
                             "but the msgctxt property is nil. " +
                             "msgid: #{msgid}"
      raise(NoMsgctxtError, no_msgctxt_message)
    end
    str << "msgctxt " << format_message(msgctxt)
  end

  str << "msgid " << format_message(msgid)
  if plural?
    if @msgid_plural.nil?
      no_plural_message = "This POEntry is a kind of plural " +
                            "but the msgid_plural property is nil. " +
                            "msgid: #{msgid}"
      raise(NoMsgidPluralError, no_plural_message)
    end

    str << "msgid_plural " << format_message(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}] " << format_message(msgstr)
      end
    end
  else
    str << "msgstr "
    str << format_message(msgstr)
  end
  str
end