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