Class: PutText::POEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/puttext/po_entry.rb

Constant Summary collapse

NS_SEPARATOR =
'|'.freeze
PO_C_STYLE_ESCAPES =
{
  "\n" => '\\n',
  "\r" => '\\r',
  "\t" => '\\t',
  '\\' => '\\\\',
  '"' => '\\"'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ POEntry

Create a new POEntry

Options Hash (attrs):

  • :msgid (String)

    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.

  • :msgid_plural (String)

    the pluralized id of the string (the pluralized string that needs to be translated).

  • :msgctxt (String)

    the context of the string.

  • :msgstr (Array<String>)

    the translated strings.

  • :references (Array<String>)

    a list of files with line numbers, pointing to where the string was found.

  • :flags (Array<String>)

    a list of flags for this entry.

  • :separator (String)

    the separator of context from id in :msgid.



37
38
39
40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



19
20
21
# File 'lib/puttext/po_entry.rb', line 19

def flags
  @flags
end

#msgctxtObject (readonly)

Returns the value of attribute msgctxt.



17
18
19
# File 'lib/puttext/po_entry.rb', line 17

def msgctxt
  @msgctxt
end

#msgidObject (readonly)

Returns the value of attribute msgid.



15
16
17
# File 'lib/puttext/po_entry.rb', line 15

def msgid
  @msgid
end

#msgid_pluralObject (readonly)

Returns the value of attribute msgid_plural.



16
17
18
# File 'lib/puttext/po_entry.rb', line 16

def msgid_plural
  @msgid_plural
end

#msgstrObject (readonly)

Returns the value of attribute msgstr.



18
19
20
# File 'lib/puttext/po_entry.rb', line 18

def msgstr
  @msgstr
end

#referencesObject (readonly)

Returns the value of attribute references.



20
21
22
# File 'lib/puttext/po_entry.rb', line 20

def references
  @references
end

Instance Method Details

#==(other) ⇒ Object



104
105
106
107
108
109
# 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

#flags?Boolean

Check if the entry has any flags.



76
77
78
# File 'lib/puttext/po_entry.rb', line 76

def flags?
  !@flags.empty?
end

#merge(other_entry) ⇒ POEntry

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.



99
100
101
102
# File 'lib/puttext/po_entry.rb', line 99

def merge(other_entry)
  @references += other_entry.references
  self
end

#plural?Boolean

Check if the entry has a plural form.



82
83
84
# File 'lib/puttext/po_entry.rb', line 82

def plural?
  !@msgid_plural.nil?
end

#references?Boolean

Check if the entry has any references.



70
71
72
# File 'lib/puttext/po_entry.rb', line 70

def references?
  !@references.empty?
end

#to_sString

Convert the entry to a string representation, to be written to a .po file



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

#unique_keyObject

Return an object uniquely identifying this entry. The returned object can be used to find duplicate entries.



89
90
91
# File 'lib/puttext/po_entry.rb', line 89

def unique_key
  [@msgid, @msgctxt]
end