Class: File_util

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-localzedLoader/File_util.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keys_hashObject

Returns the value of attribute keys_hash.



10
11
12
# File 'lib/cocoapods-localzedLoader/File_util.rb', line 10

def keys_hash
  @keys_hash
end

Instance Method Details

#getkeyHash(row1, ios: true) ⇒ Object

根据第一行来获取当前cell的key return File_util.1:selfkey,2:en,3:zh1:selfkey,2:en,3:zh.… 方便excel取Command+Option+L 对齐快捷键



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cocoapods-localzedLoader/File_util.rb', line 49

def getkeyHash(row1,ios:true)
  keys_hash = {}
  key = ''
  row1.cells.each_with_index do |cell, i|
    next unless cell.value && i > 3
    if cell.value === "zh-CN"
      key = 'zh-Hans'
    elsif cell.value == "zh-TW"
      key = 'zh-Hant-TW'
    elsif cell.value == "zh-HK"
      key = 'zh-HK'
    else
      key = cell.value.split("-")[0]
    end
    # result = cell.value.scan(/.*?\[(.*)\]/)
    keys_hash[i] = key
  end
  keys_hash
end

#getLangListObject



69
70
71
72
73
# File 'lib/cocoapods-localzedLoader/File_util.rb', line 69

def getLangList
  list = @key_hash.values
  list.delete_if{|item| item.downcase === "keys"||item.downcase === "source" || item.downcase === "length limit" || item.downcase === "context"}
  list
end

#read_excel(filename) ⇒ Object

读取excel 返回StringElement数组



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cocoapods-localzedLoader/File_util.rb', line 12

def read_excel(filename)
  unless filename != nil
    puts "读取的excel为空"
    return
  end

  workbook = RubyXL::Parser.parse filename
  worksheet = workbook[0]
  row1 = worksheet[0]
  @key_hash = getkeyHash(row1)
  lang_hash = {}
  worksheet.each_with_index do |row,rowIndex|
    next unless rowIndex>0
    ele = StringElement.new
    self_key = ''
    # 设置StringElement的值
    row.cells.each_with_index do |cell, i|
      next unless cell.value
      case i
      when 0
        self_key = cell.value
      else
        key = @key_hash[i]
        if key
          ele.langHash[key] = (cell.value || '')
        end
      end
    end

    lang_hash[self_key] = ele
  end
  lang_hash
end