class Translatomatic::ResourceFile::PO

Property list resource file @see en.wikipedia.org/wiki/Property_list

Constants

PO_DATE_FORMAT

Public Class Methods

extensions() click to toggle source

(see Base.extensions)

# File lib/translatomatic/resource_file/po.rb, line 9
def self.extensions
  %w[po pot]
end
key_value?() click to toggle source

(see Base.key_value?)

# File lib/translatomatic/resource_file/po.rb, line 14
def self.key_value?
  true
end

Public Instance Methods

save(target = path, options = {}) click to toggle source

(see Base#save)

# File lib/translatomatic/resource_file/po.rb, line 44
def save(target = path, options = {})
  return unless @po
  add_created_by unless options[:no_created_by]
  target.write(@po.to_s)
end
set(key, value) click to toggle source

(see Base#set)

Calls superclass method Translatomatic::ResourceFile::Base#set
# File lib/translatomatic/resource_file/po.rb, line 19
def set(key, value)
  super(key, value)

  if @pomap.include?(key)
    po_property = @pomap[key]
    entry = po_property.entry
    if entry.plural?
      msgstr = entry.msgstr || []
      msgstr[po_property.msgstr_index] = value
      entry.msgstr = msgstr
    else
      entry.msgstr = value
    end
  else
    # new key, create po entry
    @po << {
      msgid: key,
      msgstr: value
    }
    entry = @po.entries[-1]
    add_entry(entry, :msgid, 0)
  end
end

Private Instance Methods

add_created_by() click to toggle source
# File lib/translatomatic/resource_file/po.rb, line 87
def add_created_by
  header = po_header
  header['PO-RevisionDate'] = Time.now.strftime(PO_DATE_FORMAT)
  header['Last-Translator'] = 'Translatomatic ' + VERSION
end
add_entry(entry, key, msgstr_index) click to toggle source
# File lib/translatomatic/resource_file/po.rb, line 118
def add_entry(entry, key, msgstr_index)
  map_key = entry.send(key).to_s
  return unless map_key

  msg_context = entry.msgctxt
  map_key = map_key + '.' + msg_context.to_s if msg_context
  @pomap[map_key] = PoProperty.new(entry, msgstr_index)
  @metadata.assign_key(map_key, keep_context: true)
end
init() click to toggle source
# File lib/translatomatic/resource_file/po.rb, line 74
def init
  @po = PoParser.parse('')
  @pomap = {}
end
init_pomap(po) click to toggle source

create mapping from key to PoProperty

# File lib/translatomatic/resource_file/po.rb, line 99
def init_pomap(po)
  po.entries.each_with_index do |entry, i|
    # skip PO file header if present
    # TODO: update PO-Revision-Date, Last-Provider ?
    next if entry.msgid == '' && i.zero?

    if entry.extracted_comment
      @metadata.parse_comment(entry.extracted_comment.value)
    end
    add_entry(entry, :msgid, 0)
    add_entry(entry, :msgid_plural, 1) if entry.plural?
    @metadata.clear_context
  end
end
load() click to toggle source
# File lib/translatomatic/resource_file/po.rb, line 79
def load
  @metadata.reset
  content = read_contents(@path)
  @po = PoParser.parse(content)
  init_pomap(@po)
  @properties = pomap_to_properties
end
po_header() click to toggle source
# File lib/translatomatic/resource_file/po.rb, line 93
def po_header
  # TODO: get or create header entry
  {}
end
pomap_to_properties() click to toggle source
# File lib/translatomatic/resource_file/po.rb, line 114
def pomap_to_properties
  @pomap.transform_values { |i| i.value.to_s }
end