Class: Pixab::LocalizationSmartcat
- Inherits:
-
Object
- Object
- Pixab::LocalizationSmartcat
- Defined in:
- lib/localization/smartcat/LocalizationSmartcat.rb
Constant Summary collapse
- Localization_FILE_NAME =
'Localization.zip'
Instance Attribute Summary collapse
-
#collections ⇒ Object
Returns the value of attribute collections.
-
#format ⇒ Object
Returns the value of attribute format.
-
#languages ⇒ Object
Returns the value of attribute languages.
-
#output ⇒ Object
Returns the value of attribute output.
-
#platform ⇒ Object
Returns the value of attribute platform.
-
#projects ⇒ Object
Returns the value of attribute projects.
-
#tags ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
-
#download_zip_file_with_retry(download_url, export_id, max_retries = 60) ⇒ Object
第二步:循环尝试下载ZIP文件.
- #extract_localization_file_path(zip_file_path) ⇒ Object
-
#fetch_export_id(export_url, export_params) ⇒ Object
第一步:通过POST请求获取export ID.
- #generate_export_params ⇒ Object
-
#initialize ⇒ LocalizationSmartcat
constructor
A new instance of LocalizationSmartcat.
- #is_ignored_file_path(file_path) ⇒ Object
- #merge_xml_content(new_document, file_path) ⇒ Object
- #run(commands = nil) ⇒ Object
-
#unzip_file(zip_path) ⇒ Object
第三步:解压缩ZIP文件.
- #unzip_file_android(zip_file) ⇒ Object
- #unzip_file_common(zip_file) ⇒ Object
- #unzip_file_iOS(zip_file) ⇒ Object
Constructor Details
#initialize ⇒ LocalizationSmartcat
Returns a new instance of LocalizationSmartcat.
17 18 19 20 21 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 17 def initialize() @projects = LocalizationSmartcatInfo::Project_AirBrush @collections = 'main' @file_mode = FileMode.new('w') end |
Instance Attribute Details
#collections ⇒ Object
Returns the value of attribute collections.
15 16 17 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 15 def collections @collections end |
#format ⇒ Object
Returns the value of attribute format.
15 16 17 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 15 def format @format end |
#languages ⇒ Object
Returns the value of attribute languages.
15 16 17 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 15 def languages @languages end |
#output ⇒ Object
Returns the value of attribute output.
15 16 17 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 15 def output @output end |
#platform ⇒ Object
Returns the value of attribute platform.
15 16 17 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 15 def platform @platform end |
#projects ⇒ Object
Returns the value of attribute projects.
15 16 17 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 15 def projects @projects end |
#tags ⇒ Object
Returns the value of attribute tags.
15 16 17 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 15 def end |
Instance Method Details
#download_zip_file_with_retry(download_url, export_id, max_retries = 60) ⇒ Object
第二步:循环尝试下载ZIP文件
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 104 def download_zip_file_with_retry(download_url, export_id, max_retries=60) retries = 0 while retries < max_retries uri = URI("#{download_url}/#{export_id}") request = Net::HTTP::Get.new(uri) request.basic_auth(LocalizationSmartcatInfo::USERNAME, LocalizationSmartcatInfo::PASSWORD) response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http| http.request(request) end if response.code == "200" File.open(Localization_FILE_NAME, "wb") { |file| file.write(response.body) } return true else retries += 1 dots = '.' * retries print "\r#{dots}" # 等待1秒后重试 sleep 1 end end puts "\nFailed to download after #{max_retries} retries. Export ID: #{export_id}" return false end |
#extract_localization_file_path(zip_file_path) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 271 def extract_localization_file_path(zip_file_path) case projects when LocalizationSmartcatInfo::Project_AirBrush if platform.nil? || platform != 'android' return zip_file_path end path = File.dirname(zip_file_path) localization = '' case path when 'en' localization = '' when 'fr' localization = '-fr-rFR' when 'ru' localization = '-ru-rRU' when 'zh-rHans' localization = '-zh-rCN' when 'tr' localization = '-tr-rTR' when 'pt-rBR' localization = '-pt' else localization = "-#{path}" end return "values#{localization}/#{File.basename(zip_file_path)}" when LocalizationSmartcatInfo::Project_AirBrushVideo if platform.nil? return zip_file_path end case platform when 'android' path = File.dirname(zip_file_path) localization = '' case path when 'en' localization = '' when 'zh-rHans' localization = '-zh-rCN' when 'zh-rHant' localization = '-zh-rHK' else localization = "-#{path}" end return "values#{localization}/#{File.basename(zip_file_path)}" when 'iOS' path = File.dirname(zip_file_path) localization = zip_file_path case path when 'pt-PT.lproj' localization = File.join('pt.lproj', File.basename(zip_file_path)) end return localization end end return zip_file_path end |
#fetch_export_id(export_url, export_params) ⇒ Object
第一步:通过POST请求获取export ID
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 89 def fetch_export_id(export_url, export_params) uri = URI(export_url) uri.query = URI.encode_www_form(export_params) request = Net::HTTP::Post.new(uri) request.basic_auth(LocalizationSmartcatInfo::USERNAME, LocalizationSmartcatInfo::PASSWORD) response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http| http.request(request) end return response.body end |
#generate_export_params ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 220 def generate_export_params() export_params = { 'collections' => collections, 'completion-state' => 'final', 'fallback-to-default-language' => nil, 'include-default-language' => nil, } final_format = nil final_output = nil unless platform.nil? case platform when 'android' final_format = 'android-xml' if projects == LocalizationSmartcatInfo::Project_AirBrush final_output = '{LOCALE:ANDROID}/strings_ph.xml' else final_output = '{LOCALE:ANDROID}/strings.xml' end when 'iOS' final_format = 'ios-strings' final_output = '{LOCALE:IOS}.lproj/Localizable.strings' end end unless @format.nil? final_format = @format end unless @output.nil? final_output = @output end unless final_format.nil? export_params['format'] = final_format end unless final_output.nil? export_params['output-file-path-template'] = final_output end unless languages.nil? export_params['languages'] = languages end unless .nil? export_params['labels'] = end return export_params end |
#is_ignored_file_path(file_path) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 332 def is_ignored_file_path(file_path) case projects when LocalizationSmartcatInfo::Project_AirBrush return false unless platform == 'android' path = File.dirname(file_path) return path == 'zh-rHant' ? true : false end return false end |
#merge_xml_content(new_document, file_path) ⇒ Object
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 209 def merge_xml_content(new_document, file_path) return new_document unless File.exist?(file_path) old_document = Nokogiri::XML(File.read(file_path)) new_document.root.children.each do |node| old_document.root.add_child(node) end return old_document end |
#run(commands = nil) ⇒ Object
23 24 25 26 27 28 29 30 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/localization/smartcat/LocalizationSmartcat.rb', line 23 def run(commands = nil) commands.each_index do |index| command = commands[index] case command when '--ab-android' @platform = 'android' = 'android' @collections = 'AirBrush' @languages = 'en,ru,tr,de,fr,zh-Hans,zh-Hant,pt-BR,es,ar' when '--ab-iOS' @platform = 'iOS' = 'iOS' @collections = 'AirBrush' @languages = 'en,ru,tr,de,fr,zh-Hans,zh-Hant,pt-BR,es,ar' when '--abv-iOS' @projects = LocalizationSmartcatInfo::Project_AirBrushVideo @platform = 'iOS' = 'iOS' when '--abv-android' @projects = LocalizationSmartcatInfo::Project_AirBrushVideo @platform = 'android' = 'android' end end commands.each_index do |index| command = commands[index] case command when '--projects' @projects = commands[index + 1] when '--tags' = commands[index + 1] when '--platform' @platform = commands[index + 1] when '--collections' @collections = commands[index + 1] when '--languages' @languages = commands[index + 1] when '--format' @format = commands[index + 1] when '--output' @output = commands[index + 1] when '--mode' @file_mode = FileMode.new(commands[index + 1]) end end export_url = "https://smartcat.com/api/integration/v2/project/#{projects}/export" export_params = generate_export_params download_url = 'https://smartcat.com/api/integration/v1/document/export' puts "\n📥 正在导出【#{@collections}】文案".green export_id = fetch_export_id(export_url, export_params) export_id = export_id.tr('"','') unless download_zip_file_with_retry(download_url, export_id) return end unless unzip_file(Localization_FILE_NAME) return end puts "\n✅ #{@collections}文案更新已完成!".green end |
#unzip_file(zip_path) ⇒ Object
第三步:解压缩ZIP文件
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 132 def unzip_file(zip_path) is_success = true begin Zip::File.open(zip_path) do |zip_file| case platform when 'android' unzip_file_android(zip_file) when 'iOS' unzip_file_iOS(zip_file) else unzip_file_common(zip_file) end end rescue Zip::Error => e puts "\nError: #{e.message}".red is_success = false end File.delete(zip_path) if File.exist?(zip_path) return is_success end |
#unzip_file_android(zip_file) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 186 def unzip_file_android(zip_file) zip_file.each do |f| if is_ignored_file_path(f.name) next end f_path = extract_localization_file_path(f.name) FileUtils.mkdir_p(File.dirname(f_path)) content = f.get_input_stream.read document = Nokogiri::XML(content) document.traverse do |node| if node.text? node.content = node.content.gsub(/['"]/, '\\\\\0') end end if @file_mode.is_add document = merge_xml_content(document, f_path) end File.write(f_path, document.to_xml) end end |
#unzip_file_common(zip_file) ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 155 def unzip_file_common(zip_file) zip_file.each do |f| f_path = f.name FileUtils.mkdir_p(File.dirname(f_path)) File.delete(f_path) if File.exist?(f_path) f.extract(f_path) end end |
#unzip_file_iOS(zip_file) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/localization/smartcat/LocalizationSmartcat.rb', line 164 def unzip_file_iOS(zip_file) zip_file.each do |f| if is_ignored_file_path(f.name) next end f_path = extract_localization_file_path(f.name) FileUtils.mkdir_p(File.dirname(f_path)) content = f.get_input_stream.read if projects == LocalizationSmartcatInfo::Project_AirBrush content = content.gsub(/=\s*".*";/) do |match| match.gsub('%s', '%@') end end if @file_mode.is_replace File.write(f_path, content) else File.write(f_path, content, mode: @file_mode.mode) end end end |