-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlangToLangCompare.rb
More file actions
163 lines (110 loc) · 2.83 KB
/
langToLangCompare.rb
File metadata and controls
163 lines (110 loc) · 2.83 KB
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
#!/usr/bin/env ruby
# encoding: UTF-8
require 'fileutils'
ORIGINAL_DIR = "CurrentLocalization"
GENERATED_DIR = "Generated/Localization"
OUTPUT_FOLDER = "Generated"
DIFF_FILE = "#{OUTPUT_FOLDER}/localization_diff.txt"
# ----------------------------
# Parse .strings file
# ----------------------------
def parse_strings(path)
data = {}
return data unless File.exist?(path)
File.readlines(path, encoding: "UTF-8").each do |line|
line.strip!
next if line.empty?
next if line.start_with?("//")
# "KEY" = "VALUE";
if line =~ /^"(.*)"\s*=\s*"(.*)";$/
key = Regexp.last_match(1)
val = Regexp.last_match(2)
data[key] = val
end
end
data
end
# ----------------------------
# Find languages
# ----------------------------
def find_languages(dir)
langs = []
return langs unless Dir.exist?(dir)
Dir.entries(dir).each do |d|
if d.end_with?(".lproj")
langs << d.gsub(".lproj", "")
end
end
langs.sort
end
# ----------------------------
# Main
# ----------------------------
orig_langs = find_languages(ORIGINAL_DIR)
gen_langs = find_languages(GENERATED_DIR)
all_langs = (orig_langs + gen_langs).uniq.sort
if all_langs.empty?
abort "No localization folders found"
end
FileUtils.mkdir_p(OUTPUT_FOLDER)
diffs = []
diffs << "Localization Diff Report"
diffs << "========================"
diffs << "Generated at: #{Time.now}"
diffs << ""
# ----------------------------
# Compare each language
# ----------------------------
all_langs.each do |lang|
diffs << "Language: #{lang}"
diffs << "-" * 40
orig_file = "#{ORIGINAL_DIR}/#{lang}.lproj/Localizable.strings"
gen_file = "#{GENERATED_DIR}/#{lang}.lproj/Localizable.strings"
unless File.exist?(orig_file)
diffs << " ❗ Missing original file - #{orig_file}"
diffs << ""
next
end
unless File.exist?(gen_file)
diffs << " ❗ Missing generated file - #{gen_file}"
diffs << ""
next
end
orig_data = parse_strings(orig_file)
gen_data = parse_strings(gen_file)
all_keys = (orig_data.keys + gen_data.keys).uniq.sort
has_diff = false
all_keys.each do |key|
o = orig_data[key]
g = gen_data[key]
# Missing in generated
if o && !g
diffs << " [-] Removed: #{key}"
diffs << " Original: #{o}"
has_diff = true
# New in generated
elsif !o && g
diffs << " [+] Added: #{key}"
diffs << " New: #{g}"
has_diff = true
# Changed
elsif o != g
diffs << " [*] Changed: #{key}"
diffs << " Original: #{o}"
diffs << " New: #{g}"
has_diff = true
end
end
unless has_diff
diffs << " ✓ No differences"
end
diffs << ""
end
# ----------------------------
# Write diff file
# ----------------------------
File.open(DIFF_FILE, "w", encoding: "UTF-8") do |f|
f.puts diffs.join("\n")
end
puts "Diff file created:"
puts DIFF_FILE