Class: Xliff::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/xliff/entry.rb

Overview

Models a single translation string

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, source:, target:, note: nil, xml_space: 'default') ⇒ Entry

Create a blank Entry object

Most often used to build an XLIFF file by hand.

Parameters:

  • id (String)

    A unique identifier for this string.

  • source (String)

    The original text.

  • target (String)

    The translated text.

  • note (String) (defaults to: nil)

    Documentation for translators understand the context of a string.

  • xml_space (String) (defaults to: 'default')

    The XML whitespace processing behaviour.



41
42
43
44
45
46
47
# File 'lib/xliff/entry.rb', line 41

def initialize(id:, source:, target:, note: nil, xml_space: 'default')
  @id = id
  @source = source
  @target = target
  @note = note
  @xml_space = xml_space
end

Instance Attribute Details

#idString

A unique identifier for this translation string

This will often match the source language string, but can also be used for cases where the source translation is not a suitable unique identifier.

Returns:

  • (String)


14
15
16
# File 'lib/xliff/entry.rb', line 14

def id
  @id
end

#noteString

Documentation for translators understand the context of a string

Returns:

  • (String)


26
27
28
# File 'lib/xliff/entry.rb', line 26

def note
  @note
end

#sourceString

The original text

Returns:

  • (String)


18
19
20
# File 'lib/xliff/entry.rb', line 18

def source
  @source
end

#targetString

The translated text

Returns:

  • (String)


22
23
24
# File 'lib/xliff/entry.rb', line 22

def target
  @target
end

#xml_spaceString

The XML whitespace processing behaviour

Returns:

  • (String)


30
31
32
# File 'lib/xliff/entry.rb', line 30

def xml_space
  @xml_space
end

Class Method Details

.from_xml(xml) ⇒ Entry?

Decode the given XML into an ‘Entry` object, if possible

Raises for invalid input

Returns:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/xliff/entry.rb', line 80

def self.from_xml(xml)
  validate_source_xml(xml)

  Entry.new(
    id: xml['id'],
    source: xml.at('source').content,
    target: xml.at('target').content,
    note: xml.at('note').content || nil,
    xml_space: xml['xml:space']
  )
end

.validate_source_xml(xml) ⇒ void

This method returns an undefined value.

Validate the given XML to ensure that it’s a valid ‘<trans-unit>` element



95
96
97
98
99
# File 'lib/xliff/entry.rb', line 95

def self.validate_source_xml(xml)
  raise 'Entry XML is nil' if xml.nil?
  raise "Invalid Entry XML – must be a nokogiri object, got `#{xml.class}`" unless xml.is_a? Nokogiri::XML::Element
  raise 'Invalid Entry XML – the root node must be `<trans-unit>`' if xml.name != 'trans-unit'
end

Instance Method Details

#to_sString

Encode this ‘Entry` object to an XML string

Returns:

  • (String)


71
72
73
# File 'lib/xliff/entry.rb', line 71

def to_s
  to_xml.to_s.strip
end

#to_xmlNokogiri::XML::Element

Encode this ‘Entry` object to an Nokogiri XML Element Representation of a `<trans-unit>` element



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/xliff/entry.rb', line 52

def to_xml
  fragment = Nokogiri::XML.fragment('<trans-unit />')
  trans_unit_node = fragment.at('trans-unit')
  trans_unit_node['id'] = @id
  trans_unit_node['xml:space'] = @xml_space

  trans_unit_node.add_leaf_node(element: 'source', content: @source)
  trans_unit_node.add_leaf_node(element: 'target', content: @target)

  return trans_unit_node if @note.nil?

  trans_unit_node.add_leaf_node(element: 'note', content: @note)

  trans_unit_node
end