annotate lib/tasks/locales.rake @ 1628:9c5f8e24dadc live tip

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