Class: Translatomatic::TMX::Document
- Inherits:
-
Object
- Object
- Translatomatic::TMX::Document
- Extended by:
- Util
- Defined in:
- lib/translatomatic/tmx/document.rb
Overview
Translation Memory Exchange document
Class Method Summary collapse
-
.from_texts(texts) ⇒ Translatomatic::TMX::Document
Create a TMX document from the given converter.
- .valid?(xml) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(units, source_locale) ⇒ Translatomatic::TMX::Document
constructor
Create a new instance.
-
#to_xml(options = {}) ⇒ String
An XML string.
Constructor Details
#initialize(units, source_locale) ⇒ Translatomatic::TMX::Document
Create a new instance
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
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
50 51 52 53 54 |
# File 'lib/translatomatic/tmx/document.rb', line 50 def self.valid?(xml) = Nokogiri::XML::ParseOptions::DTDVALID doc = Nokogiri::XML::Document.parse(xml, nil, nil, ) doc.internal_subset.validate(doc) end |
Instance Method Details
#to_xml(options = {}) ⇒ String
Returns An XML string.
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( = {}) builder = Nokogiri::XML::Builder.new do |xml| dtd = [: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 |