class GetText::Tools::MsgCat::Merger

@private

Public Class Methods

new(output_po, config) click to toggle source
# File lib/gettext/tools/msgcat.rb, line 69
def initialize(output_po, config)
  @output_po = output_po
  @config = config
end

Public Instance Methods

merge(po) click to toggle source
# File lib/gettext/tools/msgcat.rb, line 74
def merge(po)
  po.each do |entry|
    if entry.msgid == :last
      next unless @config.output_obsolete_entries?
    end
    id = [entry.msgctxt, entry.msgid]
    if @output_po.has_key?(*id)
      merged_entry = merge_entry(@output_po[*id], entry)
    else
      merged_entry = entry
    end
    next unless merged_entry
    if merged_entry.header?
      update_po_revision_date!(merged_entry)
      remove_header_fields!(merged_entry)
    end
    @output_po[*id] = merged_entry
  end
end

Private Instance Methods

merge_entry(base_entry, new_entry) click to toggle source
# File lib/gettext/tools/msgcat.rb, line 95
def merge_entry(base_entry, new_entry)
  if base_entry.header?
    return merge_header(base_entry, new_entry)
  end

  if base_entry.fuzzy?
    return merge_fuzzy_entry(base_entry, new_entry)
  end

  if base_entry.translated?
    base_entry
  else
    new_entry
  end
end
merge_fuzzy_entry(base_entry, new_entry) click to toggle source
# File lib/gettext/tools/msgcat.rb, line 115
def merge_fuzzy_entry(base_entry, new_entry)
  return new_entry unless new_entry.fuzzy?
  return nil unless @config.include_fuzzy?
  base_entry
end
merge_header(base_entry, new_entry) click to toggle source
# File lib/gettext/tools/msgcat.rb, line 111
def merge_header(base_entry, new_entry)
  base_entry
end
remove_header_fields!(header_entry) click to toggle source
# File lib/gettext/tools/msgcat.rb, line 143
def remove_header_fields!(header_entry)
  remove_header_fields = @config.remove_header_fields
  return if remove_header_fields.empty?
  msgstr = header_entry.msgstr
  return if msgstr.nil?

  new_msgstr = String.new
  msgstr.each_line do |line|
    case line
    when /\A([\w\-]+):/
      name = $1
      next if remove_header_fields.include?(name)
    end
    new_msgstr << line
  end
  header_entry.msgstr = new_msgstr
end
update_po_revision_date!(header_entry) click to toggle source
# File lib/gettext/tools/msgcat.rb, line 121
def update_po_revision_date!(header_entry)
  return unless @config.update_po_revision_date?

  now = Time.now.strftime("%Y-%m-%d %H:%M%z")
  po_revision_date_value = "PO-Revision-Date: #{now}\n"
  have_po_revision_date = false
  new_msgstr = String.new
  header_entry.msgstr.each_line do |line|
    case line
    when /\APO-Revision-Date:/
      new_msgstr << po_revision_date_value
      have_po_revision_date = true
    else
      new_msgstr << line
    end
  end
  unless have_po_revision_date
    new_msgstr << po_revision_date_value
  end
  header_entry.msgstr = new_msgstr
end