-
Notifications
You must be signed in to change notification settings - Fork 5
/
localize.rb
executable file
·49 lines (37 loc) · 969 Bytes
/
localize.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
#!/usr/bin/env ruby
require 'json'
require 'set'
require 'tempfile'
def sh(cmd)
out = `#{cmd}`
raise unless $?.success?
out
end
bundle_dir = ARGV[0]
Dir.chdir(bundle_dir)
keys_set = Set.new
lang_to_map = {}
Dir["*.lproj/Localizable.strings"].each do |strings_file|
json = sh("plutil -convert json -o - #{strings_file}")
lang = strings_file.match(/(.*)\.lproj/)[1]
map = JSON.parse(json)
lang_to_map[lang] = map
keys_set += map.keys
end
keys = keys_set.to_a
keys.sort!
out_dir = "#{bundle_dir}/localization"
`rm -r #{out_dir} 2> /dev/null`
sh("mkdir #{out_dir}")
def write_compressed(out, str)
Tempfile.create('localization_values') do |file|
file.write(str)
file.close
sh("./compress #{file.path} #{out}")
end
end
write_compressed("#{out_dir}/keys.json.lzfse", keys.to_json)
lang_to_map.each do |lang, map|
values = keys.map { |key| map[key] }
write_compressed("#{out_dir}/#{lang}.values.json.lzfse", values.to_json)
end