Class: Xliff::Bundle

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

Overview

Models a collection of files for translation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Bundle

Create a blank Xliff::Bundle object, suitable for building an XLIFF file by hand

Examples:

Create an empty XLIFF bundle

bundle.new

Create an empty XLIFF bundle with a pre-specified path

bundle.new(path: /path/to/my/output/file.xliff)

Parameters:

  • path (String) (defaults to: nil)

    An optional path to where the file should be stored on disk.



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

def initialize(path: nil)
  @path = path
  @files = []
end

Instance Attribute Details

#filesArray<File> (readonly)

An array of translated files in this bundle

Examples:

Retrieve the bundle’s files

"bundle.files" #=> [{File}]

Returns:



13
14
15
# File 'lib/xliff/bundle.rb', line 13

def files
  @files
end

#pathString

The path on disk that this bundle was read from

Examples:

Retrieve the bundle path

"bundle.path" #=> /tmp/foo.xliff

Returns:

  • (String)


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

Parameters:

  • path (String)

    The path to an ‘xliff` file.

Returns:



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

Parameters:

  • string (String)

    A string containing XLIFF data.

Returns:



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

Parameters:

Returns:



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

Examples:

Add a new file to the bundle

file = File.new(...)
bundle.add_file(file)

Parameters:

  • file (File)

    The file to be stored in the bundle.

Returns:



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.

Examples:

Look up an existing file

# Bundle contains two files: [foo.txt, bar.txt]
bundle.file_named('foo.txt') => {File}

Look up a non-existent file

# Bundle contains two files: [foo.txt, bar.txt]
bundle.file_named('baz.txt') => nil

Parameters:

  • name (String)

    The name of the file to locate. If found it is returned.

Returns:

  • (File, nil)

    The file, if found.



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_sString

Encode this Xliff::Bundle object as an XLIFF document string

Returns:

  • (String)


88
89
90
# File 'lib/xliff/bundle.rb', line 88

def to_s
  to_xml.to_s.strip
end

#to_xmlNokogiri::XML::Document

Encode this Xliff::Bundle object as an XLIFF document

Returns:

  • (Nokogiri::XML::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