Module: I18n::Backend::Weeler::Exporter::ClassMethods

Defined in:
lib/i18n/backend/weeler/exporter.rb

Instance Method Summary collapse

Instance Method Details

#as_xlsx_packageObject

Prepare xlsx package from current scope Stores all translations in translations worksheet.



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
# File 'lib/i18n/backend/weeler/exporter.rb', line 30

def as_xlsx_package
  package = Axlsx::Package.new
  package.use_shared_strings = true

  sheet = package.workbook.add_worksheet(name: "translations")

  locales = I18n.available_locales
  sheet.add_row(Translation.title_row(locales))

  types = []
  (locales.size + 1).times { types << :string }

  included_keys = []

  tranlsations_by_locales = Translation.where(locale: locales).group_by(&:locale)

  current_scope.each do |translation|
    next if included_keys.include?(translation.key)

    row = Translation.translation_row_by_key_and_locales(tranlsations_by_locales, translation.key, locales)
    sheet.add_row(row, types: types)
    included_keys << translation.key
  end

  package
end

#title_row(locales) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/i18n/backend/weeler/exporter.rb', line 57

def title_row(locales)
  row = [ 'Key' ]
  locales.each do |locale|
    row.push(locale.capitalize)
  end

  row.push('Created at')
  row.push('Updated at')

  row
end

#translation_row_by_key_and_locales(tranlsations_by_locales, key, locales) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/i18n/backend/weeler/exporter.rb', line 69

def translation_row_by_key_and_locales(tranlsations_by_locales, key, locales)
  row = [key]
  created_ats = []
  updated_ats = []

  locales.each do |locale|
    translation = tranlsations_by_locales[locale.to_s]&.find { |t| t.key == key }

    if translation.present?
      created_ats.push(translation.created_at)
      updated_ats.push(translation.updated_at)

      row.push(translation.value)
    else
      row.push('')
    end
  end

  row.push(created_ats.min)
  row.push(updated_ats.max)

  row
end