class PutText::POEntry
Constants
- NS_SEPARATOR
- PO_C_STYLE_ESCAPES
Attributes
Public Class Methods
Create a new POEntry
@param [Hash] attrs @option attrs [String] :msgid the id of the string (the string that needs
to be translated). Can include a context, separated from the id by {NS_SEPARATOR} or by the specified :separator.
@option attrs [String] :msgid_plural the pluralized id of the string (the
pluralized string that needs to be translated).
@option attrs [String] :msgctxt the context of the string. @option attrs [Array<String>] :msgstr the translated strings. @option attrs [Array<String>] :references a list of files with line
numbers, pointing to where the string was found.
@option attrs [Array<String>] :flags a list of flags for this entry. @option attrs [String] :separator the separator of context from id in
:msgid.
# File lib/puttext/po_entry.rb, line 37 def initialize(attrs) id, ctx = extract_context( attrs[:msgid], attrs[:separator] || NS_SEPARATOR ) @msgid = id @msgctxt = attrs[:msgctxt] || ctx @msgid_plural = attrs[:msgid_plural] @msgstr = Array(attrs[:msgstr] || '') @references = attrs[:references] || [] @flags = attrs[:flags] || [] end
Public Instance Methods
# File lib/puttext/po_entry.rb, line 104 def ==(other) @msgid == other.msgid && @msgid_plural == other.msgid_plural && @msgctxt == other.msgctxt && @references == other.references end
Check if the entry has any flags. @return [Boolean] whether the entry has any flags.
# File lib/puttext/po_entry.rb, line 76 def flags? !@flags.empty? end
Merge this entry with another entry. Modifies the current entry in place. Currently, merges only the references, and leaves other attributes of the current entry untouched.
@param [POEntry] other_entry the entry to merge with. @return [POEntry] the merged entry.
# File lib/puttext/po_entry.rb, line 99 def merge(other_entry) @references += other_entry.references self end
Check if the entry has a plural form. @return [Boolean] whether the entry has a plural form.
# File lib/puttext/po_entry.rb, line 82 def plural? !@msgid_plural.nil? end
Check if the entry has any references. @return [Boolean] whether the entry has any references.
# File lib/puttext/po_entry.rb, line 70 def references? !@references.empty? end
Convert the entry to a string representation, to be written to a .po file @return [String] a string representation of the entry.
# File lib/puttext/po_entry.rb, line 52 def to_s str = String.new('') # Add comments str = add_comment(str, ':', @references.join(' ')) if references? str = add_comment(str, ',', @flags.join("\n")) if flags? # Add id and context str = add_string(str, 'msgctxt', @msgctxt) if @msgctxt str = add_string(str, 'msgid', @msgid) str = add_string(str, 'msgid_plural', @msgid_plural) if plural? str = add_translations(str) str end
Return an object uniquely identifying this entry. The returned object can be used to find duplicate entries. @return an object uniquely identifying this entry.
# File lib/puttext/po_entry.rb, line 89 def unique_key [@msgid, @msgctxt] end
Private Instance Methods
# File lib/puttext/po_entry.rb, line 118 def add_comment(str, comment_type, value) value.each_line do |line| str << '#' str << comment_type str << ' ' str << line str << "\n" end str end
# File lib/puttext/po_entry.rb, line 172 def add_multiline_str_part(str, part, last) return if last && part.empty? str << "\n\"" str << po_escape_string(part) str << '\\n' unless last str << '"' end
# File lib/puttext/po_entry.rb, line 130 def add_string(str, id, value) str << id str << ' ' str << string_to_po(value) str << "\n" end
# File lib/puttext/po_entry.rb, line 137 def add_translations(str) if plural? @msgstr.each_with_index do |msgstr, index| add_string(str, "msgstr[#{index}]", msgstr) end else add_string(str, 'msgstr', @msgstr.first) end str end
# File lib/puttext/po_entry.rb, line 113 def extract_context(str, separator) parts = str.rpartition(separator) [parts[2], parts[0] == '' ? nil : parts[0]] end
# File lib/puttext/po_entry.rb, line 161 def multiline_string_to_po(str_lines) po_str = String.new('""') str_lines.each_with_index do |line, index| last = index == str_lines.length - 1 add_multiline_str_part(po_str, line, last) end po_str end
# File lib/puttext/po_entry.rb, line 181 def po_escape_string(str) encoded = String.new('') str.each_char do |char| encoded << if PO_C_STYLE_ESCAPES[char] PO_C_STYLE_ESCAPES[char] else char end end encoded end
# File lib/puttext/po_entry.rb, line 149 def string_to_po(str) lines = str.split("\n", -1) if lines.empty? '""' elsif lines.length == 1 "\"#{po_escape_string(lines[0])}\"" else multiline_string_to_po(lines) end end