Class: I18nKit::Importer::ExcelXML

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_kit/importer/excel_xml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_or_string, opts = {}) ⇒ ExcelXML

Returns a new instance of ExcelXML.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/i18n_kit/importer/excel_xml.rb', line 10

def initialize(path_or_string, opts={})
  @path_or_string = path_or_string
  @locales = opts.fetch(:locales, [])
  @ignore_headline = opts.fetch(:ignore_headline, false)
  @nokodoc = if File.file?(@path_or_string)
    Nokogiri::XML(File.open(@path_or_string))
  else
    Nokogiri::XML(@path_or_string)
  end
  @result = {}
  self
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



8
9
10
# File 'lib/i18n_kit/importer/excel_xml.rb', line 8

def hash
  @hash
end

#ignore_headlineObject

Returns the value of attribute ignore_headline.



7
8
9
# File 'lib/i18n_kit/importer/excel_xml.rb', line 7

def ignore_headline
  @ignore_headline
end

#localesObject

Returns the value of attribute locales.



7
8
9
# File 'lib/i18n_kit/importer/excel_xml.rb', line 7

def locales
  @locales
end

#nokodocObject

Returns the value of attribute nokodoc.



7
8
9
# File 'lib/i18n_kit/importer/excel_xml.rb', line 7

def nokodoc
  @nokodoc
end

#path_or_stringObject (readonly)

Returns the value of attribute path_or_string.



8
9
10
# File 'lib/i18n_kit/importer/excel_xml.rb', line 8

def path_or_string
  @path_or_string
end

Instance Method Details

#parseObject

This method uses Nokogiri to parse the XML data



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/i18n_kit/importer/excel_xml.rb', line 24

def parse
  # First, we're loading all Rows of the sheet
  @nokodoc.css('Row').each_with_index do |row, row_index|
    # In each row, we load the columns (i.e. row cells)
    columns = row.css('Cell') 
    
    # We skip empty rows (i.e. rows with no or just one column)
    next unless columns.size > 1
    # The first row might be a header column containing the names of the columns
    # In case the locales are unknown and we're in the header row, let's derive the locales from this header row
    next if @ignore_headline && row_index == 0
    if @locales == [] && row_index == 0
      # Going through the cells of the header row
      columns.each_with_index do |column, column_index|
        next if column.children[1] == nil
        # The first column is expected to contain the key, all others we interpret as locale name
        @locales << column.text.to_s.strip.to_sym unless column_index == 0
      end
      #raise @locales.inspect
    else
      # This is every row except the header row
      # The key is expected to be in the first column
      key = columns[0].text.to_s.strip
      # Now let's read the translations for this key. I.e. all columns except the first one
      locales.each_with_index do |locale, locale_index|
        locale = locale.to_s
        @result[locale] ||= {}
        @result[locale][key] = (columns[locale_index+1] == nil ? '' : columns[locale_index+1].text.to_s.strip)
      end
    end
  end

  documents = []
  @result.each { |locale, hash|
    document = ::I18nKit::Document.new(:locale => locale, :flat_hash => hash, :name => "excel_#{locale}".to_sym)
    documents << document
  }
  documents
end