|
1
|
desc 'Updates and checks locales against en.yml'
|
|
2
|
task :locales do
|
|
3
|
%w(locales:update locales:check_interpolation).collect do |task|
|
|
4
|
Rake::Task[task].invoke
|
|
5
|
end
|
|
6
|
end
|
|
7
|
|
|
8
|
namespace :locales do
|
|
9
|
desc 'Updates language files based on en.yml content (only works for new top level keys).'
|
|
10
|
task :update do
|
|
11
|
dir = ENV['DIR'] || './config/locales'
|
|
12
|
|
|
13
|
en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
|
|
14
|
|
|
15
|
files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
|
|
16
|
files.sort.each do |file|
|
|
17
|
puts "Updating file #{file}"
|
|
18
|
file_strings = YAML.load(File.read(file))
|
|
19
|
file_strings = file_strings[file_strings.keys.first]
|
|
20
|
|
|
21
|
missing_keys = en_strings.keys - file_strings.keys
|
|
22
|
next if missing_keys.empty?
|
|
23
|
|
|
24
|
puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})"
|
|
25
|
lang = File.open(file, 'a')
|
|
26
|
|
|
27
|
missing_keys.each do |key|
|
|
28
|
{key => en_strings[key]}.to_yaml.each_line do |line|
|
|
29
|
next if line =~ /^---/ || line.empty?
|
|
30
|
puts " #{line}"
|
|
31
|
lang << " #{line}"
|
|
32
|
end
|
|
33
|
end
|
|
34
|
|
|
35
|
lang.close
|
|
36
|
end
|
|
37
|
end
|
|
38
|
|
|
39
|
desc 'Checks interpolation arguments in locals against en.yml'
|
|
40
|
task :check_interpolation do
|
|
41
|
dir = ENV['DIR'] || './config/locales'
|
|
42
|
en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
|
|
43
|
files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
|
|
44
|
files.sort.each do |file|
|
|
45
|
puts "parsing #{file}..."
|
|
46
|
file_strings = YAML.load_file(file)
|
|
47
|
unless file_strings.is_a?(Hash)
|
|
48
|
puts "#{file}: content is not a Hash (#{file_strings.class.name})"
|
|
49
|
next
|
|
50
|
end
|
|
51
|
unless file_strings.keys.size == 1
|
|
52
|
puts "#{file}: content has multiple keys (#{file_strings.keys.size})"
|
|
53
|
next
|
|
54
|
end
|
|
55
|
file_strings = file_strings[file_strings.keys.first]
|
|
56
|
|
|
57
|
file_strings.each do |key, string|
|
|
58
|
next unless string.is_a?(String)
|
|
59
|
string.scan /%\{\w+\}/ do |match|
|
|
60
|
unless en_strings[key].nil? || en_strings[key].include?(match)
|
|
61
|
puts "#{file}: #{key} uses #{match} not found in en.yml"
|
|
62
|
end
|
|
63
|
end
|
|
64
|
end
|
|
65
|
end
|
|
66
|
end
|
|
67
|
|
|
68
|
desc <<-END_DESC
|
|
69
|
Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows).
|
|
70
|
|
|
71
|
Options:
|
|
72
|
key=key_1,key_2 Comma-separated list of keys to delete
|
|
73
|
skip=en,de Comma-separated list of locale files to ignore (filename without extension)
|
|
74
|
END_DESC
|
|
75
|
|
|
76
|
task :remove_key do
|
|
77
|
dir = ENV['DIR'] || './config/locales'
|
|
78
|
files = Dir.glob(File.join(dir,'*.yml'))
|
|
79
|
skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
|
|
80
|
deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
|
|
81
|
# Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
|
|
82
|
delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
|
|
83
|
|
|
84
|
files.each do |path|
|
|
85
|
# Skip certain locales
|
|
86
|
(puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
|
|
87
|
puts "Deleting selected keys from #{path}"
|
|
88
|
orig_content = File.open(path, 'r') {|file| file.read}
|
|
89
|
File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
|
|
90
|
end
|
|
91
|
end
|
|
92
|
|
|
93
|
desc <<-END_DESC
|
|
94
|
Adds a new top-level translation string to all locale file (only works for childless keys, probably doesn\'t work on windows, doesn't check for duplicates).
|
|
95
|
|
|
96
|
Options:
|
|
97
|
key="some_key=foo"
|
|
98
|
key1="another_key=bar"
|
|
99
|
key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
|
|
100
|
skip=en,de Comma-separated list of locale files to ignore (filename without extension)
|
|
101
|
END_DESC
|
|
102
|
|
|
103
|
task :add_key do
|
|
104
|
dir = ENV['DIR'] || './config/locales'
|
|
105
|
files = Dir.glob(File.join(dir,'*.yml'))
|
|
106
|
skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
|
|
107
|
keys_regex = /\Akey(\d+|_.+)?\z/
|
|
108
|
adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
|
|
109
|
key_list = adds.collect {|v| v[0]}.join(", ")
|
|
110
|
|
|
111
|
files.each do |path|
|
|
112
|
# Skip certain locales
|
|
113
|
(puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
|
|
114
|
# TODO: Check for dupliate/existing keys
|
|
115
|
puts "Adding #{key_list} to #{path}"
|
|
116
|
File.open(path, 'a') do |file|
|
|
117
|
adds.each do |kv|
|
|
118
|
Hash[*kv].to_yaml.each_line do |line|
|
|
119
|
file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
|
|
120
|
end
|
|
121
|
end
|
|
122
|
end
|
|
123
|
end
|
|
124
|
end
|
|
125
|
|
|
126
|
desc 'Duplicates a key. Exemple rake locales:dup key=foo new_key=bar'
|
|
127
|
task :dup do
|
|
128
|
dir = ENV['DIR'] || './config/locales'
|
|
129
|
files = Dir.glob(File.join(dir,'*.yml'))
|
|
130
|
skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
|
|
131
|
key = ENV['key']
|
|
132
|
new_key = ENV['new_key']
|
|
133
|
abort "Missing key argument" if key.blank?
|
|
134
|
abort "Missing new_key argument" if new_key.blank?
|
|
135
|
|
|
136
|
files.each do |path|
|
|
137
|
# Skip certain locales
|
|
138
|
(puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
|
|
139
|
puts "Adding #{new_key} to #{path}"
|
|
140
|
|
|
141
|
strings = File.read(path)
|
|
142
|
unless strings =~ /^( #{key}: .+)$/
|
|
143
|
puts "Key not found in #{path}"
|
|
144
|
next
|
|
145
|
end
|
|
146
|
line = $1
|
|
147
|
|
|
148
|
File.open(path, 'a') do |file|
|
|
149
|
file.puts(line.sub(key, new_key))
|
|
150
|
end
|
|
151
|
end
|
|
152
|
end
|
|
153
|
|
|
154
|
desc 'Check parsing yaml by psych library on Ruby 1.9.'
|
|
155
|
|
|
156
|
# On Fedora 12 and 13, if libyaml-devel is available,
|
|
157
|
# in case of installing by rvm,
|
|
158
|
# Ruby 1.9 default yaml library is psych.
|
|
159
|
|
|
160
|
task :check_parsing_by_psych do
|
|
161
|
begin
|
|
162
|
require 'psych'
|
|
163
|
parser = Psych::Parser.new
|
|
164
|
dir = ENV['DIR'] || './config/locales'
|
|
165
|
files = Dir.glob(File.join(dir,'*.yml'))
|
|
166
|
files.sort.each do |filename|
|
|
167
|
next if File.directory? filename
|
|
168
|
puts "parsing #{filename}..."
|
|
169
|
begin
|
|
170
|
parser.parse File.open(filename)
|
|
171
|
rescue Exception => e1
|
|
172
|
puts(e1.message)
|
|
173
|
puts("")
|
|
174
|
end
|
|
175
|
end
|
|
176
|
rescue Exception => e
|
|
177
|
puts(e.message)
|
|
178
|
end
|
|
179
|
end
|
|
180
|
end
|