annotate lib/tasks/.svn/text-base/locales.rake.svn-base @ 855:7294e8db2515 bug_162

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