Class: TranslationSync

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

Instance Method Summary collapse

Constructor Details

#initialize(path, direction, file = nil) ⇒ TranslationSync



8
9
10
11
12
13
# File 'lib/transync/sync/translation_sync.rb', line 8

def initialize(path, direction, file = nil)
  @path   = path
  @file   = file
  @config = TransyncConfig::CONFIG
  SyncUtil.create_logger(direction)
end

Instance Method Details

#diff(language) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/transync/sync/translation_sync.rb', line 58

def diff(language)
  gdoc_trans_reader  = GdocTransReader.new(@file)
  xliff_trans_reader = XliffTransReader.new(@path, @file, nil)

  g_trans_hash = gdoc_trans_reader.translations(language)
  x_trans_hash = xliff_trans_reader.translations(language)

  diff = x_trans_hash[:translations].diff(g_trans_hash[:translations])
  SyncUtil.info_diff(@file, language, diff, true)
  diff
end

#run(direction) ⇒ Object



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

def run(direction)
  @config['FILES'].each do |file|
    xliff_files = XliffTransReader.new(@path, file, @config['LANGUAGES'])
    abort('Fix your Xliff translations by hand or run transync update!') unless xliff_files.valid?

    gdoc_trans_reader  = GdocTransReader.new(file)
    gdoc_trans_writer  = GdocTransWriter.new(gdoc_trans_reader.worksheet)
    xliff_trans_writer = XliffTransWriter.new(@path, file)

    @config['LANGUAGES'].each do |language|
      trans_sync = TranslationSync.new(@path, direction, file)
      trans_hash = trans_sync.sync(language, direction)

      if direction == 'x2g'
        gdoc_trans_writer.write(trans_hash)
      else
        xliff_trans_writer.write(trans_hash)
      end
    end
  end
end

#sync(language, direction) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/transync/sync/translation_sync.rb', line 37

def sync(language, direction)
  gdoc_trans_reader  = GdocTransReader.new(@file)
  xliff_trans_reader = XliffTransReader.new(@path, @file, nil) # we dont need languages for translations method

  g_trans_hash = gdoc_trans_reader.translations(language)
  x_trans_hash = xliff_trans_reader.translations(language)

  # We need to merge on translations hash, not whole hash since it will only merge first level
  if direction == 'x2g'
    merged_translations = g_trans_hash[:translations].merge(x_trans_hash[:translations])
    diff = x_trans_hash[:translations].diff(g_trans_hash[:translations])
    SyncUtil.info_diff(@file, language, diff)
  else
    merged_translations = x_trans_hash[:translations].merge(g_trans_hash[:translations])
    diff = g_trans_hash[:translations].diff(x_trans_hash[:translations])
    SyncUtil.info_diff(@file, language, diff)
  end

  {file: @file, language: language, translations: merged_translations}
end