annotate lib/tasks/locales.rake @ 1478:5ca1f4a47171 bibplugin_db_migrations

Close obsolete branch bibplugin_db_migrations
author Chris Cannam
date Fri, 30 Nov 2012 14:40:50 +0000
parents cbb26bc654de
children 433d4f72a19b
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@909 62 This task does not work on Ruby 1.8.6.
Chris@909 63 You need to use Ruby 1.8.7 or later.
Chris@909 64
chris@37 65 Options:
chris@37 66 key=key_1,key_2 Comma-separated list of keys to delete
chris@37 67 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
chris@37 68 END_DESC
chris@37 69
chris@37 70 task :remove_key do
chris@37 71 dir = ENV['DIR'] || './config/locales'
chris@37 72 files = Dir.glob(File.join(dir,'*.yml'))
chris@37 73 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
chris@37 74 deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
chris@37 75 # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
chris@37 76 delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
Chris@441 77
chris@37 78 files.each do |path|
chris@37 79 # Skip certain locales
chris@37 80 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
chris@37 81 puts "Deleting selected keys from #{path}"
chris@37 82 orig_content = File.open(path, 'r') {|file| file.read}
chris@37 83 File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
chris@37 84 end
chris@37 85 end
Chris@441 86
chris@37 87 desc <<-END_DESC
chris@37 88 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 89
chris@37 90 Options:
chris@37 91 key="some_key=foo"
chris@37 92 key1="another_key=bar"
chris@37 93 key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
chris@37 94 skip=en,de Comma-separated list of locale files to ignore (filename without extension)
chris@37 95 END_DESC
chris@37 96
chris@37 97 task :add_key do
chris@37 98 dir = ENV['DIR'] || './config/locales'
chris@37 99 files = Dir.glob(File.join(dir,'*.yml'))
chris@37 100 skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
chris@37 101 keys_regex = /\Akey(\d+|_.+)?\z/
chris@37 102 adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
chris@37 103 key_list = adds.collect {|v| v[0]}.join(", ")
chris@37 104
chris@37 105 files.each do |path|
chris@37 106 # Skip certain locales
chris@37 107 (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
chris@37 108 # TODO: Check for dupliate/existing keys
chris@37 109 puts "Adding #{key_list} to #{path}"
chris@37 110 File.open(path, 'a') do |file|
chris@37 111 adds.each do |kv|
chris@37 112 Hash[*kv].to_yaml.each_line do |line|
chris@37 113 file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
chris@37 114 end
chris@37 115 end
chris@37 116 end
chris@37 117 end
chris@37 118 end
Chris@909 119
Chris@909 120 desc 'Check parsing yaml by psych library on Ruby 1.9.'
Chris@909 121
Chris@909 122 # On Fedora 12 and 13, if libyaml-devel is available,
Chris@909 123 # in case of installing by rvm,
Chris@909 124 # Ruby 1.9 default yaml library is psych.
Chris@909 125
Chris@909 126 task :check_parsing_by_psych do
Chris@909 127 begin
Chris@909 128 require 'psych'
Chris@909 129 parser = Psych::Parser.new
Chris@909 130 dir = ENV['DIR'] || './config/locales'
Chris@909 131 files = Dir.glob(File.join(dir,'*.yml'))
Chris@909 132 files.each do |filename|
Chris@909 133 next if File.directory? filename
Chris@909 134 puts "parsing #{filename}..."
Chris@909 135 begin
Chris@909 136 parser.parse File.open(filename)
Chris@909 137 rescue Exception => e1
Chris@909 138 puts(e1.message)
Chris@909 139 puts("")
Chris@909 140 end
Chris@909 141 end
Chris@909 142 rescue Exception => e
Chris@909 143 puts(e.message)
Chris@909 144 end
Chris@909 145 end
Chris@0 146 end