Class: Releaf::I18nDatabase::ParseSpreadsheetTranslations

Inherits:
Object
  • Object
show all
Includes:
Service
Defined in:
app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb

Defined Under Namespace

Classes: UnsupportedFileFormatError

Instance Method Summary collapse

Instance Method Details

#callObject



8
9
10
# File 'app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb', line 8

def call
  translations
end

#data_rowsObject



16
17
18
# File 'app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb', line 16

def data_rows
  rows[1..-1].reject{|row| row.first.blank? }
end

#file_format_error?(error_class_name, error_message) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb', line 36

def file_format_error?(error_class_name, error_message)
  return true if ['Zip::ZipError','Ole::Storage::FormatError' ].include?(error_class_name)
  error_class_name == 'ArgumentError' && error_message.match("Don't know how to open file").present?
end

#localesObject



20
21
22
# File 'app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb', line 20

def locales
  @locales ||= rows.first.reject(&:blank?)
end

#maintain_translation_locales(translation, localizations) ⇒ Object



54
55
56
57
58
59
60
# File 'app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb', line 54

def maintain_translation_locales(translation, localizations)
  locales.each_with_index do|locale, i|
    locale_translation = translation.i18n_entry_translation.find{|item| item.locale == locale }
    locale_translation ||= translation.i18n_entry_translation.build(locale: locale, text: "")
    locale_translation.text = localizations[i] if localizations[i].present?
  end
end

#rowsObject



12
13
14
# File 'app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb', line 12

def rows
  @rows ||= spreadsheet.to_a
end

#spreadsheetObject



24
25
26
27
28
29
30
31
32
33
34
# File 'app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb', line 24

def spreadsheet
  begin
    Roo::Spreadsheet.open(file_path, extension: extension, file_warning: :ignore)
  rescue StandardError => e
    if file_format_error?(e.class.name, e.message)
      raise UnsupportedFileFormatError
    else
      raise
    end
  end
end

#translation_instance(key, localizations) ⇒ Object



47
48
49
50
51
52
# File 'app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb', line 47

def translation_instance(key, localizations)
  translation = Releaf::I18nDatabase::I18nEntry.where(key: key).first_or_initialize
  maintain_translation_locales(translation, localizations)

  translation
end

#translationsObject



41
42
43
44
45
# File 'app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb', line 41

def translations
  data_rows.map do |row|
    translation_instance(row.first, row[1..-1].map(&:to_s))
  end
end