Class: XliffTransWriter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, file, data) ⇒ XliffTransWriter

Returns a new instance of XliffTransWriter.



9
10
11
12
13
14
# File 'lib/transync/xliff_trans/xliff_trans_writer.rb', line 9

def initialize(path, file, data)
  @path = path
  @file = file
  @language = data[:language]
  @data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#languageObject

Returns the value of attribute language.



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

def language
  @language
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#saveObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/transync/xliff_trans/xliff_trans_writer.rb', line 16

def save
  translations = data[: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.each do |trans|
          body.tag! 'trans-unit', :id => trans[:key] do |trans_unit|
            trans_unit.source trans[:key]
            trans_unit.target trans[:value]
          end
        end

      end
    end
  end

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