Class: Pixab::LocalizationSmartcatImport

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLocalizationSmartcatImport

Returns a new instance of LocalizationSmartcatImport.



11
12
13
# File 'lib/LocalizationSmartcatImport.rb', line 11

def initialize()
  @delete_file_after_import = false
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



9
10
11
# File 'lib/LocalizationSmartcatImport.rb', line 9

def collection
  @collection
end

#completion_stateObject

Returns the value of attribute completion_state.



9
10
11
# File 'lib/LocalizationSmartcatImport.rb', line 9

def completion_state
  @completion_state
end

#conflicting_valuesObject

Returns the value of attribute conflicting_values.



9
10
11
# File 'lib/LocalizationSmartcatImport.rb', line 9

def conflicting_values
  @conflicting_values
end

#delete_file_after_importObject

Returns the value of attribute delete_file_after_import.



9
10
11
# File 'lib/LocalizationSmartcatImport.rb', line 9

def delete_file_after_import
  @delete_file_after_import
end

#formatObject

Returns the value of attribute format.



9
10
11
# File 'lib/LocalizationSmartcatImport.rb', line 9

def format
  @format
end

#projectObject

Returns the value of attribute project.



9
10
11
# File 'lib/LocalizationSmartcatImport.rb', line 9

def project
  @project
end

#tagsObject

Returns the value of attribute tags.



9
10
11
# File 'lib/LocalizationSmartcatImport.rb', line 9

def tags
  @tags
end

Instance Method Details

#check_import_status(import_id, max_retries = 60) ⇒ Object

检查文件上传是否成功



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/LocalizationSmartcatImport.rb', line 143

def check_import_status(import_id, max_retries=60)
  # 移除双引号
  import_id = import_id.tr('"','')
  retries = 0
  while retries < max_retries
    import_result_url = "https://smartcat.ai/api/v2/project/import-result/#{import_id}"
    uri = URI(import_result_url)
    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"
      puts response.body
      retries += 1
      sleep(1)
    else
      puts response.body
      return false
    end

  end
end

#generate_import_params_from_file_name(file_name) ⇒ Object

通过文件名称获取上传参数



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/LocalizationSmartcatImport.rb', line 109

def generate_import_params_from_file_name(file_name)
  params = {}

  unless @collection.nil?
    params['collection'] = @collection
  end

  file_base_name = File.basename(file_name, File.extname(file_name))
  params['language'] = file_base_name

  unless @format.nil?
    params['format'] = @format
  end

  unless @tags.nil?
    params['labels'] = @tags
  end

  unless @completion_state.nil?
    params['completion-state'] = @completion_state
  end

  unless @conflicting_values.nil?
    if @conflicting_values == 'skip'
      params['skip-conflicting-values'] = nil
    elsif @conflicting_values == 'overwrite'
      params['overwrite-conflicting-values'] = nil
    end
  end

  return params
end

#import_localization(import_url, import_params, file_path) ⇒ Object

导入本地化文案



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/LocalizationSmartcatImport.rb', line 92

def import_localization(import_url, import_params, file_path)
  uri = URI(import_url)
  uri.query = URI.encode_www_form(import_params)
  response = RestClient::Request.execute(
    method: :post,
    url: uri.to_s,
    user: LocalizationSmartcatInfo::USERNAME,
    password: LocalizationSmartcatInfo::PASSWORD,
    payload: {
      multipart: true,
      file: File.new(file_path, 'rb')
    }
  )
  return response.body
end

#import_localization_from_directory(directory) ⇒ Object

遍历目录上传本地化文案



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
87
88
89
# File 'lib/LocalizationSmartcatImport.rb', line 59

def import_localization_from_directory(directory)
  import_url = "https://smartcat.com/api/integration/v2/project/#{@project}/import"
  entries = []
  Dir.foreach(directory) do |entry|
    if entry.start_with?(".")
      next
    end

    if File.basename(entry, File.extname(entry)) == 'en'
      entries.unshift(entry)
    else
      entries.push(entry)
    end
  end

  puts "\n》》》》》正在上传本地化文案 》》》》》》》》》》\n".green

  entries.each do |entry|
    file_path = "#{directory}/#{entry}"
    import_params = generate_import_params_from_file_name(entry)
    import_id = import_localization(import_url, import_params, file_path)
    puts "#{entry} 正在上传中,上传ID:#{import_id}"
    if @delete_file_after_import
      File.delete(file_path) if File.exist?(file_path)
    end
  end 

  puts "\n》》》》》本地化文案上传已完成 》》》》》》》》》》\n".green
  puts "提示:由于Smartcat后台延迟,上传文案可能需要等待1-2分钟才能在后台全部显示。"

end

#run(commands = nil) ⇒ Object



15
16
17
18
19
20
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
# File 'lib/LocalizationSmartcatImport.rb', line 15

def run(commands = nil)
  commands.each_index do |index|
    command = commands[index]
    case command
    when '--ab'
      @project = LocalizationSmartcatInfo::Project_AirBrush
      @collection = 'AirBrush'
      @tags = 'iOS,android'
      @conflicting_values = 'skip'
    when '--abtest'
      @project = LocalizationSmartcatInfo::Project_AirBrush_test
      @collection = 'merge_test'
      @tags = 'iOS,android'
      @conflicting_values = 'skip'
    end
  end

  commands.each_index do |index|
    command = commands[index]
    case command
    when '--project'
      @project = commands[index + 1]
    when '--collection'
      @collection = commands[index + 1]
    when '--format'
      @format = commands[index + 1]
    when '--tags'
      @tags = commands[index + 1]
    when '--completion-state'
      @completion_state = commands[index + 1]
    when '--conflicting-values'
      @conflicting_values = commands[index + 1]
    when '--delete-file-after-import'
      @delete_file_after_import = true
    when '--keep-file-after-import'
      @delete_file_after_import = false
    end
  end

  import_localization_from_directory(Dir.pwd)

end