-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathtranslation_coverage.rb
More file actions
executable file
·315 lines (255 loc) · 8.9 KB
/
translation_coverage.rb
File metadata and controls
executable file
·315 lines (255 loc) · 8.9 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'yaml'
require 'json'
require 'optparse'
# Translation Coverage Analyzer for Keep a Changelog
# This utility analyzes which sections are translated in each language version
# and identifies missing translations compared to the English baseline.
class TranslationCoverageAnalyzer
SOURCE_DIR = File.join(__dir__, 'source')
# Known versions in order (newest first)
VERSIONS = ['1.1.0', '1.0.0', '0.3.0'].freeze
def initialize(options = {})
@options = options
@version_filter = options[:version]
@language_filter = options[:language]
@format = options[:format] || 'text'
@show_details = options[:details]
end
def analyze
results = {
analyzed_at: Time.now.utc.strftime('%Y-%m-%d %H:%M:%S UTC'),
versions: {}
}
versions_to_check.each do |version|
results[:versions][version] = analyze_version(version)
end
results
end
def report
results = analyze
case @format
when 'json'
print_json_report(results)
when 'csv'
print_csv_report(results)
else
print_text_report(results)
end
end
private
def versions_to_check
@version_filter ? [@version_filter] : VERSIONS
end
def analyze_version(version)
english_sections = extract_sections('en', version)
return nil if english_sections.nil?
# Check if this version uses markdown (0.3.0) - sections won't have explicit IDs
uses_markdown = version == '0.3.0'
version_data = {
baseline_sections: english_sections,
section_count: english_sections.length,
uses_markdown: uses_markdown,
languages: {}
}
available_languages(version).each do |lang|
next if lang == 'en' # Skip English baseline
next if @language_filter && lang != @language_filter
lang_sections = extract_sections(lang, version)
next if lang_sections.nil?
if uses_markdown
# For markdown versions, compare by count only (headings are translated)
coverage_pct = lang_sections.length == english_sections.length ? 100.0 :
(lang_sections.length.to_f / english_sections.length * 100).round(2)
version_data[:languages][lang] = {
section_count: lang_sections.length,
complete_count: lang_sections.length,
missing_count: english_sections.length - lang_sections.length,
coverage_percentage: coverage_pct
}
else
# For HAML versions with IDs, compare specific sections
missing = english_sections - lang_sections
extra = lang_sections - english_sections
complete = lang_sections & english_sections
coverage_pct = english_sections.empty? ? 0.0 : (complete.length.to_f / english_sections.length * 100).round(2)
version_data[:languages][lang] = {
sections: lang_sections,
complete_count: complete.length,
missing_count: missing.length,
missing_sections: missing,
extra_sections: extra,
coverage_percentage: coverage_pct
}
end
end
version_data
end
def extract_sections(language, version)
file_path = File.join(SOURCE_DIR, language, version, 'index.html.haml')
return nil unless File.exist?(file_path)
content = File.read(file_path, encoding: 'UTF-8')
sections = []
# Extract h3 and h4 heading IDs (for versions 1.0.0+)
# Pattern matches: %h3#section-id, %h4#section-id
content.scan(/^\s*%h[34]#([\w-]+)/) do |match|
sections << match[0]
end
# For version 0.3.0, extract markdown headings and generate IDs
if sections.empty?
content.scan(/^\s*###\s+(.+?)$/) do |match|
heading = match[0].strip
# Convert heading to slug (e.g., "What's a change log?" -> "whats-a-change-log")
slug = heading.downcase
.gsub(/[''']/, '') # Remove apostrophes
.gsub(/[^a-z0-9\s-]/, '') # Remove non-alphanumeric except spaces and hyphens
.gsub(/\s+/, '-') # Replace spaces with hyphens
.gsub(/-+/, '-') # Replace multiple hyphens with single
.gsub(/^-|-$/, '') # Remove leading/trailing hyphens
sections << slug unless slug.empty?
end
end
sections
end
def available_languages(version)
Dir.glob(File.join(SOURCE_DIR, '*', version))
.select { |path| File.directory?(path) }
.map { |path| File.basename(File.dirname(path)) }
.sort
end
def print_text_report(results)
puts "=" * 80
puts "TRANSLATION COVERAGE REPORT"
puts "Generated: #{results[:analyzed_at]}"
puts "=" * 80
puts
results[:versions].each do |version, data|
next if data.nil?
print_version_report(version, data)
end
end
def print_version_report(version, data)
puts "-" * 80
puts "VERSION: #{version}"
puts "-" * 80
puts "Baseline (English) has #{data[:section_count]} sections"
if data[:uses_markdown]
puts "Note: This version uses markdown format with translated headings."
puts "Coverage is based on section count, not specific section IDs."
end
puts
if @show_details && !data[:uses_markdown]
puts "Sections: " + data[:baseline_sections].join(", ")
puts
end
if data[:languages].empty?
puts "No translations found for this version."
puts
return
end
# Summary table
puts
puts "COVERAGE SUMMARY:"
puts "-" * 80
printf "%-15s %10s %10s %12s\n", "Language", "Complete", "Missing", "Coverage"
puts "-" * 80
sorted_languages = data[:languages].sort_by { |_, info| -info[:coverage_percentage] }
sorted_languages.each do |lang, info|
status_indicator = case info[:coverage_percentage]
when 100 then "✓"
when 75..99 then "●"
when 50..74 then "◐"
else "○"
end
printf "%-15s %8d %8d %9.1f%% %s\n",
lang,
info[:complete_count],
info[:missing_count],
info[:coverage_percentage],
status_indicator
end
puts "-" * 80
puts
# Detailed missing sections (only for non-markdown versions)
if @show_details && !data[:uses_markdown]
puts "MISSING SECTIONS BY LANGUAGE:"
puts "-" * 80
sorted_languages.each do |lang, info|
next if info[:missing_sections].nil? || info[:missing_sections].empty?
puts "#{lang}:"
info[:missing_sections].each do |section|
puts " - #{section}"
end
unless info[:extra_sections].nil? || info[:extra_sections].empty?
puts " Extra sections (not in English):"
info[:extra_sections].each do |section|
puts " + #{section}"
end
end
puts
end
puts
end
# Summary statistics
total_translations = data[:languages].count
if total_translations > 0
complete_translations = data[:languages].count { |_, info| info[:coverage_percentage] == 100 }
avg_coverage = data[:languages].values.sum { |info| info[:coverage_percentage] } / total_translations
puts "STATISTICS:"
puts " Total translations: #{total_translations}"
puts " Complete (100%): #{complete_translations}"
puts " Partial: #{total_translations - complete_translations}"
puts " Average coverage: #{avg_coverage.round(2)}%"
puts
end
end
def print_json_report(results)
puts JSON.pretty_generate(results)
end
def print_csv_report(results)
puts "Version,Language,Complete,Missing,Total,Coverage %"
results[:versions].each do |version, data|
next if data.nil?
data[:languages].each do |lang, info|
puts [
version,
lang,
info[:complete_count],
info[:missing_count],
data[:section_count],
info[:coverage_percentage]
].join(',')
end
end
end
end
# CLI Interface
if __FILE__ == $PROGRAM_NAME
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: translation_coverage.rb [options]"
opts.separator ""
opts.separator "Analyzes translation coverage for Keep a Changelog"
opts.separator ""
opts.separator "Options:"
opts.on("-v", "--version VERSION", "Analyze specific version (1.1.0, 1.0.0, or 0.3.0)") do |v|
options[:version] = v
end
opts.on("-l", "--language LANGUAGE", "Analyze specific language (e.g., es-ES, fr, de)") do |l|
options[:language] = l
end
opts.on("-f", "--format FORMAT", "Output format: text (default), json, or csv") do |f|
options[:format] = f
end
opts.on("-d", "--details", "Show detailed section-by-section breakdown") do
options[:details] = true
end
opts.on("-h", "--help", "Show this help message") do
puts opts
exit
end
end.parse!
analyzer = TranslationCoverageAnalyzer.new(options)
analyzer.report
end