##// END OF EJS Templates
Ruby 1.9: fix typo of rake task name to check parsing yaml by psych library (#8847, #4050)....
Toshi MARUYAMA -
r6529:b2bdfc8ff1dd
parent child
Show More
@@ -1,146 +1,146
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.each do |file|
16 files.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.each do |file|
44 files.each do |file|
45 file_strings = YAML.load(File.read(file))
45 file_strings = YAML.load(File.read(file))
46 file_strings = file_strings[file_strings.keys.first]
46 file_strings = file_strings[file_strings.keys.first]
47
47
48 file_strings.each do |key, string|
48 file_strings.each do |key, string|
49 next unless string.is_a?(String)
49 next unless string.is_a?(String)
50 string.scan /%\{\w+\}/ do |match|
50 string.scan /%\{\w+\}/ do |match|
51 unless en_strings[key].nil? || en_strings[key].include?(match)
51 unless en_strings[key].nil? || en_strings[key].include?(match)
52 puts "#{file}: #{key} uses #{match} not found in en.yml"
52 puts "#{file}: #{key} uses #{match} not found in en.yml"
53 end
53 end
54 end
54 end
55 end
55 end
56 end
56 end
57 end
57 end
58
58
59 desc <<-END_DESC
59 desc <<-END_DESC
60 Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows).
60 Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows).
61
61
62 This task does not work on Ruby 1.8.6.
62 This task does not work on Ruby 1.8.6.
63 You need to use Ruby 1.8.7 or later.
63 You need to use Ruby 1.8.7 or later.
64
64
65 Options:
65 Options:
66 key=key_1,key_2 Comma-separated list of keys to delete
66 key=key_1,key_2 Comma-separated list of keys to delete
67 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
67 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
68 END_DESC
68 END_DESC
69
69
70 task :remove_key do
70 task :remove_key do
71 dir = ENV['DIR'] || './config/locales'
71 dir = ENV['DIR'] || './config/locales'
72 files = Dir.glob(File.join(dir,'*.yml'))
72 files = Dir.glob(File.join(dir,'*.yml'))
73 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
73 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
74 deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
74 deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
75 # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
75 # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
76 delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
76 delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
77
77
78 files.each do |path|
78 files.each do |path|
79 # Skip certain locales
79 # Skip certain locales
80 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
80 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
81 puts "Deleting selected keys from #{path}"
81 puts "Deleting selected keys from #{path}"
82 orig_content = File.open(path, 'r') {|file| file.read}
82 orig_content = File.open(path, 'r') {|file| file.read}
83 File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
83 File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
84 end
84 end
85 end
85 end
86
86
87 desc <<-END_DESC
87 desc <<-END_DESC
88 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).
88 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).
89
89
90 Options:
90 Options:
91 key="some_key=foo"
91 key="some_key=foo"
92 key1="another_key=bar"
92 key1="another_key=bar"
93 key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
93 key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
94 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
94 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
95 END_DESC
95 END_DESC
96
96
97 task :add_key do
97 task :add_key do
98 dir = ENV['DIR'] || './config/locales'
98 dir = ENV['DIR'] || './config/locales'
99 files = Dir.glob(File.join(dir,'*.yml'))
99 files = Dir.glob(File.join(dir,'*.yml'))
100 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
100 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
101 keys_regex = /\Akey(\d+|_.+)?\z/
101 keys_regex = /\Akey(\d+|_.+)?\z/
102 adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
102 adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
103 key_list = adds.collect {|v| v[0]}.join(", ")
103 key_list = adds.collect {|v| v[0]}.join(", ")
104
104
105 files.each do |path|
105 files.each do |path|
106 # Skip certain locales
106 # Skip certain locales
107 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
107 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
108 # TODO: Check for dupliate/existing keys
108 # TODO: Check for dupliate/existing keys
109 puts "Adding #{key_list} to #{path}"
109 puts "Adding #{key_list} to #{path}"
110 File.open(path, 'a') do |file|
110 File.open(path, 'a') do |file|
111 adds.each do |kv|
111 adds.each do |kv|
112 Hash[*kv].to_yaml.each_line do |line|
112 Hash[*kv].to_yaml.each_line do |line|
113 file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
113 file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
114 end
114 end
115 end
115 end
116 end
116 end
117 end
117 end
118 end
118 end
119
119
120 desc 'Check parsing yaml by psych library on Ruby 1.9.'
120 desc 'Check parsing yaml by psych library on Ruby 1.9.'
121
121
122 # On Fedora 12 and 13, if libyaml-devel is available,
122 # On Fedora 12 and 13, if libyaml-devel is available,
123 # in case of installing by rvm,
123 # in case of installing by rvm,
124 # Ruby 1.9 default yaml library is psych.
124 # Ruby 1.9 default yaml library is psych.
125
125
126 task :check_parsing_by_psynch do
126 task :check_parsing_by_psych do
127 begin
127 begin
128 require 'psych'
128 require 'psych'
129 parser = Psych::Parser.new
129 parser = Psych::Parser.new
130 dir = ENV['DIR'] || './config/locales'
130 dir = ENV['DIR'] || './config/locales'
131 files = Dir.glob(File.join(dir,'*.yml'))
131 files = Dir.glob(File.join(dir,'*.yml'))
132 files.each do |filename|
132 files.each do |filename|
133 next if File.directory? filename
133 next if File.directory? filename
134 puts "parsing #{filename}..."
134 puts "parsing #{filename}..."
135 begin
135 begin
136 parser.parse File.open(filename)
136 parser.parse File.open(filename)
137 rescue Exception => e1
137 rescue Exception => e1
138 puts(e1.message)
138 puts(e1.message)
139 puts("")
139 puts("")
140 end
140 end
141 end
141 end
142 rescue Exception => e
142 rescue Exception => e
143 puts(e.message)
143 puts(e.message)
144 end
144 end
145 end
145 end
146 end
146 end
General Comments 0
You need to be logged in to leave comments. Login now