-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpo-to-schema.rb
69 lines (55 loc) · 1.45 KB
/
po-to-schema.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
require 'json'
lang_dict = Hash.new
translation = "fr.po"
file = File.new(translation)
pairs = []
begin
line = file.gets
eng = ""
otr = ""
while(line)
if(/^msgid/.match(line))
eng = /^msgid "(.*)"/.match(line)[1]
elsif(/^msgstr/.match(line))
otr = /^msgstr "(.*)"/.match(line)[1]
pairs << [eng, otr]
eng = ""
otr = ""
end
line = file.gets
end
end
pairs.each do |pr|
if(!pr[0].empty? && !pr[1].empty?)
#TODO detect collisions
lang_dict[pr[0]] = pr[1]
end
end
puts "Total Translation Pairs: #{pairs.length}"
puts "Total Translated: #{lang_dict.length}"
src_file = "osc-schema.json"
dst_file = "osc-schema-fr.json"
output = File.open(dst_file, "w+")
json = File.read(src_file)
obj = JSON.parse(json)
obj["parameter"].each do |x|
path = x["path"]
shortname = x["shortname"]
tooltip = x["tooltip"]
options = x["options"]
if(shortname && lang_dict.include?(shortname))
x["shortname"] = lang_dict[shortname]
end
if(tooltip && lang_dict.include?(tooltip))
x["tooltip"] = lang_dict[tooltip]
end
if(options)
(0...options.length).each do |i|
opt = options[i]["value"]
if(opt && lang_dict.include?(opt))
options[i]["value"] = lang_dict[opt]
end
end
end
end
output.puts JSON.pretty_generate(obj)