annotate lib/tasks/locales.rake @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 433d4f72a19b
children 622f24f53b42
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@1115 16 files.sort.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@1115 44 files.sort.each do |file|
Chris@1115 45 puts "parsing #{file}..."
Chris@245 46 file_strings = YAML.load(File.read(file))
Chris@245 47 file_strings = file_strings[file_strings.keys.first]
Chris@441 48
Chris@245 49 file_strings.each do |key, string|
Chris@245 50 next unless string.is_a?(String)
Chris@245 51 string.scan /%\{\w+\}/ do |match|
Chris@245 52 unless en_strings[key].nil? || en_strings[key].include?(match)
Chris@245 53 puts "#{file}: #{key} uses #{match} not found in en.yml"
Chris@245 54 end
Chris@245 55 end
Chris@245 56 end
Chris@245 57 end
Chris@245 58 end
chris@37 59
chris@37 60 desc <<-END_DESC
chris@37 61 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 62
Chris@909 63 This task does not work on Ruby 1.8.6.
Chris@909 64 You need to use Ruby 1.8.7 or later.
Chris@909 65
chris@37 66 Options:
chris@37 67 key=key_1,key_2 Comma-separated list of keys to delete
chris@37 68 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
chris@37 69 END_DESC
chris@37 70
chris@37 71 task :remove_key do
chris@37 72 dir = ENV['DIR'] || './config/locales'
chris@37 73 files = Dir.glob(File.join(dir,'*.yml'))
chris@37 74 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
chris@37 75 deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
chris@37 76 # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
chris@37 77 delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
Chris@441 78
chris@37 79 files.each do |path|
chris@37 80 # Skip certain locales
chris@37 81 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
chris@37 82 puts "Deleting selected keys from #{path}"
chris@37 83 orig_content = File.open(path, 'r') {|file| file.read}
chris@37 84 File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
chris@37 85 end
chris@37 86 end
Chris@441 87
chris@37 88 desc <<-END_DESC
chris@37 89 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 90
chris@37 91 Options:
chris@37 92 key="some_key=foo"
chris@37 93 key1="another_key=bar"
chris@37 94 key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
chris@37 95 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
chris@37 96 END_DESC
chris@37 97
chris@37 98 task :add_key do
chris@37 99 dir = ENV['DIR'] || './config/locales'
chris@37 100 files = Dir.glob(File.join(dir,'*.yml'))
chris@37 101 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
chris@37 102 keys_regex = /\Akey(\d+|_.+)?\z/
chris@37 103 adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
chris@37 104 key_list = adds.collect {|v| v[0]}.join(", ")
chris@37 105
chris@37 106 files.each do |path|
chris@37 107 # Skip certain locales
chris@37 108 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
chris@37 109 # TODO: Check for dupliate/existing keys
chris@37 110 puts "Adding #{key_list} to #{path}"
chris@37 111 File.open(path, 'a') do |file|
chris@37 112 adds.each do |kv|
chris@37 113 Hash[*kv].to_yaml.each_line do |line|
chris@37 114 file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
chris@37 115 end
chris@37 116 end
chris@37 117 end
chris@37 118 end
chris@37 119 end
Chris@909 120
Chris@1115 121 desc 'Duplicates a key. Exemple rake locales:dup key=foo new_key=bar'
Chris@1115 122 task :dup do
Chris@1115 123 dir = ENV['DIR'] || './config/locales'
Chris@1115 124 files = Dir.glob(File.join(dir,'*.yml'))
Chris@1115 125 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
Chris@1115 126 key = ENV['key']
Chris@1115 127 new_key = ENV['new_key']
Chris@1115 128 abort "Missing key argument" if key.blank?
Chris@1115 129 abort "Missing new_key argument" if new_key.blank?
Chris@1115 130
Chris@1115 131 files.each do |path|
Chris@1115 132 # Skip certain locales
Chris@1115 133 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
Chris@1115 134 puts "Adding #{new_key} to #{path}"
Chris@1115 135
Chris@1115 136 strings = File.read(path)
Chris@1115 137 unless strings =~ /^( #{key}: .+)$/
Chris@1115 138 puts "Key not found in #{path}"
Chris@1115 139 next
Chris@1115 140 end
Chris@1115 141 line = $1
Chris@1115 142
Chris@1115 143 File.open(path, 'a') do |file|
Chris@1115 144 file.puts(line.sub(key, new_key))
Chris@1115 145 end
Chris@1115 146 end
Chris@1115 147 end
Chris@1115 148
Chris@909 149 desc 'Check parsing yaml by psych library on Ruby 1.9.'
Chris@909 150
Chris@909 151 # On Fedora 12 and 13, if libyaml-devel is available,
Chris@909 152 # in case of installing by rvm,
Chris@909 153 # Ruby 1.9 default yaml library is psych.
Chris@909 154
Chris@909 155 task :check_parsing_by_psych do
Chris@909 156 begin
Chris@909 157 require 'psych'
Chris@909 158 parser = Psych::Parser.new
Chris@909 159 dir = ENV['DIR'] || './config/locales'
Chris@909 160 files = Dir.glob(File.join(dir,'*.yml'))
Chris@1115 161 files.sort.each do |filename|
Chris@909 162 next if File.directory? filename
Chris@909 163 puts "parsing #{filename}..."
Chris@909 164 begin
Chris@909 165 parser.parse File.open(filename)
Chris@909 166 rescue Exception => e1
Chris@909 167 puts(e1.message)
Chris@909 168 puts("")
Chris@909 169 end
Chris@909 170 end
Chris@909 171 rescue Exception => e
Chris@909 172 puts(e.message)
Chris@909 173 end
Chris@909 174 end
Chris@0 175 end