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

Parameters:

  • attrs (Hash)

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.

  • :references (Array<String>)

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

  • :separator (String)

    the separator of context from id in :msgid.



33
34
35
36
37
38
39
40
41
42
# File 'lib/puttext/po_entry.rb', line 33

def initialize(attrs)
  id, ctx = extract_context(
    attrs[:msgid], attrs[:separator] || NS_SEPARATOR
  )

  @msgid        = id
  @msgctxt      = attrs[:msgctxt] || ctx
  @msgid_plural = attrs[:msgid_plural]
  @references   = attrs[:references] || []
end

Instance Attribute Details

#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

#referencesObject (readonly)

Returns the value of attribute references.



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

def references
  @references
end

Instance Method Details

#==(other) ⇒ Object



91
92
93
94
95
96
# File 'lib/puttext/po_entry.rb', line 91

def ==(other)
  @msgid == other.msgid &&
    @msgid_plural == other.msgid_plural &&
    @msgctxt == other.msgctxt &&
    @references == other.references
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.

Parameters:

  • other_entry (POEntry)

    the entry to merge with.

Returns:



86
87
88
89
# File 'lib/puttext/po_entry.rb', line 86

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

#plural?Boolean

Check if the entry has a plural form.

Returns:

  • (Boolean)

    whether the entry has a plural form.



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

def plural?
  !@msgid_plural.nil?
end

#references?Boolean

Check if the entry has any references.

Returns:

  • (Boolean)

    whether the entry has any references.



63
64
65
# File 'lib/puttext/po_entry.rb', line 63

def references?
  !@references.empty?
end

#to_sString

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

Returns:

  • (String)

    a string representation of the entry.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puttext/po_entry.rb', line 46

def to_s
  str = String.new('')

  # Add comments
  str = add_comment(str, ':', @references.join(' ')) if references?

  # 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.

Returns:

  • an object uniquely identifying this entry.



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

def unique_key
  [@msgid, @msgctxt]
end