comparison lib/tasks/.svn/text-base/locales.rake.svn-base @ 514:7eba09d624db live

Merge
author Chris Cannam
date Thu, 14 Jul 2011 10:50:53 +0100
parents cbce1fd3b1b7
children
comparison
equal deleted inserted replaced
512:b9aebdd7dd40 514:7eba09d624db
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
1 namespace :locales do 8 namespace :locales do
2 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).'
3 task :update do 10 task :update do
4 dir = ENV['DIR'] || './config/locales' 11 dir = ENV['DIR'] || './config/locales'
5 12
6 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']
7 14
8 files = Dir.glob(File.join(dir,'*.{yaml,yml}')) 15 files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
9 files.each do |file| 16 files.each do |file|
10 puts "Updating file #{file}" 17 puts "Updating file #{file}"
11 file_strings = YAML.load(File.read(file)) 18 file_strings = YAML.load(File.read(file))
12 file_strings = file_strings[file_strings.keys.first] 19 file_strings = file_strings[file_strings.keys.first]
13 20
14 missing_keys = en_strings.keys - file_strings.keys 21 missing_keys = en_strings.keys - file_strings.keys
15 next if missing_keys.empty? 22 next if missing_keys.empty?
16 23
17 puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})" 24 puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})"
18 lang = File.open(file, 'a') 25 lang = File.open(file, 'a')
19 26
20 missing_keys.each do |key| 27 missing_keys.each do |key|
21 {key => en_strings[key]}.to_yaml.each_line do |line| 28 {key => en_strings[key]}.to_yaml.each_line do |line|
22 next if line =~ /^---/ || line.empty? 29 next if line =~ /^---/ || line.empty?
23 puts " #{line}" 30 puts " #{line}"
24 lang << " #{line}" 31 lang << " #{line}"
25 end 32 end
26 end 33 end
27 34
28 lang.close 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.each do |file|
45 file_strings = YAML.load(File.read(file))
46 file_strings = file_strings[file_strings.keys.first]
47
48 file_strings.each do |key, string|
49 next unless string.is_a?(String)
50 string.scan /%\{\w+\}/ do |match|
51 unless en_strings[key].nil? || en_strings[key].include?(match)
52 puts "#{file}: #{key} uses #{match} not found in en.yml"
53 end
54 end
55 end
29 end 56 end
30 end 57 end
31 58
32 desc <<-END_DESC 59 desc <<-END_DESC
33 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).
42 files = Dir.glob(File.join(dir,'*.yml')) 69 files = Dir.glob(File.join(dir,'*.yml'))
43 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil 70 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
44 deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil 71 deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
45 # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :) 72 # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
46 delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/ 73 delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
47 74
48 files.each do |path| 75 files.each do |path|
49 # Skip certain locales 76 # Skip certain locales
50 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips 77 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
51 puts "Deleting selected keys from #{path}" 78 puts "Deleting selected keys from #{path}"
52 orig_content = File.open(path, 'r') {|file| file.read} 79 orig_content = File.open(path, 'r') {|file| file.read}
53 File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}} 80 File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
54 end 81 end
55 end 82 end
56 83
57 desc <<-END_DESC 84 desc <<-END_DESC
58 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). 85 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).
59 86
60 Options: 87 Options:
61 key="some_key=foo" 88 key="some_key=foo"