Class: XliffTransWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/transync/xliff_trans/xliff_trans_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, file) ⇒ XliffTransWriter

Returns a new instance of XliffTransWriter.



5
6
7
8
# File 'lib/transync/xliff_trans/xliff_trans_writer.rb', line 5

def initialize(path, file)
  @path = path
  @file = file
end

Instance Method Details

#write(trans_hash) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/transync/xliff_trans/xliff_trans_writer.rb', line 10

def write(trans_hash)
  language     = trans_hash[:language]
  translations = trans_hash[:translations]

  xml = Builder::XmlMarkup.new( :indent => 4 )
  xml.instruct! :xml, :encoding => 'UTF-8'
  xml.xliff :version => '1.2', :xmlns => 'urn:oasis:names:tc:xliff:document:1.2' do |xliff|
    xliff.file :'source-language' => language, :datatype => 'plaintext', :original => 'file.ext' do |file|
      file.body do |body|

        translations.keys.each do |trans_key|
          body.tag! 'trans-unit', :id => trans_key do |trans_unit|
            trans_unit.source trans_key
            trans_unit.target translations[trans_key]
          end
        end

      end
    end
  end

  File.open(file_path(language), 'w') { |file| file.write(xml.target!) }
end