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, convert_file) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/applocale/Core/convert_to_localefile.rb', line 136

def self.add_escape(platform, lang, key, content, convert_file)
  value = ContentUtil.add_escape(platform, content)
  if convert_file.has_convent_to_locale
    return convert_file.load_convent_to_locale(lang.to_s, key,  content, value)
  end
  return value
end

.convert(setting, sheetcontent_list) ⇒ Object



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

def self.convert(setting, sheetcontent_list)

  platform = setting.platform
  lang_path_list =  setting.lang_path_list

  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, setting.convert_file)
    elsif platform == Platform::ANDROID
      self.convert_to_xml(platform, langpath_obj, sheetcontent_list, setting.convert_file)
    elsif platform == Platform::JSON
      self.convert_to_json(platform, langpath_obj, sheetcontent_list, setting.convert_file)
    end
  end
  puts 'Convert Finished !!!'.green
end

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/applocale/Core/convert_to_localefile.rb', line 113

def self.convert_to_json(platform, lang_path_obj, sheet_content_list, convert_file)
  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, convert_file)
      [row.key_str, value]
    end.to_h
    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, convert_file) ⇒ Object



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
# File 'lib/applocale/Core/convert_to_localefile.rb', line 31

def self.convert_to_stringfile(platform, langpath_obj, sheetcontent_list, convert_file)
  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]
      value = self.add_escape(platform, langpath_obj.lang, rowinfo.key_str, content, convert_file)
      target.puts("\"#{rowinfo.key_str}\" = \"#{value}\";")
    end
  end
  if convert_file.has_append_other
    convert_file.load_append_other(langpath_obj.lang.to_s,target)
  end
  target.close
end

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



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

def self.convert_to_xml(platform, langpath_obj, sheetcontent_list, convert_file)
  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, convert_file)
      target.puts("   <string name=\"#{rowinfo.key_str}\">#{value}</string>")
    end
    target.puts('')
  end

  if convert_file.has_append_other
    convert_file.load_append_other(langpath_obj.lang.to_s, target)
  end



  target.puts('</resources>')
  target.close
end