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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/applocale/Core/convert_to_localefile.rb', line 89

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



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 67

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|
    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
  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
# 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))
  target = open(langpath_obj.filepath, 'w')
  sheetcontent_list.each do |sheetcontent|
    contentlist = sheetcontent.get_rowInfo_sortby_key
    next if contentlist.length <= 0
    target.puts('/*******************************')
    target.puts(" *   #{sheetcontent.comment}")
    target.puts(' *******************************/')
    target.puts('')
    contentlist.each do |rowinfo|
      content = rowinfo.content_dict[langpath_obj.lang]
      value = self.add_escape(platform, langpath_obj.lang, rowinfo.key_str, content, injectObj)
      target.puts("\"#{rowinfo.key_str}\" = \"#{value}\";")
    end
    target.puts('')
  end
  target.close
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/applocale/Core/convert_to_localefile.rb', line 49

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