Class: Xliff::Bundle
- Inherits:
-
Object
- Object
- Xliff::Bundle
- Defined in:
- lib/xliff/bundle.rb
Overview
Models a collection of files for translation
Instance Attribute Summary collapse
-
#files ⇒ Array<File>
readonly
An array of translated files in this bundle.
-
#path ⇒ String
The path on disk that this bundle was read from.
Class Method Summary collapse
-
.from_path(path) ⇒ Bundle?
Parse the XLIFF file at the given ‘path` as an XLIFF Bundle object.
-
.from_string(string) ⇒ Xliff::Bundle?
Parse the XLIFF file stored in the given ‘string` as an XLIFF Bundle object.
-
.from_xml(xml) ⇒ Bundle?
Parse the Nokogiri XML representation of an XLIFF file to a Bundle object.
Instance Method Summary collapse
-
#add_file(file) ⇒ Object
Add an additional File object to the bundle.
-
#file_named(name) ⇒ File?
Find a given file by name.
-
#initialize(path: nil) ⇒ Bundle
constructor
Create a blank Bundle object, suitable for building an XLIFF file by hand.
-
#to_s ⇒ String
Encode this Bundle object as an XLIFF document string.
-
#to_xml ⇒ Nokogiri::XML::Document
Encode this Bundle object as an XLIFF document.
Constructor Details
#initialize(path: nil) ⇒ Bundle
Create a blank Xliff::Bundle object, suitable for building an XLIFF file by hand
30 31 32 33 |
# File 'lib/xliff/bundle.rb', line 30 def initialize(path: nil) @path = path @files = [] end |
Instance Attribute Details
#files ⇒ Array<File> (readonly)
An array of translated files in this bundle
13 14 15 |
# File 'lib/xliff/bundle.rb', line 13 def files @files end |
#path ⇒ String
The path on disk that this bundle was read from
21 22 23 |
# File 'lib/xliff/bundle.rb', line 21 def path @path end |
Class Method Details
.from_path(path) ⇒ Bundle?
Parse the XLIFF file at the given ‘path` as an XLIFF Xliff::Bundle object
Raises for invalid input
98 99 100 101 102 103 104 |
# File 'lib/xliff/bundle.rb', line 98 def self.from_path(path) xml = Nokogiri::XML(::File.open(path)) bundle = from_xml(xml) bundle.path = path bundle end |
.from_string(string) ⇒ Xliff::Bundle?
Parse the XLIFF file stored in the given ‘string` as an XLIFF Xliff::Bundle object
Raises for invalid input
112 113 114 115 |
# File 'lib/xliff/bundle.rb', line 112 def self.from_string(string) xml = Nokogiri::XML(string) from_xml(xml) end |
.from_xml(xml) ⇒ Bundle?
Parse the Nokogiri XML representation of an XLIFF file to a Xliff::Bundle object
Raises for invalid input
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/xliff/bundle.rb', line 123 def self.from_xml(xml) raise if xml.nil? raise 'Invalid XLIFF file – the root node must be `<xliff>`' if xml.document.root.name != 'xliff' bundle = Bundle.new xml.document.root.element_children .select { |node| node.name == 'file' } .each { |node| bundle.add_file File.from_xml(node) } bundle end |
Instance Method Details
#add_file(file) ⇒ Object
Add an additional File object to the bundle
42 43 44 |
# File 'lib/xliff/bundle.rb', line 42 def add_file(file) @files << file end |
#file_named(name) ⇒ File?
Find a given file by name
If two files exist with the same name, only the first will be returned.
58 59 60 61 62 |
# File 'lib/xliff/bundle.rb', line 58 def file_named(name) @files.find do |file| file.original == name || ::File.basename(f.original) == name end end |
#to_s ⇒ String
Encode this Xliff::Bundle object as an XLIFF document string
88 89 90 |
# File 'lib/xliff/bundle.rb', line 88 def to_s to_xml.to_s.strip end |
#to_xml ⇒ Nokogiri::XML::Document
Encode this Xliff::Bundle object as an XLIFF document
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/xliff/bundle.rb', line 67 def to_xml document = Nokogiri::XML::Document.new document.encoding = 'UTF-8' xliff_node = document.create_element('xliff') (xliff_node) return document if @files.empty? @files.each do |file| xliff_node.add_child(file.to_xml) end document.add_child(xliff_node) document end |