Class: I18n::Migrations::Backends::GoogleSpreadsheetsBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/migrations/backends/google_spreadsheets_backend.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GoogleSpreadsheetsBackend

Returns a new instance of GoogleSpreadsheetsBackend.



7
8
9
# File 'lib/i18n/migrations/backends/google_spreadsheets_backend.rb', line 7

def initialize(config)
  @config = config
end

Instance Method Details

#pull(locale) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/i18n/migrations/backends/google_spreadsheets_backend.rb', line 11

def pull(locale)
  return if locale.main_locale?

  sheet = get_google_spreadsheet(locale.name)
  pull_from_sheet(sheet, locale)
  locale.migrate!
end

#pull_from_sheet(sheet, locale) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/i18n/migrations/backends/google_spreadsheets_backend.rb', line 36

def pull_from_sheet(sheet, locale)
  puts "Pulling #{locale.name}"
  data = {}
  notes = {}
  count = 0

  (2..sheet.num_rows).each do |row|
    key, value, note = sheet[row, 1], sheet[row, 3], sheet[row, 4]
    if key.present?
      locale.assign_complex_key(data, key.split('.'), value.present? ? value : '')
      if note.present?
        locale.assign_complex_key(notes, key.split('.'), note)
      end
      count += 1
      print '.'
    end
  end

  locale.write_data_and_notes(data, notes)
  locale.write_remote_version(data)

  puts "\n#{count} keys"
end

#push(locale, force = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/i18n/migrations/backends/google_spreadsheets_backend.rb', line 19

def push(locale, force = false)
  return if locale.main_locale?

  sheet = get_google_spreadsheet(locale.name)
  unless force
    pull_from_sheet(sheet, locale)
    locale.migrate!
  end
  push_to_sheet(sheet, locale)
end

#push_to_sheet(sheet, locale) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/i18n/migrations/backends/google_spreadsheets_backend.rb', line 60

def push_to_sheet(sheet, locale)
  main_data = locale.main_locale.read_data
  data, notes = locale.read_data_and_notes
  row = 2

  puts "Pushing #{locale.name}"

  main_data.each do |key, value|
    sheet[row, 1] = key
    sheet[row, 2] = value
    sheet[row, 3] = data[key]
    sheet[row, 4] = notes[key]
    row += 1
    print '.'
  end

  sheet.synchronize
  locale.write_remote_version(data)

  puts "\n#{main_data.keys.length} keys"
end