Class: GetText::RMsgMerge::Merger

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/tools/rmsgmerge.rb

Overview

:nodoc:

Constant Summary collapse

DOT_COMMENT_RE =

the definition, take the msgstr from the definition. Add this merged entry to the output message list.

/\A#\./
SEMICOLON_COMMENT_RE =
/\A#\:/
FUZZY_RE =
/\A#\,/
NOT_SPECIAL_COMMENT_RE =
/\A#([^:.,]|\z)/
CRLF_RE =
/\r?\n/
POT_DATE_EXTRACT_RE =
/POT-Creation-Date:\s*(.*)?\s*$/
POT_DATE_RE =
/POT-Creation-Date:.*?$/

Instance Method Summary collapse

Instance Method Details

#change_reference_comment(msgid, podata) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/gettext/tools/rmsgmerge.rb', line 276

def change_reference_comment(msgid, podata)
  normal_comment = []
  dot_comment = []
  semi_comment = []
  is_fuzzy = false

  podata.comment(msgid).split(CRLF_RE).each do |l|
    if DOT_COMMENT_RE =~ l
      dot_comment << l
    elsif SEMICOLON_COMMENT_RE =~ l
      semi_comment << l
    elsif FUZZY_RE =~ l
      is_fuzzy = true
    else
      normal_comment << l
    end
  end

  str = format_comment(normal_comment, dot_comment, semi_comment, is_fuzzy)
  podata.set_comment(msgid, str)
end

#format_comment(normal_comment, dot_comment, semi_comment, is_fuzzy) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/gettext/tools/rmsgmerge.rb', line 298

def format_comment(normal_comment, dot_comment, semi_comment, is_fuzzy)
  str = ''

  str << normal_comment.join("\n").gsub(/^#(\s*)/){|sss|
    if $1 == ""
      "# "
    else
      sss
    end
  }
  if normal_comment.size > 0
    str << "\n"
  end

  str << dot_comment.join("\n").gsub(/^#.(\s*)/){|sss|
    if $1 == ""
      "#. "
    else
      sss
    end
  }
  if dot_comment.size > 0
    str << "\n"
  end

  str << semi_comment.join("\n").gsub(/^#:\s*/, "#: ")
  if semi_comment.size > 0
    str << "\n"
  end

  if is_fuzzy
    str << "#, fuzzy\n"
  end

  str
end

#merge(definition, reference) ⇒ Object



173
174
175
176
177
178
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
# File 'lib/gettext/tools/rmsgmerge.rb', line 173

def merge(definition, reference)
  # deep copy
  result = Marshal.load( Marshal.dump(reference) )

  used = []
  merge_header(result, definition)

  result.each_msgid do |msgid|
    if definition.msgid?(msgid)
      used << msgid
      merge_message(msgid, result, msgid, definition)
    elsif other_msgid = definition.search_msgid_fuzzy(msgid, used)
      used << other_msgid
      merge_fuzzy_message(msgid, result, other_msgid, definition)
    elsif msgid.index("\000") and ( reference.msgstr(msgid).gsub("\000", '') == '' )
      # plural
      result[msgid] = "\000" * definition.nplural
    else
      change_reference_comment(msgid, result)
    end
  end

  ###################################################################
  # msgids which are not used in reference are handled as obsolete. #
  ###################################################################
  last_comment = result.comment(:last) || ''
  definition.each_msgid do |msgid|
    unless used.include?(msgid)
      last_comment << "\n"
      last_comment << definition.generate_po_entry(msgid).strip.gsub(/^/, '#. ')
      last_comment << "\n"
    end
  end
  result.set_comment(:last, last_comment)

  result
end

#merge_comment(msgid, target, def_msgid, definition) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/gettext/tools/rmsgmerge.rb', line 247

def merge_comment(msgid, target, def_msgid, definition)
  ref_comment = target.comment(msgid)
  def_comment = definition.comment(def_msgid)

  normal_comment = []
  dot_comment = []
  semi_comment = []
  is_fuzzy = false

  def_comment.split(CRLF_RE).each do |l|
    if NOT_SPECIAL_COMMENT_RE =~ l
      normal_comment << l
    end
  end

  ref_comment.split(CRLF_RE).each do |l|
    if DOT_COMMENT_RE =~ l
      dot_comment << l
    elsif SEMICOLON_COMMENT_RE =~ l
      semi_comment << l
    elsif FUZZY_RE =~ l
      is_fuzzy = true
    end
  end

  str = format_comment(normal_comment, dot_comment, semi_comment, is_fuzzy)
  target.set_comment(msgid, str)
end

#merge_fuzzy_message(msgid, target, def_msgid, definition) ⇒ Object

for the future



243
244
245
# File 'lib/gettext/tools/rmsgmerge.rb', line 243

def merge_fuzzy_message(msgid, target, def_msgid, definition)
  merge_message(msgid, target, def_msgid, definition)
end

#merge_header(target, definition) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/gettext/tools/rmsgmerge.rb', line 335

def merge_header(target, definition)
  merge_comment('', target, '', definition)

  msg = target.msgstr('')
  def_msg = definition.msgstr('')
  if POT_DATE_EXTRACT_RE =~ msg
    time = $1
    def_msg = def_msg.sub(POT_DATE_RE, "POT-Creation-Date: #{time}")
  end

  target[''] = def_msg
end

#merge_message(msgid, target, def_msgid, definition) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gettext/tools/rmsgmerge.rb', line 211

def merge_message(msgid, target, def_msgid, definition)
  merge_comment(msgid, target, def_msgid, definition)

  ############################################
  # check mismatch of msgid and msgid_plural #
  ############################################
  def_msgstr = definition[def_msgid]
  if msgid.index("\000")
    if def_msgstr.index("\000")
      # OK
      target[msgid] = def_msgstr
    else
      # NG
      s = ''
      definition.nplural.times {
        s << def_msgstr
        s << "\000"
      }
      target[msgid] = s
    end
  else
    if def_msgstr.index("\000")
      # NG
      target[msgid] = def_msgstr.split("\000")[0]
    else
      # OK
      target[msgid] = def_msgstr
    end
  end
end