-
Notifications
You must be signed in to change notification settings - Fork 31
/
lokalise.rb
175 lines (159 loc) Β· 8.04 KB
/
lokalise.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
87
88
89
90
91
92
93
94
95
96
97
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
module Fastlane
module Actions
class LokaliseAction < Action
def self.run(params)
require 'net/http'
token = params[:api_token]
project_identifier = params[:project_identifier]
destination = params[:destination]
clean_destination = params[:clean_destination]
include_comments = params[:include_comments]
original_filenames = params[:use_original]
body = {
format: "ios_sdk",
original_filenames: original_filenames,
bundle_filename: "Localization.zip",
bundle_structure: "%LANG_ISO%.lproj/Localizable.%FORMAT%",
export_empty_as: "base",
export_sort: "first_added",
include_comments: include_comments,
replace_breaks: false
}
filter_langs = params[:languages]
if filter_langs.kind_of? Array then
body["filter_langs"] = filter_langs
end
tags = params[:tags]
if tags.kind_of? Array then
body["include_tags"] = tags
end
uri = URI("https://api.lokalise.com/api2/projects/#{project_identifier}/files/download")
request = Net::HTTP::Post.new(uri)
request.body = body.to_json
request.add_field("x-api-token", token)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.request(request)
jsonResponse = JSON.parse(response.body)
UI.error "Bad response π\n#{response.body}" unless jsonResponse.kind_of? Hash
if response.code == "200" && jsonResponse["bundle_url"].kind_of?(String) then
UI.message "Downloading localizations archive π¦"
FileUtils.mkdir_p("lokalisetmp")
fileURL = jsonResponse["bundle_url"]
uri = URI(fileURL)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
zipRequest = Net::HTTP::Get.new(uri)
response = http.request(zipRequest)
if response.content_type == "application/zip" or response.content_type == "application/octet-stream" then
FileUtils.mkdir_p("lokalisetmp")
open("lokalisetmp/a.zip", "wb") { |file|
file.write(response.body)
}
unzip_file("lokalisetmp/a.zip", destination, clean_destination)
FileUtils.remove_dir("lokalisetmp")
UI.success "Localizations extracted to #{destination} π π π"
else
UI.error "Response did not include ZIP"
end
elsif jsonResponse["error"].kind_of? Hash
code = jsonResponse["error"]["code"]
message = jsonResponse["error"]["message"]
UI.error "Response error code #{code} (#{message}) π"
else
UI.error "Bad response π\n#{jsonResponse}"
end
end
def self.unzip_file(file, destination, clean_destination)
require 'zip'
require 'rubygems'
Zip::File.open(file) { |zip_file|
if clean_destination then
UI.message "Cleaning destination folder β»οΈ"
FileUtils.remove_dir(destination)
FileUtils.mkdir_p(destination)
end
UI.message "Unarchiving localizations to destination π"
zip_file.each { |f|
f_path= File.join(destination, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
FileUtils.rm(f_path) if File.file? f_path
zip_file.extract(f, f_path)
}
}
end
#####################################################
# @!group Documentation
#####################################################
def self.description
"Download Lokalise localization"
end
def self.available_options
[
FastlaneCore::ConfigItem.new(key: :api_token,
env_name: "LOKALISE_API_TOKEN",
description: "API Token for Lokalise",
verify_block: proc do |value|
UI.user_error! "No API token for Lokalise given, pass using `api_token: 'token'`" unless (value and not value.empty?)
end),
FastlaneCore::ConfigItem.new(key: :project_identifier,
env_name: "LOKALISE_PROJECT_ID",
description: "Lokalise Project ID",
verify_block: proc do |value|
UI.user_error! "No Project Identifier for Lokalise given, pass using `project_identifier: 'identifier'`" unless (value and not value.empty?)
end),
FastlaneCore::ConfigItem.new(key: :destination,
description: "Localization destination",
verify_block: proc do |value|
UI.user_error! "Things are pretty bad" unless (value and not value.empty?)
UI.user_error! "Directory you passed is in your imagination" unless File.directory?(value)
end),
FastlaneCore::ConfigItem.new(key: :clean_destination,
description: "Clean destination folder",
optional: true,
is_string: false,
default_value: false,
verify_block: proc do |value|
UI.user_error! "Clean destination should be true or false" unless [true, false].include? value
end),
FastlaneCore::ConfigItem.new(key: :languages,
description: "Languages to download",
optional: true,
is_string: false,
verify_block: proc do |value|
UI.user_error! "Language codes should be passed as array" unless value.kind_of? Array
end),
FastlaneCore::ConfigItem.new(key: :include_comments,
description: "Include comments in exported files",
optional: true,
is_string: false,
default_value: false,
verify_block: proc do |value|
UI.user_error! "Include comments should be true or false" unless [true, false].include? value
end),
FastlaneCore::ConfigItem.new(key: :use_original,
description: "Use original filenames/formats (bundle_structure parameter is ignored then)",
optional: true,
is_string: false,
default_value: false,
verify_block: proc do |value|
UI.user_error! "Use original should be true of false." unless [true, false].include?(value)
end),
FastlaneCore::ConfigItem.new(key: :tags,
description: "Include only the keys tagged with a given set of tags",
optional: true,
is_string: false,
verify_block: proc do |value|
UI.user_error! "Tags should be passed as array" unless value.kind_of? Array
end),
]
end
def self.authors
"Fedya-L"
end
def self.is_supported?(platform)
[:ios, :mac].include? platform
end
end
end
end