Class: XliffToGdoc

Inherits:
Object
  • Object
show all
Defined in:
lib/transync/sync/xliff_to_gdoc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ XliffToGdoc

Returns a new instance of XliffToGdoc.



11
12
13
14
15
16
17
# File 'lib/transync/sync/xliff_to_gdoc.rb', line 11

def initialize(options = {})
  self.xliff_translations = options[:xliff_translations]
  self.gdoc_trans_reader = options[:gdoc_trans_reader]
  self.gdoc_trans_writer = options[:gdoc_trans_writer]
  self.language = options[:language]
  self.languages = options[:languages]
end

Instance Attribute Details

#gdoc_trans_readerObject

Returns the value of attribute gdoc_trans_reader.



4
5
6
# File 'lib/transync/sync/xliff_to_gdoc.rb', line 4

def gdoc_trans_reader
  @gdoc_trans_reader
end

#gdoc_trans_writerObject

Returns the value of attribute gdoc_trans_writer.



4
5
6
# File 'lib/transync/sync/xliff_to_gdoc.rb', line 4

def gdoc_trans_writer
  @gdoc_trans_writer
end

#languageObject

Returns the value of attribute language.



4
5
6
# File 'lib/transync/sync/xliff_to_gdoc.rb', line 4

def language
  @language
end

#languagesObject

Returns the value of attribute languages.



4
5
6
# File 'lib/transync/sync/xliff_to_gdoc.rb', line 4

def languages
  @languages
end

#loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/transync/sync/xliff_to_gdoc.rb', line 4

def logger
  @logger
end

#xliff_translationsObject

Returns the value of attribute xliff_translations.



4
5
6
# File 'lib/transync/sync/xliff_to_gdoc.rb', line 4

def xliff_translations
  @xliff_translations
end

Instance Method Details

#syncObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/transync/sync/xliff_to_gdoc.rb', line 19

def sync
  gdoc_tab_language = gdoc_trans_reader.build_trans_hash(language)
  dirty = false
  file = gdoc_tab_language[:title]

  xliff_for_language = xliff_translations.detect{ |x| x[:language] == language }
  xliff_for_language[:translations].each_with_index do |x_trans, index|

    # for current xliff translation find the same trans key in google doc
    gdoc_trans = gdoc_tab_language[:translations].detect{ |g_trans| g_trans[:key] == x_trans[:key] }
    current_row = index + GdocTrans::START_ROW

    # whole key is missing
    if gdoc_trans.nil?
      # heavy coupling
      gdoc_trans_reader.worksheet = gdoc_trans_writer.shift_up(current_row, xliff_for_language[:translations].length)
      # recalculate hashes after shift
      gdoc_tab_language = gdoc_trans_reader.build_trans_hash(language)

      gdoc_trans_writer.write(current_row, 'key',    x_trans[:key])
      gdoc_trans_writer.write(current_row, language, x_trans[:value])

      SyncUtil.info_diff(file, language, 'Adding', x_trans)

      # Go for all other languages if key was missing
      languages.each do |key_lang|
        next if key_lang == language

        xliff_lang = xliff_translations.detect{ |xt| xt[:language] == key_lang }
        xliff_lang_value = xliff_lang[:translations].detect{ |xt| xt[:key] == x_trans[:key] }

        gdoc_trans_writer.write(current_row, key_lang, xliff_lang_value[:value])
      end

      dirty = true
    elsif gdoc_trans[:value] != x_trans[:value]
      gdoc_trans_writer.write(current_row, language, x_trans[:value])
      SyncUtil.info_diff(file, language, 'Changing', x_trans)
      dirty = true
    end
  end

  dirty
end