##// END OF EJS Templates
add description that locales:remove_key rake task does not work on Ruby 1.8.6....
Toshi MARUYAMA -
r6514:ec377438b248
parent child
Show More
@@ -1,116 +1,119
1 1 desc 'Updates and checks locales against en.yml'
2 2 task :locales do
3 3 %w(locales:update locales:check_interpolation).collect do |task|
4 4 Rake::Task[task].invoke
5 5 end
6 6 end
7 7
8 8 namespace :locales do
9 9 desc 'Updates language files based on en.yml content (only works for new top level keys).'
10 10 task :update do
11 11 dir = ENV['DIR'] || './config/locales'
12 12
13 13 en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
14 14
15 15 files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
16 16 files.each do |file|
17 17 puts "Updating file #{file}"
18 18 file_strings = YAML.load(File.read(file))
19 19 file_strings = file_strings[file_strings.keys.first]
20 20
21 21 missing_keys = en_strings.keys - file_strings.keys
22 22 next if missing_keys.empty?
23 23
24 24 puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})"
25 25 lang = File.open(file, 'a')
26 26
27 27 missing_keys.each do |key|
28 28 {key => en_strings[key]}.to_yaml.each_line do |line|
29 29 next if line =~ /^---/ || line.empty?
30 30 puts " #{line}"
31 31 lang << " #{line}"
32 32 end
33 33 end
34 34
35 35 lang.close
36 36 end
37 37 end
38 38
39 39 desc 'Checks interpolation arguments in locals against en.yml'
40 40 task :check_interpolation do
41 41 dir = ENV['DIR'] || './config/locales'
42 42 en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
43 43 files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
44 44 files.each do |file|
45 45 file_strings = YAML.load(File.read(file))
46 46 file_strings = file_strings[file_strings.keys.first]
47 47
48 48 file_strings.each do |key, string|
49 49 next unless string.is_a?(String)
50 50 string.scan /%\{\w+\}/ do |match|
51 51 unless en_strings[key].nil? || en_strings[key].include?(match)
52 52 puts "#{file}: #{key} uses #{match} not found in en.yml"
53 53 end
54 54 end
55 55 end
56 56 end
57 57 end
58 58
59 59 desc <<-END_DESC
60 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.
63 You need to use Ruby 1.8.7 or later.
64
62 65 Options:
63 66 key=key_1,key_2 Comma-separated list of keys to delete
64 67 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
65 68 END_DESC
66 69
67 70 task :remove_key do
68 71 dir = ENV['DIR'] || './config/locales'
69 72 files = Dir.glob(File.join(dir,'*.yml'))
70 73 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
71 74 deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
72 75 # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
73 76 delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
74 77
75 78 files.each do |path|
76 79 # Skip certain locales
77 80 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
78 81 puts "Deleting selected keys from #{path}"
79 82 orig_content = File.open(path, 'r') {|file| file.read}
80 83 File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
81 84 end
82 85 end
83 86
84 87 desc <<-END_DESC
85 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).
86 89
87 90 Options:
88 91 key="some_key=foo"
89 92 key1="another_key=bar"
90 93 key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
91 94 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
92 95 END_DESC
93 96
94 97 task :add_key do
95 98 dir = ENV['DIR'] || './config/locales'
96 99 files = Dir.glob(File.join(dir,'*.yml'))
97 100 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
98 101 keys_regex = /\Akey(\d+|_.+)?\z/
99 102 adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
100 103 key_list = adds.collect {|v| v[0]}.join(", ")
101 104
102 105 files.each do |path|
103 106 # Skip certain locales
104 107 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
105 108 # TODO: Check for dupliate/existing keys
106 109 puts "Adding #{key_list} to #{path}"
107 110 File.open(path, 'a') do |file|
108 111 adds.each do |kv|
109 112 Hash[*kv].to_yaml.each_line do |line|
110 113 file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
111 114 end
112 115 end
113 116 end
114 117 end
115 118 end
116 119 end
General Comments 0
You need to be logged in to leave comments. Login now