annotate lib/tasks/.svn/text-base/locales.rake.svn-base @ 904:0a8317a50fa0 redmine-1.1

Close obsolete branch redmine-1.1
author Chris Cannam
date Fri, 14 Jan 2011 12:53:21 +0000
parents 94944d00e43c
children 051f544170fe
rev   line source
Chris@0 1 namespace :locales do
Chris@0 2 desc 'Updates language files based on en.yml content (only works for new top level keys).'
Chris@0 3 task :update do
Chris@0 4 dir = ENV['DIR'] || './config/locales'
Chris@0 5
Chris@0 6 en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
Chris@0 7
Chris@0 8 files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
Chris@0 9 files.each do |file|
Chris@0 10 puts "Updating file #{file}"
Chris@0 11 file_strings = YAML.load(File.read(file))
Chris@0 12 file_strings = file_strings[file_strings.keys.first]
Chris@0 13
Chris@0 14 missing_keys = en_strings.keys - file_strings.keys
Chris@0 15 next if missing_keys.empty?
Chris@0 16
Chris@0 17 puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})"
Chris@0 18 lang = File.open(file, 'a')
Chris@0 19
Chris@0 20 missing_keys.each do |key|
Chris@0 21 {key => en_strings[key]}.to_yaml.each_line do |line|
Chris@0 22 next if line =~ /^---/ || line.empty?
Chris@0 23 puts " #{line}"
Chris@0 24 lang << " #{line}"
Chris@0 25 end
Chris@0 26 end
Chris@0 27
Chris@0 28 lang.close
Chris@0 29 end
Chris@0 30 end
chris@37 31
chris@37 32 desc <<-END_DESC
chris@37 33 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 34
chris@37 35 Options:
chris@37 36 key=key_1,key_2 Comma-separated list of keys to delete
chris@37 37 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
chris@37 38 END_DESC
chris@37 39
chris@37 40 task :remove_key do
chris@37 41 dir = ENV['DIR'] || './config/locales'
chris@37 42 files = Dir.glob(File.join(dir,'*.yml'))
chris@37 43 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
chris@37 44 deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
chris@37 45 # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
chris@37 46 delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
chris@37 47
chris@37 48 files.each do |path|
chris@37 49 # Skip certain locales
chris@37 50 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
chris@37 51 puts "Deleting selected keys from #{path}"
chris@37 52 orig_content = File.open(path, 'r') {|file| file.read}
chris@37 53 File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
chris@37 54 end
chris@37 55 end
chris@37 56
chris@37 57 desc <<-END_DESC
chris@37 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).
chris@37 59
chris@37 60 Options:
chris@37 61 key="some_key=foo"
chris@37 62 key1="another_key=bar"
chris@37 63 key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
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 :add_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 keys_regex = /\Akey(\d+|_.+)?\z/
chris@37 72 adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
chris@37 73 key_list = adds.collect {|v| v[0]}.join(", ")
chris@37 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 # TODO: Check for dupliate/existing keys
chris@37 79 puts "Adding #{key_list} to #{path}"
chris@37 80 File.open(path, 'a') do |file|
chris@37 81 adds.each do |kv|
chris@37 82 Hash[*kv].to_yaml.each_line do |line|
chris@37 83 file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
chris@37 84 end
chris@37 85 end
chris@37 86 end
chris@37 87 end
chris@37 88 end
Chris@0 89 end