Class: Applocale::ConvertToStrFile

Inherits:
Object
  • Object
show all
Defined in:
lib/applocale/Core/convert_to_localefile.rb

Class Method Summary collapse

Class Method Details

.add_escape(platform, lang, key, content, injectObj) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/applocale/Core/convert_to_localefile.rb', line 146

def self.add_escape(platform, lang, key, content, injectObj)
  value = content
  if injectObj.has_before_convent_to_locale
    value = injectObj.load_before_convent_to_locale(lang.to_s, key,  value)
  end
  if injectObj.has_convent_to_locale
    value = injectObj.load_convent_to_locale(lang.to_s, key,  value)
  else
    value = ContentUtil.add_escape(platform, value)
  end
  if injectObj.has_after_convent_to_locale
    value = injectObj.load_after_convent_to_locale(lang.to_s, key,  value)
  end
  return value
end

.convert(platform, lang_path_list, sheetcontent_list, rubycode) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/applocale/Core/convert_to_localefile.rb', line 12

def self.convert(platform, lang_path_list, sheetcontent_list, rubycode)

  injectObj = Applocale::Injection.load(rubycode)

  lang_path_list.each do |langpath_obj|
    puts "Start to convert to string file for [\"#{langpath_obj.lang}\"] #{langpath_obj.filepath}...".green
    if platform == Platform::IOS
      self.convert_to_stringfile(platform, langpath_obj, sheetcontent_list, injectObj)
    elsif platform == Platform::ANDROID
      self.convert_to_xml(platform, langpath_obj, sheetcontent_list, injectObj)
    elsif platform == Platform::JSON
      self.convert_to_json(platform, langpath_obj, sheetcontent_list, injectObj)
    end
  end
  puts 'Convert Finished !!!'.green
end

.convert_to_json(platform, lang_path_obj, sheet_content_list, inject_obj) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/applocale/Core/convert_to_localefile.rb', line 113

def self.convert_to_json(platform, lang_path_obj, sheet_content_list, inject_obj)
  FileUtils.mkdir_p(File.dirname(lang_path_obj.filepath))
  hash = sheet_content_list.map do |sheet_content|
    newResult = sheet_content.get_rowInfo_sortby_key.map do |row|
      content = ContentUtil.remove_escaped_new_line(row.content_dict[lang_path_obj.lang])
      value = add_escape(platform, lang_path_obj.lang, row.key_str, content, inject_obj)
      [row.key_str, value]
    end.to_h
    newResult = newResult.select do |key, value|
      to_skip = false
      if inject_obj.has_is_skip_by_key
        is_skip_by_key = inject_obj.load_is_skip_by_key(sheet_content.sheetname, lang_path_obj.lang, key)
        if is_skip_by_key.to_s.downcase == "true"
          to_skip = true
        end
      end
      !to_skip
    end
    newResult
  end.reduce({}, :merge)
  section_last_row = sheet_content_list
                         .map {|sheet_content| sheet_content.get_rowInfo_sortby_key.last.key_str }
                         .compact
                         .reverse
                         .drop(1)
                         .reverse
  json = JSON.pretty_generate(hash)
  section_last_row.each { |row| json.gsub!(/(.*)("#{row}")(.*)/, '\1\2\3' + "\n") }
  target = open(lang_path_obj.filepath, 'w')
  target.puts(json)
  target.close
end

.convert_to_stringfile(platform, langpath_obj, sheetcontent_list, injectObj) ⇒ Object



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/applocale/Core/convert_to_localefile.rb', line 29

def self.convert_to_stringfile(platform, langpath_obj, sheetcontent_list, injectObj)
  FileUtils.mkdir_p(File.dirname(langpath_obj.filepath))
  file_header_comment = []
  is_double_dash_comment = false
  is_multiline_comment = false
  if File.file?(langpath_obj.filepath)
    File.open(langpath_obj.filepath, 'r').each do |line|
    if is_double_dash_comment
      break unless line.strip.start_with?('//')
      file_header_comment.push(line)
      next
    end

    if is_multiline_comment
      file_header_comment.push(line)
      break if line.include?('*/')
    end

    unless is_multiline_comment or is_double_dash_comment
      case
      when line.chomp.empty?
        file_header_comment.push(line)
      when line.strip.start_with?('//')
        is_double_dash_comment = true
        file_header_comment.push(line)
      when line.strip.start_with?('/*') && !line.strip.start_with?('/**')
        is_multiline_comment = true
        file_header_comment.push(line)
      else
        break
      end
    end
    end
  end

  target = open(langpath_obj.filepath, 'w')
  target.puts file_header_comment.join('') if is_double_dash_comment or is_multiline_comment
  sheetcontent_list.each_with_index do |sheetcontent, index|
    contentlist = sheetcontent.get_rowInfo_sortby_key
    next if contentlist.length <= 0
    target.puts('') if index > 0 or !file_header_comment.empty?
    target.puts('/*******************************')
    target.puts(" *   #{sheetcontent.comment}")
    target.puts(' *******************************/')
    target.puts('')
    contentlist.each do |rowinfo|
      content = rowinfo.content_dict[langpath_obj.lang]
      if injectObj.has_is_skip_by_key
        is_skip_by_key = injectObj.load_is_skip_by_key(sheetcontent.sheetname, langpath_obj.lang, rowinfo.key_str)
        if is_skip_by_key.to_s.downcase == "true"
          next
        end
      end
      value = self.add_escape(platform, langpath_obj.lang, rowinfo.key_str, content, injectObj)
      target.puts("\"#{rowinfo.key_str}\" = \"#{value}\";")
    end
  end
  target.close
end

.convert_to_xml(platform, langpath_obj, sheetcontent_list, injectObj) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/applocale/Core/convert_to_localefile.rb', line 89

def self.convert_to_xml(platform, langpath_obj, sheetcontent_list, injectObj)
  FileUtils.mkdir_p(File.dirname(langpath_obj.filepath))
  target = open(langpath_obj.filepath, 'w')
  target.puts('<resources>')
  sheetcontent_list.each do |sheetcontent|
    target.puts("   <!-- #{sheetcontent.comment} -->")
    contentlist = sheetcontent.get_rowInfo_sortby_key
    contentlist.each do |rowinfo|
      content = rowinfo.content_dict[langpath_obj.lang]
      if injectObj.has_is_skip_by_key
        is_skip_by_key = injectObj.load_is_skip_by_key(sheetcontent.sheetname, langpath_obj.lang, rowinfo.key_str)
        if is_skip_by_key.to_s.downcase == "true"
          next
        end
      end
      value = self.add_escape(platform, langpath_obj.lang, rowinfo.key_str, content, injectObj)
      target.puts("   <string name=\"#{rowinfo.key_str}\">#{value}</string>")
    end
    target.puts('')
  end
  target.puts('</resources>')
  target.close
end