Class: Translatomatic::TMX::Document

Inherits:
Object
  • Object
show all
Extended by:
Util
Defined in:
lib/translatomatic/tmx/document.rb

Overview

Translation Memory Exchange document

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(units, source_locale) ⇒ Translatomatic::TMX::Document

Create a new instance

Parameters:

  • units (Array<TranslationUnit>)

    A list of translation units

  • source_locale (Locale)

    Source locale



9
10
11
12
13
# File 'lib/translatomatic/tmx/document.rb', line 9

def initialize(units, source_locale)
  units = [units] unless units.is_a?(Array)
  @units = units
  @source_locale = source_locale
end

Class Method Details

.from_collection(collection) ⇒ Translatomatic::TMX::Document

Create a TMX document from a translation collection

Parameters:

Returns:



47
48
49
50
51
52
53
54
# File 'lib/translatomatic/tmx/document.rb', line 47

def self.from_collection(collection)
  originals = collection.translations.collect(&:original)
  source_locales = originals.collect(&:locale)
  raise t('tmx.multiple_locales') if source_locales.length > 1
  units = units_from_collection(collection)

  new(units, source_locales[0])
end

.from_texts(texts) ⇒ Translatomatic::TMX::Document

Create a TMX document from the given converter

Parameters:

Returns:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/translatomatic/tmx/document.rb', line 31

def self.from_texts(texts)
  # group texts by from_text_id to create units
  # source_locale: use from_text.locale
  # origin: use text.provider
  sources = texts.select { |i| i.from_text.nil? }
  source_locales = sources.collect(&:locale).uniq
  raise t('tmx.multiple_locales') if source_locales.length > 1
  units = units_from_texts(texts)

  new(units, source_locales[0])
end

.valid?(xml) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/translatomatic/tmx/document.rb', line 56

def self.valid?(xml)
  options = Nokogiri::XML::ParseOptions::DTDVALID
  doc = Nokogiri::XML::Document.parse(xml, nil, nil, options)
  doc.internal_subset.validate(doc)
end

Instance Method Details

#to_xml(options = {}) ⇒ String

Returns An XML string.

Returns:

  • (String)

    An XML string



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/translatomatic/tmx/document.rb', line 16

def to_xml(options = {})
  builder = Nokogiri::XML::Builder.new do |xml|
    dtd = options[:dtd] || TMX_DTD
    xml.doc.create_internal_subset('tmx', nil, dtd)
    xml.tmx(version: '1.4') do
      xml.header(tmx_header)
      xml.body { tmx_body(xml) }
    end
  end
  builder.to_xml
end