class PoParser::Header

The very first entry of the PO file is considered the header

Attributes

charset[RW]
comments[RW]
encoding[RW]
entry[R]
flag[R]
language[RW]
last_translator[RW]
original_configs[R]
plural_forms[RW]
po_revision_date[RW]
pot_creation_date[RW]
project_id[RW]
report_to[RW]
team[RW]

Public Class Methods

new(entry) click to toggle source
# File lib/poparser/header.rb, line 11
def initialize(entry)
  @entry            = entry
  @comments         = entry.translator_comment.value unless entry.translator_comment.nil?
  @original_configs = convert_msgstr_to_hash(entry.msgstr)
  @flag             = entry.flag

  define_labels_instance_variables
end

Public Instance Methods

configs() click to toggle source
# File lib/poparser/header.rb, line 20
def configs
  configs = HEADER_LABELS.each_with_object({}) do |(k, v), hash|
    hash[v] = instance_variable_get "@#{k}".to_sym
  end
  @original_configs.merge(configs)
end
flag_as(flag) click to toggle source

Set flag to a custom string

# File lib/poparser/header.rb, line 43
def flag_as(flag)
  raise ArgumentError if flag.class != String

  @flag = flag
end
flag_as_fuzzy() click to toggle source

Flag the entry as Fuzzy

@return [Header]

# File lib/poparser/header.rb, line 37
def flag_as_fuzzy
  @flag = 'fuzzy'
  self
end
fuzzy?() click to toggle source

Checks if the entry is fuzzy

@return [Boolean]

# File lib/poparser/header.rb, line 30
def fuzzy?
  @flag.to_s.match?('fuzzy') ? true : false
end
inspect() click to toggle source
# File lib/poparser/header.rb, line 75
def inspect
  string = []
  if @comments.is_a?(Array)
    @comments.each do |comment|
      string << "# #{comment}".strip
    end
  else
    string << "# #{@comments}".strip
  end
  string << "#, #{@flag}" if @flag
  string << "msgid \"\"\nmsgstr \"\""
  configs.each do |k, v|
    next if v.nil? || v.empty?

    string << "#{k}: #{v}\n".dump
  end
  string.join("\n")
end
to_h() click to toggle source
# File lib/poparser/header.rb, line 49
def to_h
  @entry.to_h
end
to_s() click to toggle source
# File lib/poparser/header.rb, line 53
def to_s
  string = []
  if @comments.is_a?(Array)
    @comments.each do |comment|
      string << "# #{comment}".strip
    end
  else
    string << "# #{@comments}".strip
  end
  string << "#, #{@flag}" if @flag
  string << "msgid \"\"\nmsgstr \"\""
  configs.each do |k, v|
    if v.nil? || v.empty?
      puts "WARNING: \"#{k}\" header field is empty and skipped"
      next
    end

    string << "#{k}: #{v}\n".dump
  end
  string.join("\n")
end

Private Instance Methods

convert_msgstr_to_hash(msgstr) click to toggle source
# File lib/poparser/header.rb, line 96
def convert_msgstr_to_hash(msgstr)
  options_array = msgstr.value.map do |options|
    options.split(':', 2).each do |k|
      k.strip!
      k.chomp!
      k.gsub!(/\\+n$/, '')
    end
  end
  Hash[merge_to_previous_string(options_array)]
end
define_labels_instance_variables() click to toggle source
# File lib/poparser/header.rb, line 120
def define_labels_instance_variables
  HEADER_LABELS.each do |k, v|
    instance_variable_set("@#{k}".to_sym, @original_configs[v])
  end
end
merge_to_previous_string(array) click to toggle source

Sometimes long lines are wrapped into new lines, this function join them back

['a', 'b'], ['c']

#=> [['a', 'bc']]

# File lib/poparser/header.rb, line 111
def merge_to_previous_string(array)
  array.each_with_index do |key, index|
    next unless key.length == 1

    array[index - 1][1] += key[0]
    array.delete_at(index)
  end
end