class I18nAdmin::Export::XLS

Constants

PAGE_LENGTH

Attributes

export_file[RW]
sheet[R]
spreadsheet[R]

Public Instance Methods

file_path() click to toggle source
# File lib/i18n_admin/export/xls.rb, line 65
def file_path
  @file_path ||= [
    '',
    'tmp',
    I18n.t('i18n_admin.exports.file.name', time: Time.now.strftime('%Y_%m_%d-%H_%M'), lang: locale, ext: 'xls')
  ].join('/')
end
monitoring_data(_, state) click to toggle source
# File lib/i18n_admin/export/xls.rb, line 73
def monitoring_data(_, state)
  { url: export_file.file.url } if state == 'complete'
end
perform(locale) click to toggle source
# File lib/i18n_admin/export/xls.rb, line 11
def perform(locale)
  @locale = locale
  @spreadsheet = Spreadsheet::Workbook.new
  @sheet = spreadsheet.create_worksheet

  add_headers!

  run
  save
end
run() click to toggle source
# File lib/i18n_admin/export/xls.rb, line 22
def run
  default_format = Spreadsheet::Format.new(text_wrap: true)

  index = 0

  translations.each do |key, value|
    value = value.to_s
    original_translation = original_translations[key].to_s
    max_length = [value.length, original_translation.length].max
    pages = (max_length / PAGE_LENGTH).ceil

    pages.times do |page|
      index += 1

      translation_key = if pages > 1
        "#{ key } (#{ page + 1 } / #{ pages })"
      else
        key
      end

      start_offset = page * PAGE_LENGTH

      row = sheet.row(index)
      row.default_format = default_format
      row.push(translation_key)
      row.push(original_translation[start_offset, PAGE_LENGTH])
      row.push(value[start_offset, PAGE_LENGTH])
    end
  end
end
save() click to toggle source
# File lib/i18n_admin/export/xls.rb, line 53
def save
  spreadsheet.write(file_path)

  source = File.open(file_path, 'rb')
  self.export_file = ExportFile.create!(job_id: job_id, file: source)
ensure
  if defined?(source) && source
    source.close
    File.unlink(source.path)
  end
end

Private Instance Methods

add_headers!() click to toggle source
# File lib/i18n_admin/export/xls.rb, line 83
def add_headers!
  sheet.row(0).replace(
    [
      I18n.t('i18n_admin.exports.file.columns.key'),
      I18n.t('i18n_admin.exports.file.columns.original', lang: I18n.default_locale),
      I18n.t('i18n_admin.exports.file.columns.translated', lang: locale)
    ]
  )

  format = Spreadsheet::Format.new weight: :bold

  sheet.row(0).default_format = format

  sheet.column(0).width = 30
  sheet.column(1).width = 100
  sheet.column(2).width = 100
end
job_id() click to toggle source
# File lib/i18n_admin/export/xls.rb, line 101
def job_id
  @job_id ||= jid || randomize_job_id
end
randomize_job_id() click to toggle source
# File lib/i18n_admin/export/xls.rb, line 105
def randomize_job_id
  ['sync', (Time.now.to_f * 1000).round, rand(1000)]
end