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_collection(collection) ⇒ Translatomatic::TMX::Document
Create a TMX document from a translation collection.
-
.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.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
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
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
56 57 58 59 60 |
# File 'lib/translatomatic/tmx/document.rb', line 56 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 |
# 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(tmx_header) xml.body { tmx_body(xml) } end end builder.to_xml end |