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

Methods included from Util

locale, log, string

Constructor Details

#initialize(units, source_locale, origin) ⇒ Object

Create a new instance

Parameters:



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

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

Class Method Details

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

Create a TMX document from the given converter

Parameters:

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/translatomatic/tmx/document.rb', line 39

def self.from_texts(texts)
  # group texts by from_text_id to create units
  # source_locale: use from_text.locale
  # origin: use text.translator
  origins = texts.collect { |i| i.translator }.compact.uniq
  raise "Multiple origins in texts" if origins.length > 1
  sources = texts.select { |i| i.from_text.nil? }
  source_locales = sources.collect { |i| i.locale }.uniq
  raise "Multiple source locales in texts" if source_locales.length > 1
  units = units_from_texts(texts)

  return new(units, source_locales[0], origins[0])
end

.valid?(xml) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/translatomatic/tmx/document.rb', line 53

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:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/translatomatic/tmx/document.rb', line 17

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(creationtool: "Translatomatic",
        creationtoolversion: Translatomatic::VERSION,
        datatype: "PlainText",
        segtype: "phrase",  # default segtype
        adminlang: @source_locale.to_s,
        srclang: @source_locale.to_s,
        "o-tmx": @origin
      )
      xml.body { tmx_body(xml) }
    end
  end
  builder.to_xml
end