module I18n::Backend::Weeler::Importer::ClassMethods
Public Instance Methods
import(file)
click to toggle source
Loads file and iterates each sheet and row.
# File lib/i18n/backend/weeler/importer.rb, line 27 def import file xls = open_spreadsheet file xls.each_with_pagename do |name, sheet| # Lookup locales locales = locales_from_xlsx_sheet_row(sheet.row(1)) # Lookup values (2..sheet.last_row).each do |row_no| store_translations_from_xlsx_row(sheet.row(row_no), locales) end # rows end # sheets end
Private Instance Methods
locales_from_xlsx_sheet_row(row)
click to toggle source
Lookup locales and sequence for loading
# File lib/i18n/backend/weeler/importer.rb, line 57 def locales_from_xlsx_sheet_row row locales = [] row.each_with_index do |cell, i| if i > 0 locales.push(cell.downcase) end end locales end
open_spreadsheet(file)
click to toggle source
Open csv, xls, xlsx or ods file and read content
# File lib/i18n/backend/weeler/importer.rb, line 46 def open_spreadsheet(file) case File.extname(file.original_filename) when ".csv" then Roo::Csv.new(file.path, file_warning: :ignore) when ".xls" then Roo::Excel.new(file.path, file_warning: :ignore) when ".xlsx" then Roo::Excelx.new(file.path, file_warning: :ignore) when ".ods" then Roo::OpenOffice.new(file.path, file_warning: :ignore) else raise "Unknown file type: #{file.original_filename}" end end
store_translation_from_xlsx_cell(locale, key, cell)
click to toggle source
Store locale if locale and key present
# File lib/i18n/backend/weeler/importer.rb, line 84 def store_translation_from_xlsx_cell locale, key, cell value = cell.nil? ? '' : cell if locale.present? && key.present? translation = Translation.find_or_initialize_by locale: locale, key: key if translation.value != value translation.value = value translation.save end end end
store_translations_from_xlsx_row(row, locales)
click to toggle source
Iterate each cell in row and store translation by locale
# File lib/i18n/backend/weeler/importer.rb, line 68 def store_translations_from_xlsx_row row, locales locale = nil key = nil value = nil row.each_with_index do |cell, i| if i == 0 key = cell else locale = locales[ i - 1 ] store_translation_from_xlsx_cell locale, key, cell end end end