pixab 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9d4f753ccc7140be05570069eb1bf3507c48be7140fabc614160651ce830408
4
- data.tar.gz: f7e77b2d31251597031ecfb909118483d507f69d39a435d47d91ffe92c6d55c4
3
+ metadata.gz: 719c921d81119297c694e5f6a1553534785bb5dce889db4965e54a37735b847a
4
+ data.tar.gz: 2d0e66a1af711b7c0ed0bfe4cd1e07de18ff3a23751033b9820055ae27b4c62b
5
5
  SHA512:
6
- metadata.gz: 100f235e8f134a17d5bd456ef2b34fb9d4f7f299921e899111858999bb3d6e227442784cbdecc624f3689de7f325870dbbe12d4bd43f71e95e7525ae39eb77ad
7
- data.tar.gz: 598b5ff4d12027d759250a0a3c750362492dc5082563696e6e797c8327116aa31daeb8ea4f51db9ff81115eeaae069e05a50e68b7294f047be075e456d4f1c1d
6
+ metadata.gz: 81c66b5eb3a53bc1ae0d028c376dfb0352962264588a800f346edb6f7bc2416554f071857e6dbcf8fd8970f72c2bbda2ecc69598283624d0ad83e1e9055213ba
7
+ data.tar.gz: 790ebdb889298e22b7e21343a88283c421127c128de29d6651527c625562cebfa104d6a114d1185b1cd293c6494c83ca512f7093bf81c6cdaadf4ce3b11357f3
data/exe/pixab CHANGED
@@ -7,8 +7,11 @@ require_relative '../lib/MergeRequest.rb'
7
7
  require_relative '../lib/ComponentSynchronizer.rb'
8
8
  require_relative '../lib/Localization.rb'
9
9
  require_relative '../lib/Package.rb'
10
+ require_relative '../lib/LocalizationSmartcat.rb'
10
11
 
11
12
  case ARGV[0]
13
+ when '--version'
14
+ puts Pixab::VERSION
12
15
  when 'merge'
13
16
  Pixab::MergeRequest.new(Pixab::RepoManager.new, ARGV[1..-1]).run
14
17
  when 'sync'
@@ -29,12 +32,12 @@ when 'sync'
29
32
  end
30
33
  Pixab::MergeRequest.new(repo_manager, commands).run
31
34
  end
32
- when 'localize'
35
+ when 'localizePhrase'
33
36
  Pixab::Localization.new.run(ARGV[1..-1])
34
37
  when 'package'
35
38
  Pixab::Package.new.run
36
- when '--version'
37
- puts Pixab::VERSION
39
+ when 'localize'
40
+ Pixab::LocalizationSmartcat.new.run(ARGV[1..-1])
38
41
  else
39
42
  puts "Invalid command".red
40
43
  end
@@ -0,0 +1,244 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'zip'
4
+ require 'colored2'
5
+ require 'rexml/document'
6
+ include REXML
7
+
8
+ module Pixab
9
+
10
+ class LocalizationSmartcat
11
+
12
+ # 设置基本认证信息
13
+ USERNAME = '4c8b26ac-ff12-427f-a460-afaacbb3fa3c'
14
+ PASSWORD = '6_nDd3whcOZQHv5dPAusbq5Wmfl'
15
+ Localization_FILE_NAME = 'Localization.zip'
16
+
17
+ Project_AirBrush = '6cd2db15-6325-43ae-9087-f78aca9bec9a'
18
+
19
+ attr_accessor :projects, :tags, :platform, :collections
20
+
21
+ def initialize()
22
+ @projects = Project_AirBrush
23
+ @collections = 'main'
24
+ end
25
+
26
+ def run(commands = nil)
27
+ commands.each_index do |index|
28
+ command = commands[index]
29
+ case command
30
+ when '--ab-android'
31
+ @platform = 'android'
32
+ @tags = 'android'
33
+ @collections = 'AirBrush'
34
+ when '--ab-iOS'
35
+ @platform = 'iOS'
36
+ @tags = 'iOS'
37
+ @collections = 'AirBrush'
38
+ end
39
+ end
40
+
41
+ commands.each_index do |index|
42
+ command = commands[index]
43
+ case command
44
+ when '--projects'
45
+ @projects = commands[index + 1]
46
+ when '--tags'
47
+ @tags = commands[index + 1]
48
+ when '--platform'
49
+ @platform = commands[index + 1]
50
+ when '--collections'
51
+ @collections = commands[index + 1]
52
+ end
53
+ end
54
+
55
+ export_url = "https://smartcat.com/api/integration/v2/project/#{projects}/export"
56
+ export_params = generate_export_params
57
+ download_url = 'https://smartcat.com/api/integration/v1/document/export'
58
+
59
+ puts "\n》》》》》正在导出本地化文案 》》》》》》》》》》\n".green
60
+ export_id = fetch_export_id(export_url, export_params)
61
+ export_id = export_id.tr('"','')
62
+ puts "\n》》》》》正在下载本地化文案 》》》》》》》》》》\n".green
63
+ if download_zip_file_with_retry(download_url, export_id)
64
+ puts "\n》》》》》正在替换本地化文案 》》》》》》》》》》\n".green
65
+ unzip_file(Localization_FILE_NAME)
66
+ puts "\n》》》》》本地化文案更新已完成 》》》》》》》》》》\n".green
67
+ end
68
+
69
+ end
70
+
71
+ # 第一步:通过POST请求获取export ID
72
+ def fetch_export_id(export_url, export_params)
73
+ uri = URI(export_url)
74
+ uri.query = URI.encode_www_form(export_params)
75
+
76
+ request = Net::HTTP::Post.new(uri)
77
+ request.basic_auth(USERNAME,PASSWORD)
78
+
79
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
80
+ http.request(request)
81
+ end
82
+
83
+ return response.body
84
+ end
85
+
86
+ # 第二步:循环尝试下载ZIP文件
87
+ def download_zip_file_with_retry(download_url, export_id, max_retries=30)
88
+ # 加载动画字符
89
+ spinner = Enumerator.new do |e|
90
+ loop do
91
+ e.yield '|'
92
+ e.yield '/'
93
+ e.yield '-'
94
+ e.yield '\\'
95
+ end
96
+ end
97
+
98
+ retries = 0
99
+ while retries < max_retries
100
+ uri = URI("#{download_url}/#{export_id}")
101
+ request = Net::HTTP::Get.new(uri)
102
+ request.basic_auth(USERNAME,PASSWORD)
103
+
104
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
105
+ http.request(request)
106
+ end
107
+
108
+ if response.code == "200"
109
+ File.open(Localization_FILE_NAME, "wb") { |file| file.write(response.body) }
110
+ return true
111
+ else
112
+ # 等待1秒后重试
113
+ # 打印动画字符
114
+ print "\r#{spinner.next}"
115
+ sleep 0.5
116
+ print "\r#{spinner.next}"
117
+ sleep 0.5
118
+ retries += 1
119
+ end
120
+ end
121
+
122
+ puts "Failed to download after #{max_retries} retries. Export ID: #{export_id}"
123
+ return false
124
+ end
125
+
126
+ # 第三步:解压缩ZIP文件
127
+ def unzip_file(zip_path)
128
+ Zip::File.open(zip_path) do |zip_file|
129
+ if platform == 'android'
130
+ unzip_file_android(zip_file)
131
+ else
132
+ unzip_file_iOS(zip_file)
133
+ end
134
+ end
135
+
136
+ File.delete(zip_path) if File.exist?(zip_path)
137
+
138
+ end
139
+
140
+ def unzip_file_iOS(zip_file)
141
+ zip_file.each do |f|
142
+ if is_ignored_file_path(f.name)
143
+ next
144
+ end
145
+ f_path = extract_localization_file_path(f.name)
146
+ FileUtils.mkdir_p(File.dirname(f_path))
147
+ content = f.get_input_stream.read
148
+ localization_content = content.gsub(/=\s*".*";/) do |match|
149
+ match.gsub('%s', '%@')
150
+ end
151
+ File.write(f_path, localization_content)
152
+ end
153
+ end
154
+
155
+ def unzip_file_android(zip_file)
156
+ zip_file.each do |f|
157
+ if is_ignored_file_path(f.name)
158
+ next
159
+ end
160
+ f_path = extract_localization_file_path(f.name)
161
+ FileUtils.mkdir_p(File.dirname(f_path))
162
+ content = f.get_input_stream.read
163
+ document = Document.new(content)
164
+ # 遍历所有文本节点
165
+ XPath.each(document, '//text()') do |text_node|
166
+ # 对文本中的单双引号进行转义处理
167
+ text_node.value = text_node.value.gsub(/['"]/, '\0')
168
+ end
169
+ # 将修改后的XML内容写入新文件
170
+ File.open(f_path, 'w') do |file|
171
+ document.write(file)
172
+ end
173
+ end
174
+ end
175
+
176
+ def generate_export_params()
177
+
178
+ export_params = {
179
+ 'collections' => collections,
180
+ 'completion-state' => 'final',
181
+ 'fallback-to-default-language' => nil,
182
+ 'include-default-language' => nil,
183
+ }
184
+
185
+ if !platform.nil?
186
+ format = nil
187
+ template = nil
188
+ if platform == 'android'
189
+ format = 'android-xml'
190
+ template = '{LOCALE:ANDROID}/strings_ph.xml'
191
+ else
192
+ format = 'ios-strings'
193
+ template = '{LOCALE:IOS}.lproj/Localizable.strings'
194
+ end
195
+ export_params['format'] = format
196
+ export_params['output-file-path-template'] = template
197
+ end
198
+
199
+ if !tags.nil?
200
+ export_params['labels'] = tags
201
+ end
202
+
203
+ return export_params
204
+ end
205
+
206
+ def extract_localization_file_path(zip_file_path)
207
+ if platform.nil? || platform != 'android'
208
+ return zip_file_path
209
+ end
210
+
211
+
212
+ path = File.dirname(zip_file_path)
213
+ localization = ''
214
+ case path
215
+ when 'en'
216
+ localization = ''
217
+ when 'fr'
218
+ localization = '-fr-rFR'
219
+ when 'ru'
220
+ localization = '-ru-rRU'
221
+ when 'zh-rHans'
222
+ localization = '-zh-rCN'
223
+ when 'tr'
224
+ localization = '-tr-rTR'
225
+ when 'pt-rBR'
226
+ localization = '-pt'
227
+ else
228
+ localization = "-#{path}"
229
+ end
230
+ return "values#{localization}/#{File.basename(zip_file_path)}"
231
+ end
232
+
233
+ def is_ignored_file_path(file_path)
234
+ if platform != 'android'
235
+ return false
236
+ end
237
+
238
+ path = File.dirname(file_path)
239
+ return path == 'zh-rHant' ? true : false
240
+ end
241
+
242
+ end
243
+
244
+ end
data/lib/pixab/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pixab
4
- VERSION = "1.6.1"
4
+ VERSION = "1.7.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixab
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 廖再润
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-08 00:00:00.000000000 Z
11
+ date: 2023-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colored2
@@ -43,6 +43,7 @@ files:
43
43
  - lib/GitUtils.rb
44
44
  - lib/Localization.rb
45
45
  - lib/LocalizationPlatform.rb
46
+ - lib/LocalizationSmartcat.rb
46
47
  - lib/MergeRequest.rb
47
48
  - lib/Package.rb
48
49
  - lib/RepoManager.rb