Sha256: 2831c7de91b30a8f94783167e6536fea4a9bc3910c11469bcb530148d66f739a

Contents?: true

Size: 1.77 KB

Versions: 31

Compression:

Stored size: 1.77 KB

Contents

module Releaf::I18nDatabase
  class ParseSpreadsheetTranslations
    class UnsupportedFileFormatError < StandardError; end
    include Releaf::Service
    attribute :file_path, String
    attribute :extension, String

    def call
      translations
    end

    def rows
      @rows ||= spreadsheet.to_a
    end

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

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

    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

    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

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

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

      translation
    end

    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
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
releaf-i18n_database-1.1.22 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.21 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.20 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.19 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.18 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.17 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.16 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.15 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.14 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.13 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.12 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.11 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.10 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.9 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.8 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.7 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.6 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.5 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.4 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb
releaf-i18n_database-1.1.3 app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb