class PoParser::Po
Attributes
Public Class Methods
# File lib/poparser/po.rb, line 10 def initialize(args = {}) @entries = [] @path = args[:path] end
Public Instance Methods
add new entries to po file
@example
entry = { translator_comment: 'comment', reference: 'reference comment', flag: 'fuzzy', msgid: 'translatable string', msgstr: 'translation' } add(entry)
@param entry [Hash, Array] a hash of entry contents or an array of hashes @return [Po]
# File lib/poparser/po.rb, line 29 def add(entry) return import_hash(entry) if entry.is_a?(Hash) return import_array(entry) if entry.is_a?(Array) raise ArgumentError, 'Must be a hash or an array of hashes' end
Delete entry from po file
@example
delete(entry)
@param entry [Entry] to be deleted @return [Entry]
# File lib/poparser/po.rb, line 45 def delete(entry) raise(ArgumentError, 'Must be an Entry') unless entry.is_a?(PoParser::Entry) @entries.delete(entry) end
# File lib/poparser/po.rb, line 154 def each @entries.each do |entry| yield entry end end
Returns an array of all entries in po file
@param include_obsolete [Boolean] Whether include obsolete entries or not @return [Array]
# File lib/poparser/po.rb, line 55 def entries(include_obsolete = false) return @entries if include_obsolete find_all { |entry| !entry.obsolete? } end
Finds all entries that are flaged as fuzzy
@return [Array] an array of fuzzy entries
# File lib/poparser/po.rb, line 65 def fuzzy find_all(&:fuzzy?) end
# File lib/poparser/po.rb, line 160 def inspect "<#{self.class.name}, Translated: #{translated.length}"\ "(#{stats[:translated]}%) Untranslated: #{untranslated.length}"\ "(#{stats[:untranslated]}%) Fuzzy: #{fuzzy.length}(#{stats[:fuzzy]}%)>" end
Finds all obsolete entries
@return [Array] an array of obsolete entries
# File lib/poparser/po.rb, line 86 def obsolete find_all(&:obsolete?) end
Saves the file to the provided path
# File lib/poparser/po.rb, line 148 def save_file raise ArgumentError, 'Need a Path to save the file' if @path.nil? File.open(@path, 'w') { |file| file.write(to_s) } end
Search for entries with provided string
@param label [Symbol] One of the known LABELS @param string [String] String to search for @return [Array] Array of matched entries
# File lib/poparser/po.rb, line 104 def search_in(label, string) raise(ArgumentError, "Unknown key: #{label}") unless LABELS.include?(label.to_sym) find_all do |entry| text = entry.send(label).str text.match(/#{string}/i) end end
Count of all entries without counting obsolete entries
@return [String]
# File lib/poparser/po.rb, line 94 def size entries.length end
Shows statistics and status of the provided file in percentage.
@return [Hash] a hash of translated, untranslated and fuzzy percentages
# File lib/poparser/po.rb, line 116 def stats untranslated_size = untranslated.size translated_size = translated.size fuzzy_size = fuzzy.size { translated: percentage(translated_size), untranslated: percentage(untranslated_size), fuzzy: percentage(fuzzy_size), } end
Converts Po
file to an hashes of entries
@return [Array] array of hashes of entries
# File lib/poparser/po.rb, line 131 def to_h array = @entries.map(&:to_h) array.unshift(@header.to_h) if @header array end
Shows a String representation of the Po
file
@return [String]
# File lib/poparser/po.rb, line 140 def to_s array = @entries.map(&:to_s) # add a blank line after header array.unshift(@header.to_s, '') if @header array.join("\n") end
Finds all entries that are translated
@return [Array] an array of translated entries
# File lib/poparser/po.rb, line 79 def translated find_all(&:translated?) end
Finds all entries that are untranslated
@return [Array] an array of untranslated entries
# File lib/poparser/po.rb, line 72 def untranslated find_all(&:untranslated?) end
Private Instance Methods
rubocop:disable Style/SafeNavigation
# File lib/poparser/po.rb, line 189 def add_entry(entry) return add_header_entry(entry) if entry[:msgid] && entry[:msgid].empty? @entries << Entry.new(entry) end
rubocop:enable Style/SafeNavigation
# File lib/poparser/po.rb, line 196 def add_header_entry(entry) raise('Duplicate entry, header was already instantiated') if @header @header = Header.new(Entry.new(entry)) end
# File lib/poparser/po.rb, line 182 def import_array(entry) entry.each { |en| add_entry(en) } self end
# File lib/poparser/po.rb, line 176 def import_hash(entry) add_entry(entry) self end
calculates percentages based on total number of entries
@param [Integer] number of entries @return [Float] percentage of the provided entries
# File lib/poparser/po.rb, line 172 def percentage(count) ((count.to_f / size) * 100).round(1) end