Class: Xliff::Entry
- Inherits:
-
Object
- Object
- Xliff::Entry
- Defined in:
- lib/xliff/entry.rb
Overview
Models a single translation string
Instance Attribute Summary collapse
-
#id ⇒ String
A unique identifier for this translation string.
-
#note ⇒ String
Documentation for translators understand the context of a string.
-
#source ⇒ String
The original text.
-
#target ⇒ String
The translated text.
-
#xml_space ⇒ String
The XML whitespace processing behaviour.
Class Method Summary collapse
-
.from_xml(xml) ⇒ Entry?
Decode the given XML into an ‘Entry` object, if possible.
-
.validate_source_xml(xml) ⇒ void
Validate the given XML to ensure that it’s a valid ‘<trans-unit>` element.
Instance Method Summary collapse
-
#initialize(id:, source:, target:, note: nil, xml_space: 'default') ⇒ Entry
constructor
Create a blank Entry object.
-
#to_s ⇒ String
Encode this ‘Entry` object to an XML string.
-
#to_xml ⇒ Nokogiri::XML::Element
Encode this ‘Entry` object to an Nokogiri XML Element Representation of a `<trans-unit>` element.
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.
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
#id ⇒ String
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.
14 15 16 |
# File 'lib/xliff/entry.rb', line 14 def id @id end |
#note ⇒ String
Documentation for translators understand the context of a string
26 27 28 |
# File 'lib/xliff/entry.rb', line 26 def note @note end |
#source ⇒ String
The original text
18 19 20 |
# File 'lib/xliff/entry.rb', line 18 def source @source end |
#target ⇒ String
The translated text
22 23 24 |
# File 'lib/xliff/entry.rb', line 22 def target @target end |
#xml_space ⇒ String
The XML whitespace processing behaviour
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
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_s ⇒ String
Encode this ‘Entry` object to an XML string
71 72 73 |
# File 'lib/xliff/entry.rb', line 71 def to_s to_xml.to_s.strip end |
#to_xml ⇒ Nokogiri::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 |