Class: Pixab::LocalizationSmartcat

Inherits:
Object
  • Object
show all
Defined in:
lib/LocalizationSmartcat.rb

Constant Summary collapse

Localization_FILE_NAME =
'Localization.zip'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLocalizationSmartcat

Returns a new instance of LocalizationSmartcat.



16
17
18
19
# File 'lib/LocalizationSmartcat.rb', line 16

def initialize()
  @projects = LocalizationSmartcatInfo::Project_AirBrush
  @collections = 'main'
end

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



14
15
16
# File 'lib/LocalizationSmartcat.rb', line 14

def collections
  @collections
end

#formatObject

Returns the value of attribute format.



14
15
16
# File 'lib/LocalizationSmartcat.rb', line 14

def format
  @format
end

#languagesObject

Returns the value of attribute languages.



14
15
16
# File 'lib/LocalizationSmartcat.rb', line 14

def languages
  @languages
end

#outputObject

Returns the value of attribute output.



14
15
16
# File 'lib/LocalizationSmartcat.rb', line 14

def output
  @output
end

#platformObject

Returns the value of attribute platform.



14
15
16
# File 'lib/LocalizationSmartcat.rb', line 14

def platform
  @platform
end

#projectsObject

Returns the value of attribute projects.



14
15
16
# File 'lib/LocalizationSmartcat.rb', line 14

def projects
  @projects
end

#tagsObject

Returns the value of attribute tags.



14
15
16
# File 'lib/LocalizationSmartcat.rb', line 14

def tags
  @tags
end

Instance Method Details

#download_zip_file_with_retry(download_url, export_id, max_retries = 60) ⇒ Object

第二步:循环尝试下载ZIP文件



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/LocalizationSmartcat.rb', line 98

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



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
270
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
# File 'lib/LocalizationSmartcat.rb', line 237

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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/LocalizationSmartcat.rb', line 83

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_paramsObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/LocalizationSmartcat.rb', line 186

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 tags.nil?
    export_params['labels'] = tags
  end

  return export_params
end

#is_ignored_file_path(file_path) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/LocalizationSmartcat.rb', line 298

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

#run(commands = nil) ⇒ Object



21
22
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
# File 'lib/LocalizationSmartcat.rb', line 21

def run(commands = nil)
  commands.each_index do |index|
    command = commands[index]
    case command
    when '--ab-android'
      @platform = 'android'
      @tags = 'android'
      @collections = 'AirBrush'
      @languages = 'en,ru,tr,de,fr,zh-Hans,zh-Hant,pt-BR,es,ar'
    when '--ab-iOS'
      @platform = 'iOS'
      @tags = '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'
      @tags = 'iOS'
    when '--abv-android'
      @projects = LocalizationSmartcatInfo::Project_AirBrushVideo
      @platform = 'android'
      @tags = 'android'
    end
  end

  commands.each_index do |index|
    command = commands[index]
    case command
    when '--projects'
      @projects = commands[index + 1] 
    when '--tags' 
      @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]
    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》》》》》正在导出本地化文案 》》》》》》》》》》\n".green
  export_id = fetch_export_id(export_url, export_params)
  export_id = export_id.tr('"','')
  puts "\n》》》》》正在下载本地化文案 》》》》》》》》》》\n".green
  if download_zip_file_with_retry(download_url, export_id)
    puts "\n》》》》》正在替换本地化文案 》》》》》》》》》》\n".green
    unzip_file(Localization_FILE_NAME)
    puts "\n》》》》》本地化文案更新已完成 》》》》》》》》》》\n".green
  end

end

#unzip_file(zip_path) ⇒ Object

第三步:解压缩ZIP文件



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/LocalizationSmartcat.rb', line 126

def unzip_file(zip_path)
  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

  File.delete(zip_path) if File.exist?(zip_path)

end

#unzip_file_android(zip_file) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/LocalizationSmartcat.rb', line 168

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
    File.write(f_path, document.to_xml)
  end
end

#unzip_file_common(zip_file) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/LocalizationSmartcat.rb', line 142

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/LocalizationSmartcat.rb', line 151

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
    File.write(f_path, content)
  end
end