applocale 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,35 +1,37 @@
1
1
  require File.expand_path('../../setting.rb', __FILE__)
2
2
  require File.expand_path('../../../Util/error_util.rb', __FILE__)
3
3
  require File.expand_path('../../../Util/regex_util.rb', __FILE__)
4
+ require File.expand_path('../../../Util/injection.rb', __FILE__)
4
5
 
5
6
  module Applocale
6
7
  class ParseStringsFile
7
8
 
8
- attr_reader :strings_keys, :errorlist, :in_multiline_comments, :keys_list, :platform
9
+ attr_reader :strings_keys, :errorlist, :in_multiline_comments, :keys_list, :platform, :injectobj
9
10
 
10
- def initialize()
11
+ def initialize(platform, langpathobj_list, injectobj)
11
12
  @strings_keys = {}
12
13
  @keys_list = Array.new
13
14
  @errorlist = Array.new()
14
- @platform = Setting.platform
15
- self.to_parse_files(Setting.langlist)
15
+ @platform = platform
16
+ @injectobj = injectobj
17
+ self.to_parse_files(langpathobj_list)
16
18
  end
17
19
 
18
- def to_parse_files(lang_list)
19
- lang_list.each do |key, langinfo|
20
- self.to_parse_strings_file(key, langinfo[:path])
20
+ def to_parse_files(langpathobj_list)
21
+ langpathobj_list.each do |langpathobj|
22
+ self.to_parse_strings_file(langpathobj.lang, langpathobj.filepath)
21
23
  end
22
24
  end
23
25
 
24
- def to_parse_strings_file(lang, strings_path)
25
- puts "Start to Parse strings file: \"#{strings_path}\" ...".green
26
+ def to_parse_strings_file(lang, strings_filepath)
27
+ puts "Start to Parse strings file: \"#{strings_filepath}\" ...".green
26
28
 
27
29
  @in_multiline_comments = false
28
30
  keyrowno = {}
29
31
  linenum = 0
30
32
  begin
31
33
 
32
- IO.foreach(strings_path, mode: 'r:bom|utf-8') {|line|
34
+ IO.foreach(strings_filepath, mode: 'r:bom|utf-8') {|line|
33
35
  linenum += 1
34
36
  line.strip!
35
37
  if !@in_multiline_comments
@@ -41,25 +43,25 @@ module Applocale
41
43
  end
42
44
  while true
43
45
 
44
- key, line = parse_token(linenum, line, "=", lang, strings_path)
46
+ key, line = parse_token(linenum, line, "=", lang, strings_filepath)
45
47
  line.strip!
46
48
 
47
49
  if not line.start_with?("=")
48
50
  if !@in_multiline_comments && line.length > 0
49
- error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_path, lang, linenum)
51
+ error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_filepath, lang, linenum)
50
52
  @errorlist.push(error)
51
53
  end
52
54
  break
53
55
  end
54
56
  line.slice!(0)
55
57
 
56
- value, line = parse_token(linenum, line, ";", lang, strings_path)
58
+ value, line = parse_token(linenum, line, ";", lang, strings_filepath)
57
59
  line.strip!
58
60
 
59
61
  if line.start_with?(";")
60
62
  line.slice!(0)
61
63
  else
62
- error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_path, lang, linenum)
64
+ error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_filepath, lang, linenum)
63
65
  @errorlist.push(error)
64
66
  key = nil
65
67
  value = nil
@@ -67,7 +69,7 @@ module Applocale
67
69
  end
68
70
 
69
71
  if !ValidKey.is_validkey(@platform, key)
70
- error = ErrorUtil::ParseLocalizedError::InvalidKey.new(key, strings_path, lang, linenum)
72
+ error = ErrorUtil::ParseLocalizedError::InvalidKey.new(key, strings_filepath, lang, linenum)
71
73
  @errorlist.push(error)
72
74
  break
73
75
  end
@@ -78,10 +80,10 @@ module Applocale
78
80
  if @strings_keys[key][lang.to_s].nil?
79
81
  @strings_keys[key][lang.to_s] = Hash.new
80
82
  @strings_keys[key][lang.to_s][:rowno] = linenum
81
- @strings_keys[key][lang.to_s][:value] = ContentUtil.remove_escape(@platform, value)
83
+ @strings_keys[key][lang.to_s][:value] = self.remove_escape(lang, key, value)
82
84
  keyrowno[key] = linenum
83
85
  else
84
- error = ErrorUtil::ParseLocalizedError::DuplicateKey.new(key, keyrowno[key], strings_path, lang, linenum)
86
+ error = ErrorUtil::ParseLocalizedError::DuplicateKey.new(key, keyrowno[key], strings_filepath, lang, linenum)
85
87
  @errorlist.push(error)
86
88
  end
87
89
  if line.length <= 0
@@ -89,8 +91,9 @@ module Applocale
89
91
  end
90
92
  end
91
93
  }
92
- rescue
93
- ErrorUtil::ParseLocalizedError::InvalidFile.new(strings_path).raise
94
+ rescue Exception => e
95
+ puts e.message
96
+ ErrorUtil::ParseLocalizedError::InvalidFile.new(strings_filepath).raise
94
97
  end
95
98
  end
96
99
 
@@ -159,5 +162,21 @@ module Applocale
159
162
  return value, line[n..-1]
160
163
  end
161
164
 
165
+ def remove_escape(lang, key, content)
166
+ value = content
167
+ if @injectobj.has_before_parse_from_locale
168
+ value = @injectobj.load_before_parse_from_locale(lang.to_s, key, value)
169
+ end
170
+ if @injectobj.has_parse_from_locale
171
+ value = @injectobj.load_parse_from_locale(lang.to_s, key, value)
172
+ else
173
+ value = ContentUtil.remove_escape(@platform, value)
174
+ end
175
+ if @injectobj.has_after_parse_from_locale
176
+ value = @injectobj.load_after_parse_from_locale(lang.to_s, key, value)
177
+ end
178
+ return value
179
+ end
180
+
162
181
  end
163
182
  end
@@ -5,19 +5,20 @@ require File.expand_path('../../../Util/regex_util.rb', __FILE__)
5
5
  module Applocale
6
6
 
7
7
  class ParseXMLFile
8
- attr_reader :strings_keys, :errorlist, :in_multiline_comments, :keys_list, :platform
8
+ attr_reader :strings_keys, :errorlist, :in_multiline_comments, :keys_list, :platform, :injectobj
9
9
 
10
- def initialize()
10
+ def initialize(platform, langpathobj_list, injectobj)
11
11
  @strings_keys = {}
12
12
  @keys_list = Array.new
13
13
  @errorlist = Array.new()
14
- @platform = Setting.platform
15
- self.to_parse_files(Setting.langlist)
14
+ @platform = platform
15
+ @injectobj = injectobj
16
+ self.to_parse_files(langpathobj_list)
16
17
  end
17
18
 
18
- def to_parse_files(lang_list)
19
- lang_list.each do |key, langinfo|
20
- self.to_parse_strings_file(key, langinfo[:path])
19
+ def to_parse_files(langpathobj_list)
20
+ langpathobj_list.each do |langpathobj|
21
+ self.to_parse_strings_file(langpathobj.lang, langpathobj.filepath)
21
22
  end
22
23
  end
23
24
 
@@ -37,7 +38,7 @@ module Applocale
37
38
  end
38
39
  if @strings_keys[key][lang.to_s].nil?
39
40
  @strings_keys[key][lang.to_s] = Hash.new
40
- @strings_keys[key][lang.to_s][:value] = ContentUtil.remove_escape(@platform, value)
41
+ @strings_keys[key][lang.to_s][:value] = self.remove_escape(lang, key, value)
41
42
  else
42
43
  error = ErrorUtil::ParseLocalizedError::DuplicateKey.new(key, -1, strings_path, lang, -1)
43
44
  @errorlist.push(error)
@@ -46,5 +47,21 @@ module Applocale
46
47
  end
47
48
  end
48
49
 
50
+ def remove_escape(lang, key, content)
51
+ value = content
52
+ if @injectobj.has_before_parse_from_locale
53
+ value = @injectobj.load_before_parse_from_locale(lang.to_s, key, value)
54
+ end
55
+ if @injectobj.has_parse_from_locale
56
+ value = @injectobj.load_parse_from_locale(lang.to_s, key, value)
57
+ else
58
+ value = ContentUtil.remove_escape(@platform, value)
59
+ end
60
+ if @injectobj.has_after_parse_from_locale
61
+ value = @injectobj.load_after_parse_from_locale(lang.to_s, key, value)
62
+ end
63
+ return value
64
+ end
65
+
49
66
  end
50
67
  end
@@ -1,26 +1,32 @@
1
1
  require File.expand_path('../setting.rb', __FILE__)
2
2
  require File.expand_path('../../Util/platform.rb', __FILE__)
3
3
  require File.expand_path('../../Util/regex_util.rb', __FILE__)
4
+ require File.expand_path('../../Util/injection.rb', __FILE__)
5
+
4
6
  require 'colorize'
5
7
 
8
+
6
9
  module Applocale
7
10
  class ConvertToStrFile
8
11
 
9
- def self.convert(sheetcontent_list, setting = Setting)
10
- setting.langlist.each do |lang, langinfo|
11
- puts "Start to convert to string file for [\"#{lang}\"] #{langinfo[:path]}...".green
12
- if setting.platform == Platform::IOS
13
- self.convert_to_localefile(setting.platform, lang, langinfo[:path], sheetcontent_list)
14
- elsif setting.platform == Platform::ANDROID
15
- self.convert_to_xml(setting.platform, lang, langinfo[:path], sheetcontent_list)
12
+ def self.convert(platform, lang_path_list, sheetcontent_list, rubycode)
13
+
14
+ injectObj = Applocale::Injection.load(rubycode)
15
+
16
+ lang_path_list.each do |langpath_obj|
17
+ puts "Start to convert to string file for [\"#{langpath_obj.lang}\"] #{langpath_obj.filepath}...".green
18
+ if platform == Platform::IOS
19
+ self.convert_to_stringfile(platform, langpath_obj, sheetcontent_list, injectObj)
20
+ elsif platform == Platform::ANDROID
21
+ self.convert_to_xml(platform, langpath_obj, sheetcontent_list, injectObj)
16
22
  end
17
23
  end
18
24
  puts 'Convert Finished !!!'.green
19
25
  end
20
26
 
21
- def self.convert_to_localefile(platform, lang, langfilepath, sheetcontent_list)
22
- FileUtils.mkdir_p(File.dirname(langfilepath))
23
- target = open(langfilepath, 'w')
27
+ def self.convert_to_stringfile(platform, langpath_obj, sheetcontent_list, injectObj)
28
+ FileUtils.mkdir_p(File.dirname(langpath_obj.filepath))
29
+ target = open(langpath_obj.filepath, 'w')
24
30
  sheetcontent_list.each do |sheetcontent|
25
31
  contentlist = sheetcontent.get_rowInfo_sortby_key
26
32
  next if contentlist.length <= 0
@@ -29,8 +35,8 @@ module Applocale
29
35
  target.puts(' *******************************/')
30
36
  target.puts('')
31
37
  contentlist.each do |rowinfo|
32
- content = rowinfo.content_dict[lang]
33
- value = ContentUtil.add_escape(platform, content)
38
+ content = rowinfo.content_dict[langpath_obj.lang]
39
+ value = self.add_escape(platform, langpath_obj.lang, rowinfo.key_str, content, injectObj)
34
40
  target.puts("\"#{rowinfo.key_str}\" = \"#{value}\";")
35
41
  end
36
42
  target.puts('')
@@ -38,17 +44,17 @@ module Applocale
38
44
  target.close
39
45
  end
40
46
 
41
- def self.convert_to_xml(platform, lang, langfilepath, sheetcontent_list)
42
- FileUtils.mkdir_p(File.dirname(langfilepath))
43
- target = open(langfilepath, 'w')
47
+ def self.convert_to_xml(platform, langpath_obj, sheetcontent_list, injectObj)
48
+ FileUtils.mkdir_p(File.dirname(langpath_obj.filepath))
49
+ target = open(langpath_obj.filepath, 'w')
44
50
  target.puts('<resources>')
45
51
  sheetcontent_list.each do |sheetcontent|
46
52
  target.puts(" <!-- #{sheetcontent.comment} -->")
47
53
  contentlist = sheetcontent.get_rowInfo_sortby_key
48
54
  contentlist.each do |rowinfo|
49
- content = rowinfo.content_dict[lang]
50
- value = ContentUtil.add_escape(platform, content)
51
- target.puts(" <string name=\"#{rowinfo.key_str.downcase}\">#{value}</string>")
55
+ content = rowinfo.content_dict[langpath_obj.lang]
56
+ value = self.add_escape(platform, langpath_obj.lang, rowinfo.key_str, content, injectObj)
57
+ target.puts(" <string name=\"#{rowinfo.key_str}\">#{value}</string>")
52
58
  end
53
59
  target.puts('')
54
60
  end
@@ -56,5 +62,21 @@ module Applocale
56
62
  target.close
57
63
  end
58
64
 
65
+ def self.add_escape(platform, lang, key, content, injectObj)
66
+ value = content
67
+ if injectObj.has_before_convent_to_locale
68
+ value = injectObj.load_before_convent_to_locale(lang.to_s, key, value)
69
+ end
70
+ if injectObj.has_convent_to_locale
71
+ value = injectObj.load_convent_to_locale(lang.to_s, key, value)
72
+ else
73
+ value = ContentUtil.add_escape(platform, value)
74
+ end
75
+ if injectObj.has_after_convent_to_locale
76
+ value = injectObj.load_after_convent_to_locale(lang.to_s, key, value)
77
+ end
78
+ return value
79
+ end
80
+
59
81
  end
60
82
  end
@@ -1,28 +1,90 @@
1
1
  require File.expand_path('../setting.rb', __FILE__)
2
+ require File.expand_path('../../Util/file_util.rb', __FILE__)
3
+ require File.expand_path('../../Util/error_util.rb', __FILE__)
4
+ require File.expand_path('../../Util/config_util.rb', __FILE__)
2
5
  require File.expand_path('../GoogleHepler/google_helper.rb', __FILE__)
3
6
  require File.expand_path('../ParseXLSX/parse_xlsx', __FILE__)
7
+ require File.expand_path('../ParserStringFile/parse_localized_resource.rb', __FILE__)
4
8
  require File.expand_path('../convert_to_localefile', __FILE__)
9
+ require File.expand_path('../FindStrKey/find_str_key', __FILE__)
10
+
11
+ require 'open-uri'
5
12
 
6
13
  module Applocale
7
14
 
8
- def self.start_update(is_localupdate)
9
- is_local = is_localupdate
10
- ConfigUtil.load_and_validate_xlsx_to_localefile(is_local)
11
- Setting.printlog
12
- setting = Applocale::Setting
13
- unless is_localupdate
14
- google_file_id = GoogleHelper.is_googlelink(setting.link)
15
- unless google_file_id.nil?
16
- GoogleHelper.download_spreadsheet(google_file_id, setting.xlsxpath)
15
+ def self.create_config_file( platformStr = nil, projpath = Dir.pwd)
16
+ proj_path = projpath
17
+ proj_path = Dir.pwd if projpath.nil?
18
+ proj_apath = Applocale::FilePathUtil.get_proj_absoluat_path(proj_path)
19
+ if platformStr.nil?
20
+ if Dir.glob("#{proj_apath}/**/*.xcodeproj").length > 0 || Dir.glob("#{proj_apath}/*.xcworkspace").length > 0
21
+ platformsybom = Platform::IOS
22
+ elsif Dir.glob("#{proj_apath}/**/*.gradle").length > 0
23
+ platformsybom = Platform::ANDROID
24
+ else
25
+ Applocale::ErrorUtil::CommandError.new("Mssing [platform] : ios | android ").raise
26
+ end
27
+ else
28
+ platformsybom = Platform.init(platform.strip)
29
+ end
30
+ if platformsybom.nil?
31
+ ErrorUtil::CommandError.new("Invalid [platform] : ios | android ").raise
32
+ else
33
+ Applocale::Config::ConfigUtil.create_configfile_ifneed(platformsybom,proj_apath.to_s )
34
+ end
35
+ end
36
+
37
+ def self.start_update(projpath = Dir.pwd)
38
+ proj_path = projpath
39
+ proj_path = Dir.pwd if projpath.nil?
40
+ proj_apath = Applocale::FilePathUtil.get_proj_absoluat_path(proj_path)
41
+ obj = Applocale::Config::ConfigUtil.new(proj_apath)
42
+ setting = obj.load_configfile_to_setting
43
+ if setting.link.to_s.length <= 0
44
+ ErrorUtil::ConfigFileInValid.new('[link] is missing in config file ').raise
45
+ end
46
+ if Applocale::GoogleHelper.is_googlelink(setting.link)
47
+ if setting.google_credentials_path.to_s.length <= 0
48
+ setting.google_credentials_path = File.expand_path(FilePathUtil.default_google_credentials_filename, File.dirname(setting.configfile_pathstr))
17
49
  end
50
+ googleobj = Applocale::GoogleHelper.new(setting.link, setting.google_credentials_path, setting.xlsxpath)
51
+ googleobj.download
52
+ else
53
+ download = open(setting.link)
54
+ IO.copy_stream(download, setting.xlsxpath)
18
55
  end
56
+ Applocale.start_local_update( setting, proj_path)
57
+ end
19
58
 
20
- parse_xlsx = ParseXLSX.new
21
- ConvertToStrFile.convert(parse_xlsx.result)
59
+ def self.start_local_update(asetting = nil, projpath = Dir.pwd)
60
+ proj_path = projpath
61
+ proj_path = Dir.pwd if projpath.nil?
62
+ proj_apath = Applocale::FilePathUtil.get_proj_absoluat_path(proj_path)
63
+ setting = asetting
64
+ if setting.nil?
65
+ obj = Applocale::Config::ConfigUtil.new(proj_apath)
66
+ setting = obj.load_configfile_to_setting
67
+ end
68
+ parse_xlsx = Applocale::ParseXLSX.new(setting.platform, setting.xlsxpath, setting.lang_path_list, setting.sheet_obj_list)
69
+ ConvertToStrFile.convert(setting.platform, setting.lang_path_list,parse_xlsx.result, setting.rubycode)
22
70
  end
23
71
 
24
- def self.start_reverse(is_skip)
25
- Applocale::ParseLocalizedResource.new(is_skip)
72
+ def self.start_reverse( is_skip, projpath = Dir.pwd)
73
+ proj_path = projpath
74
+ proj_path = Dir.pwd if projpath.nil?
75
+ proj_apath = Applocale::FilePathUtil.get_proj_absoluat_path(proj_path)
76
+ obj = Applocale::Config::ConfigUtil.new(proj_apath)
77
+ setting = obj.load_configfile_to_setting
78
+ Applocale::ParseLocalizedResource.new(is_skip,setting.platform,setting.xlsxpath, setting.lang_path_list, setting.sheet_obj_list, setting.rubycode )
26
79
  end
27
80
 
28
- end
81
+ def self.findkey( key, projpath = Dir.pwd)
82
+ proj_path = projpath
83
+ proj_path = Dir.pwd if projpath.nil?
84
+ proj_apath = Applocale::FilePathUtil.get_proj_absoluat_path(proj_path)
85
+ obj = Applocale::Config::ConfigUtil.new(proj_apath)
86
+ report_folder = File.dirname(obj.configfile_pathstr)
87
+ findobj = FindStrKey::FindValue.new(Applocale::Platform::IOS, proj_apath, report_folder, key)
88
+ findobj.find
89
+ end
90
+ end
@@ -1,18 +1,119 @@
1
1
  require 'colorize'
2
+ # module Applocale
3
+ # class Setting
4
+ # class <<self
5
+ # attr_accessor :link, :platform, :keystr, :langlist, :xlsxpath
6
+ # end
7
+ #
8
+ # def self.printlog
9
+ # puts ' In Setting'
10
+ # puts " link = #{self.link}"
11
+ # puts " platform = #{self.platform}"
12
+ # puts " keystr = #{self.keystr}"
13
+ # puts " langlist = #{self.langlist}"
14
+ # puts " xlsxpath = #{self.xlsxpath}"
15
+ # end
16
+ #
17
+ # end
18
+ # end
19
+ #
2
20
 
3
21
  module Applocale
4
- class Setting
5
- class <<self
6
- attr_accessor :link, :platform, :keystr, :langlist, :xlsxpath
22
+ module Config
23
+ class Setting
24
+ attr_accessor :configfile_pathstr, :link, :platform, :xlsxpath, :google_credentials_path, :lang_path_list, :sheet_obj_list, :rubycode
25
+ def initialize(configfile_pathstr)
26
+ self.configfile_pathstr = configfile_pathstr
27
+ self.lang_path_list = Array.new
28
+ self.sheet_obj_list = Array.new
29
+ end
30
+
31
+ def printlog
32
+ puts ' In Setting'
33
+ puts " link = #{self.link}"
34
+ puts " platform = #{self.platform}"
35
+ puts " xlsxpath = #{self.xlsxpath}"
36
+ puts " google_credentials_path = #{self.google_credentials_path} "
37
+
38
+ puts " lang_path_list = "
39
+ self.lang_path_list.each do |langpath_obj|
40
+ puts " #{langpath_obj.to_s}"
41
+ end
42
+ puts " sheet_obj_list = "
43
+ self.sheet_obj_list.each do |sheet_obj|
44
+ puts " #{sheet_obj.to_s}"
45
+ end
46
+
47
+ # puts self.rubycode
48
+
49
+ end
7
50
  end
8
51
 
9
- def self.printlog
10
- puts ' In Setting'
11
- puts " link = #{self.link}"
12
- puts " platform = #{self.platform}"
13
- puts " keystr = #{self.keystr}"
14
- puts " langlist = #{self.langlist}"
15
- puts " xlsxpath = #{self.xlsxpath}"
52
+ class LangPath
53
+ attr_accessor :lang, :filepath
54
+
55
+ def initialize(lang, filepath)
56
+ self.lang = lang
57
+ self.filepath = filepath
58
+ end
59
+
60
+ def to_s
61
+ return "#{self.lang}: #{self.filepath}"
62
+ end
63
+ end
64
+
65
+ class Sheet
66
+ attr_accessor :sheetname, :obj
67
+
68
+ def initialize(sheetname, obj)
69
+ self.sheetname = sheetname
70
+ self.obj = obj
71
+ end
72
+
73
+ def to_s
74
+ return "#{self.sheetname} #{self.obj.to_s}"
75
+ end
76
+
77
+
78
+ def self.get_sheetlist(sheet_obj_list)
79
+ return sheet_obj_list.map{ |sheet_obj| sheet_obj.sheetname }
80
+ end
81
+
82
+ def self.get_sheetobj_by_sheetname(sheet_obj_list, sheetname)
83
+ sheet_obj_list.each do |sheet_obj|
84
+ if sheet_obj.sheetname == sheetname
85
+ return sheet_obj.obj
86
+ end
87
+ end
88
+ return nil
89
+ end
90
+ end
91
+
92
+ class SheetInfoByHeader
93
+ attr_accessor :key_header, :lang_headers
94
+
95
+ def initialize(key_header, lang_headers)
96
+ self.key_header = key_header
97
+ self.lang_headers = lang_headers
98
+ end
99
+
100
+ def to_s
101
+ return "key_header: #{self.key_header} | headers: #{self.lang_headers.to_s}"
102
+ end
103
+ end
104
+
105
+ class SheetInfoByRow
106
+ attr_accessor :row, :key_col, :lang_cols
107
+
108
+ def initialize(row, key_col, lang_cols)
109
+ self.row = row
110
+ self.key_col = key_col
111
+ self.lang_cols = lang_cols
112
+ end
113
+
114
+ def to_s
115
+ return "row: #{self.row} | key_col: #{self.key_col} | lang_cols: #{self.lang_cols.to_s}"
116
+ end
16
117
  end
17
118
 
18
119
  end