applocale 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b45b48d7674c6d97e3d11df01c42f8c01846f111
4
+ data.tar.gz: 3aff08fc3d51d24ba1d9bdc3ab24d52d73be9a2a
5
+ SHA512:
6
+ metadata.gz: 73bd62b7e73636000fae0a58189b5e3cfe9acc7b35943de568c99ecb3312f932329823ec6fd2db94832792fe88afd42970c578bec50658786170be72aca6721f
7
+ data.tar.gz: ead0928386d2a507f3f1945b48297c8ac622b02faf61f5694c392d3b16ea123a0c7679ad0a4dcb1fb657681f4f731c48506f2cfa7fde429181bb1c8d7cb8a38d
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "applocale"
4
+
5
+
6
+
@@ -0,0 +1,9 @@
1
+ require "applocale/version"
2
+ require 'thor'
3
+ require 'applocale/Command/init'
4
+
5
+ module Applocale
6
+ # Your code goes here...
7
+ end
8
+
9
+ Applocale::Command::Init.start(ARGV)
@@ -0,0 +1,8 @@
1
+ link: "https://docs.google.com/spreadsheets/d/1Wy2gN_DSw-TCU2gPCzqvYxLfFG5fyK5rodXs5MLUy8w"
2
+ platform: "#{platform}"
3
+ keystr: "Key"
4
+ langlist:
5
+ zh_TW: ["Chinese(Traditional)", "#{path_zh_TW}"]
6
+ zh_CN: ["Chinese(Simplified)", "#{path_zh_CN}"]
7
+ en_US: ["English", "#{path_en_US}"]
8
+ xlsxpath: "#{xlsxpath}"
@@ -0,0 +1,58 @@
1
+ require File.expand_path('../../Util/file_util.rb', __FILE__)
2
+ require File.expand_path('../../Util/config_util.rb', __FILE__)
3
+ require File.expand_path('../../Util/error_util.rb', __FILE__)
4
+ require File.expand_path('../../Core/setting.rb', __FILE__)
5
+ require File.expand_path('../../Core/init.rb', __FILE__)
6
+ require File.expand_path('../../Core/ParserStringFile/parse_localized_resource.rb', __FILE__)
7
+
8
+ require 'thor'
9
+
10
+ module Applocale
11
+ class Command
12
+ class Init < Thor
13
+ desc "init [platform]", "Create Config File, platform: ios | android"
14
+
15
+ def init(platform = nil)
16
+ if platform.nil?
17
+ if Dir["*.xcodeproj"].length > 0
18
+ platformsybom = Platform::IOS
19
+ elsif Dir[".gradle"].length > 0
20
+ platformsybom = Platform::ANDROID
21
+ else
22
+ self.class.help(shell)
23
+ Applocale::ErrorUtil::CommandError.new("Mssing [platform] : ios | android ").raise
24
+ end
25
+ else
26
+ platformsybom = Platform.init(platform)
27
+ end
28
+
29
+ if platformsybom.nil?
30
+ self.class.help(shell)
31
+ ErrorUtil::CommandError.new("Invalid [platform] : ios | android ").raise
32
+ else
33
+ ConfigUtil.createConfigFileIfNeed(platformsybom)
34
+ end
35
+ end
36
+
37
+ desc "update", "Download xlsx and convert to localization string file"
38
+ option :local, :desc => "Convert local xlsx file to localization string file"
39
+ def update()
40
+ is_local = !options[:local].nil?
41
+ puts is_local
42
+ ConfigUtil.loadAndValidateForXlsxToStringFile(false)
43
+ Setting.printlog
44
+ Applocale.start(is_local, Applocale::Setting)
45
+ end
46
+
47
+ desc "reverse", "Convert localization string file to xlsx"
48
+ option :skip, :desc => "Skip Error"
49
+ def reverse()
50
+ is_skip = !options[:skip].nil?
51
+ ConfigUtil.loadAndValidateForStringFileToXlsx()
52
+ Setting.printlog
53
+ Applocale::ParseLocalizedResource.new(is_skip)
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,92 @@
1
+ require File.expand_path('../../setting.rb', __FILE__)
2
+ require File.expand_path('../../../Util/platform.rb', __FILE__)
3
+ require File.expand_path('../../parse_xlsx_module', __FILE__)
4
+ require File.expand_path('../../../Util/color_util.rb', __FILE__)
5
+ require File.expand_path('../parse_strings_file', __FILE__)
6
+ require File.expand_path('../parse_xml_file', __FILE__)
7
+
8
+ require 'rubyXL'
9
+
10
+ module Applocale
11
+ class ParseLocalizedResource
12
+
13
+ # @xlsx = nil
14
+
15
+ @skip_error = false
16
+ @setting = Setting
17
+ # @xlsx = RubyXL::Workbook
18
+ # @allError = Array
19
+ # @sheetcontent_list = Array
20
+
21
+ def initialize(skip_error = false)
22
+ @skip_error = skip_error
23
+ @setting = Setting
24
+
25
+ FileUtils.mkdir_p(File.dirname(@setting.xlsxpath))
26
+ FileUtils.rm(@setting.xlsxpath) if File.exist? @setting.xlsxpath
27
+
28
+ keystrwithColNo = ParseXLSXModule::KeyStrWithColNo.new(@setting.keystr, 0)
29
+ langwithColNolist = Array.new
30
+ colno = 1
31
+
32
+ @setting.langlist.each do |key, langinfo|
33
+ langwithColNo = ParseXLSXModule::LangWithColNo.new(langinfo[:xlsheader], key, colno)
34
+ colno+=1
35
+ langwithColNolist.push(langwithColNo)
36
+ end
37
+
38
+ if @setting.platform == Platform::IOS
39
+ result = self.parseIOS()
40
+ writeToXlSX(@setting.xlsxpath, keystrwithColNo, langwithColNolist, result[:errorlist], result[:content], result[:keylist])
41
+ else
42
+ result = self.parseAndroid()
43
+ writeToXlSX(@setting.xlsxpath, keystrwithColNo, langwithColNolist, result[:errorlist], result[:content], result[:keylist])
44
+ end
45
+
46
+
47
+ end
48
+
49
+ def parseIOS()
50
+ result = ParseStringsFile.new()
51
+ errorlist = result.errorlist
52
+ content = result.strings_keys
53
+ keylist = result.keys_list
54
+ return {:errorlist => errorlist, :content => content, :keylist => keylist}
55
+ end
56
+
57
+ def parseAndroid()
58
+ result = ParseXMLFile.new()
59
+ errorlist = result.errorlist
60
+ content = result.strings_keys
61
+ keylist = result.keys_list
62
+ return {:errorlist => errorlist, :content => content, :keylist => keylist}
63
+ end
64
+
65
+ def writeToXlSX(path, keystrwithColNo, langwithColNolist, errorlist, content, keylist)
66
+ ErrorUtil::ParseLocalizedError::ParseLocalizedError.raiseArr(errorlist, !@skip_error)
67
+ puts "Start write to file: \"#{path}\" ...".green
68
+
69
+ workbook = RubyXL::Workbook.new
70
+ worksheet = workbook.worksheets[0]
71
+ rowno = 0
72
+ worksheet.add_cell(rowno, keystrwithColNo.colno, keystrwithColNo.header_str)
73
+ langwithColNolist.each do |langwithColNo|
74
+ worksheet.add_cell(rowno, langwithColNo.colno, langwithColNo.header_str)
75
+ end
76
+
77
+ rowno+=1
78
+ keylist.each do |key|
79
+ worksheet.add_cell(rowno, keystrwithColNo.colno, key)
80
+ if !content[key].nil?
81
+ langwithColNolist.each do |langwithColNo|
82
+ lang = langwithColNo.lang.to_s
83
+ worksheet.add_cell(rowno, langwithColNo.colno, content[key][lang][:value]) if !content[key][lang].nil? && !content[key][lang][:value].nil?
84
+ end
85
+ end
86
+ rowno+=1
87
+ end
88
+ workbook.write(path)
89
+
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,157 @@
1
+ require File.expand_path('../../setting.rb', __FILE__)
2
+ require File.expand_path('../../../Util/error_util.rb', __FILE__)
3
+ require File.expand_path('../../../Util/regex_util.rb', __FILE__)
4
+
5
+ module Applocale
6
+ class ParseStringsFile
7
+
8
+ attr_reader :strings_keys, :errorlist, :in_multiline_comments, :keys_list, :platform
9
+
10
+ def initialize()
11
+ @strings_keys = {}
12
+ @keys_list = Array.new
13
+ @errorlist = Array.new()
14
+ @platform = Setting.platform
15
+ self.to_parse_files(Setting.langlist)
16
+ end
17
+
18
+ def to_parse_files(lang_list)
19
+ lang_list.each do |key, langinfo|
20
+ self.to_parse_strings_file(key, langinfo[:path])
21
+ end
22
+ end
23
+
24
+ def to_parse_strings_file(lang, strings_path)
25
+ puts "Start to Parse strings file: \"#{strings_path}\" ...".green
26
+
27
+ @in_multiline_comments = false
28
+ keyrowno = {}
29
+ linenum = 0
30
+ IO.foreach(strings_path, mode: 'r:bom|utf-8') {|line|
31
+ linenum += 1
32
+ line.strip!
33
+ if !@in_multiline_comments
34
+ next if line.start_with?('#')
35
+ next if line.start_with?('//')
36
+ end
37
+ if line.length <= 0
38
+ next
39
+ end
40
+ while true
41
+
42
+ key, line = parse_token(linenum, line, "=", lang, strings_path)
43
+ line.strip!
44
+
45
+ if not line.start_with?("=")
46
+ if !@in_multiline_comments && line.length > 0
47
+ error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_path, lang, linenum)
48
+ @errorlist.push(error)
49
+ end
50
+ break
51
+ end
52
+ line.slice!(0)
53
+
54
+ value, line = parse_token(linenum, line, ";", lang, strings_path)
55
+ line.strip!
56
+
57
+ if line.start_with?(";")
58
+ line.slice!(0)
59
+ else
60
+ error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_path, lang, linenum)
61
+ @errorlist.push(error)
62
+ key = nil
63
+ value = nil
64
+ break
65
+ end
66
+
67
+ if !ValidKey.isValidKey(@platform, key)
68
+ error = ErrorUtil::ParseLocalizedError::InvalidKey.new(key, strings_path, lang, linenum)
69
+ @errorlist.push(error)
70
+ break
71
+ end
72
+ if @strings_keys[key].nil?
73
+ @strings_keys[key] = Hash.new
74
+ @keys_list.push(key)
75
+ end
76
+ if @strings_keys[key][lang.to_s].nil?
77
+ @strings_keys[key][lang.to_s] = Hash.new
78
+ @strings_keys[key][lang.to_s][:rowno] = linenum
79
+ @strings_keys[key][lang.to_s][:value] = ContentUtil.removeEscape(@platform, value)
80
+ keyrowno[key] = linenum
81
+ else
82
+ error = ErrorUtil::ParseLocalizedError::DuplicateKey.new(key, keyrowno[key], strings_path, lang, linenum)
83
+ @errorlist.push(error)
84
+ end
85
+ if line.length <= 0
86
+ break
87
+ end
88
+ end
89
+ }
90
+ end
91
+
92
+ def parse_token(linenum, line, sep, lang, file)
93
+ n = 0
94
+ in_value = false
95
+ in_quote = false
96
+ in_escape = false
97
+ value = ""
98
+
99
+ for ch in line.chars
100
+ prech = ""
101
+ prech = line.chars[n-1] if n > 0
102
+ n += 1
103
+ if @in_multiline_comments
104
+ if "#{prech}#{ch}" == "*/"
105
+ @in_multiline_comments = false
106
+ in_value = false
107
+ value = ""
108
+ end
109
+ next
110
+ end
111
+
112
+ if not in_value
113
+ if ch == "\""
114
+ in_quote = true
115
+ in_value = true
116
+ elsif ch != " " and ch != "\t" and ch != sep
117
+ in_value = true
118
+ value << ch
119
+ end
120
+ next
121
+ end
122
+
123
+ if in_escape
124
+ value << prech
125
+ value << ch
126
+ in_escape = false
127
+ elsif ch == "\\"
128
+ in_escape = true
129
+ elsif in_quote
130
+ if ch == "\""
131
+ break
132
+ else
133
+ value << ch
134
+ end
135
+ else
136
+ if ch == " " or ch == "\t" or ch == sep
137
+ n -= 1
138
+ break
139
+ elsif "#{prech}#{ch}" == "/*"
140
+ @in_multiline_comments = true
141
+ elsif "#{prech}#{ch}" == "//"
142
+ return value, ""
143
+ elsif ch == "#"
144
+ return value, ""
145
+ elsif "#{prech}#{ch}".length > 1
146
+ error = ErrorUtil::ParseLocalizedError::WrongFormat.new(file, lang, linenum)
147
+ @errorlist.push(error)
148
+ return value, ""
149
+ else
150
+ value << ch
151
+ end
152
+ end
153
+ end
154
+ return value, line[n..-1]
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path('../../setting.rb', __FILE__)
2
+ require File.expand_path('../../../Util/error_util.rb', __FILE__)
3
+ require File.expand_path('../../../Util/regex_util.rb', __FILE__)
4
+
5
+ module Applocale
6
+
7
+ class ParseXMLFile
8
+ attr_reader :strings_keys, :errorlist, :in_multiline_comments, :keys_list, :platform
9
+
10
+ def initialize()
11
+ @strings_keys = {}
12
+ @keys_list = Array.new
13
+ @errorlist = Array.new()
14
+ @platform = Setting.platform
15
+ self.to_parse_files(Setting.langlist)
16
+ end
17
+
18
+ def to_parse_files(lang_list)
19
+ lang_list.each do |key, langinfo|
20
+ self.to_parse_strings_file(key, langinfo[:path])
21
+ end
22
+ end
23
+
24
+ def to_parse_strings_file(lang, strings_path)
25
+ xml_doc = nil
26
+ return if !File.exist? strings_path
27
+ puts "Start to Parse xml file: \"#{strings_path}\" ...".green
28
+
29
+ xml_doc = Nokogiri::XML(File.open(strings_path))
30
+ string_nodes = xml_doc.xpath("//string")
31
+ string_nodes.each do |node|
32
+ key = node["name"]
33
+ value = node.content
34
+ if !key.nil? && key.strip.length > 0
35
+ if @strings_keys[key].nil?
36
+ @strings_keys[key] = Hash.new
37
+ @keys_list.push(key)
38
+ end
39
+ if @strings_keys[key][lang.to_s].nil?
40
+ @strings_keys[key][lang.to_s] = Hash.new
41
+ @strings_keys[key][lang.to_s][:value] = ContentUtil.removeEscape(@platform, value)
42
+ else
43
+ error = ErrorUtil::ParseLocalizedError::DuplicateKey.new(key, -1, strings_path, lang, -1)
44
+ @errorlist.push(error)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1 @@
1
+ {"installed":{"client_id":"750829847289-9pslc6jsi1mhe1oioj6sgl7i8o2omi80.apps.googleusercontent.com","project_id":"rubystringfileconvert-166004","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"SEAeayq9qpTZeH8MRac2K7q6","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
@@ -0,0 +1,66 @@
1
+ require File.expand_path('../setting.rb', __FILE__)
2
+ require File.expand_path('../../Util/platform.rb', __FILE__)
3
+ require File.expand_path('../../Util/color_util.rb', __FILE__)
4
+ require File.expand_path('../../Util/regex_util.rb', __FILE__)
5
+
6
+ module Applocale
7
+ class ConvertToStrFile
8
+
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.convertToStrings(setting.platform, lang, langinfo[:path], sheetcontent_list)
14
+ elsif setting.platform == Platform::ANDROID
15
+ self.convertToXML(setting.platform,lang, langinfo[:path], sheetcontent_list)
16
+ end
17
+ end
18
+
19
+ puts "Convert Finished !!!".green
20
+ end
21
+
22
+ def self.convertToStrings(platform, lang, langfilepath, sheetcontent_list)
23
+ FileUtils.mkdir_p(File.dirname(langfilepath))
24
+ target = open(langfilepath, 'w')
25
+
26
+ sheetcontent_list.each do |sheetcontent|
27
+ target.puts("/*******************************")
28
+ target.puts(" * #{sheetcontent.comment}")
29
+ target.puts(" *******************************/")
30
+ target.puts("")
31
+ contentlist = sheetcontent.getRowInfoSortByKey()
32
+ contentlist.each do |rowinfo|
33
+ content = rowinfo.content_dict[lang]
34
+ value = ContentUtil.addEscape(platform,content)
35
+ target.puts("\"#{rowinfo.key_str.downcase}\" = \"#{value}\";")
36
+ end
37
+ target.puts("")
38
+ end
39
+ target.close
40
+
41
+ end
42
+
43
+ def self.convertToXML(platform, lang, langfilepath, sheetcontent_list)
44
+ FileUtils.mkdir_p(File.dirname(langfilepath))
45
+ target = open(langfilepath, 'w')
46
+ target.puts("<resources>")
47
+
48
+ sheetcontent_list.each do |sheetcontent|
49
+ target.puts(" <!-- #{sheetcontent.comment} -->")
50
+ contentlist = sheetcontent.getRowInfoSortByKey()
51
+ contentlist.each do |rowinfo|
52
+ content = rowinfo.content_dict[lang]
53
+ value = ContentUtil.addEscape(platform,content)
54
+ target.puts(" <string name=\"#{rowinfo.key_str.downcase}\">#{value}</string>")
55
+ end
56
+ target.puts("")
57
+ end
58
+
59
+ target.puts("</resources>")
60
+ target.close
61
+ end
62
+
63
+ end
64
+ end
65
+
66
+ # attr_accessor :link, :platform, :keystr, :langlist, :langfilepathlist, :xlsxpath