class I18nAdmin::Import::XLS

Constants

PAGINATION_PATTERN

Used to split cells with large content

Attributes

file[R]
sheet[R]
spreadsheet[R]

Public Class Methods

import(locale, file) click to toggle source
# File lib/i18n_admin/import/xls.rb, line 11
def self.import(locale, file)
  import = new(locale, file)
  import.run
ensure
  file.close
end
new(locale, file) click to toggle source
# File lib/i18n_admin/import/xls.rb, line 18
def initialize(locale, file)
  @locale = locale
  @spreadsheet = Spreadsheet.open(file.path)
  @sheet = spreadsheet.worksheet(0)
end

Public Instance Methods

run() click to toggle source
# File lib/i18n_admin/import/xls.rb, line 24
def run
  I18n.with_locale(locale) do
    index = 0

    while (index += 1) < sheet.row_count
      key, value, index = extract_translation_at(index)
      update_translation(key, value)
    end

    save_updated_models
  end

  errors.empty?
end

Private Instance Methods

extract_translation_at(index) click to toggle source
# File lib/i18n_admin/import/xls.rb, line 41
def extract_translation_at(index)
  key, _, value = sheet.row(index)

  if (pagination = key.match(PAGINATION_PATTERN))
    pages = pagination[1].to_i
    key = key.gsub(PAGINATION_PATTERN, '').strip

    (pages - 1).times do |page|
      index += 1
      value += sheet.row(index).pop.to_s
    end
  end

  [key, value, index]
end