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.kind_of?(Array)
  @units = units
  @source_locale = source_locale
end

Class Method Details

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

Create a TMX document from the given converter

Parameters:

Returns:



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

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

  return new(units, source_locales[0])
end

.valid?(xml) ⇒ Boolean

Returns:

  • (Boolean)


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

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:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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(creationtool: "Translatomatic",
        creationtoolversion: Translatomatic::VERSION,
        datatype: "PlainText",
        segtype: "phrase",  # default segtype
        adminlang: @source_locale.to_s,
        srclang: @source_locale.to_s,
        "o-tmf": DEFAULT_OTMF
      )
      xml.body { tmx_body(xml) }
    end
  end
  builder.to_xml
end