-
Notifications
You must be signed in to change notification settings - Fork 51
/
Rakefile
80 lines (69 loc) · 2.48 KB
/
Rakefile
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
namespace :i18n do
desc "Invalidate locales one-by-one"
task :invalidate do
master = :en
basedir = File.join(Dir.pwd, "locales")
available = Dir.glob(File.join(basedir, "*.yml"))
translations = available - [master]
identation = " " # ._.
while true
puts "Path to invalidate:"
path = STDIN.gets.strip
exit if %w(q quit exit).include?(path)
confirm = []
results = Hash.new
translations.each do |translation|
locale = File.basename(translation, ".yml")
lines = File.read(translation).split("\n")
result = Hash.new
level = 0
full_path = [locale] + path.split(".")
lines.each_with_index do |text, line|
next if text.empty? # Atom cleans up blank lines
current_identation = " "*level
if !text.start_with?(current_identation)
spaces = text[/^[\s]*/, 1]
level = spaces ? spaces.split(identation).length : 0
next
end
if text.start_with?(current_identation+full_path[level]+":")
if level == full_path.length-1
result[:lines] = [line]
result[:texts] = [text]
lines[line+1..-1].each_with_index do |child_text, child_line|
if child_text.start_with?(current_identation+identation)
result[:lines] << line + child_line + 1
result[:texts] << child_text
else
break
end
end
break
else
level += 1
end
end
end
if result[:lines]
multiline = result[:lines].length > 1
result[:texts].map! { |x| x.gsub(/^[\s]{#{full_path.length}}/, "") }
confirm << "#{locale}.yml:#{multiline ? "#{result[:lines].first+1}..#{result[:lines].last+1}" : result[:lines].first+1}"
confirm << (multiline ? "\n#{result[:texts].join("\n")}\n\n" : " - #{result[:texts].first.strip}\n")
results[translation] = result
end
end
if confirm.any?
puts confirm.join(""), "\n", "Confirm? y/n"
if STDIN.gets.strip == "y"
results.each do |translation, result|
lines = File.read(translation).split("\n")
result[:lines].each { |x| lines[x] = nil }
File.open(translation, "w") { |f| f.write(lines.compact.join("\n")+"\n") }
end
puts "Done."
end
puts
end
end
end
end