Chris@245: desc 'Updates and checks locales against en.yml' Chris@245: task :locales do Chris@245: %w(locales:update locales:check_interpolation).collect do |task| Chris@245: Rake::Task[task].invoke Chris@245: end Chris@245: end Chris@245: Chris@0: namespace :locales do Chris@0: desc 'Updates language files based on en.yml content (only works for new top level keys).' Chris@0: task :update do Chris@0: dir = ENV['DIR'] || './config/locales' Chris@441: Chris@0: en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] Chris@441: Chris@0: files = Dir.glob(File.join(dir,'*.{yaml,yml}')) Chris@0: files.each do |file| Chris@0: puts "Updating file #{file}" Chris@0: file_strings = YAML.load(File.read(file)) Chris@0: file_strings = file_strings[file_strings.keys.first] Chris@441: Chris@0: missing_keys = en_strings.keys - file_strings.keys Chris@0: next if missing_keys.empty? Chris@441: Chris@0: puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})" Chris@0: lang = File.open(file, 'a') Chris@441: Chris@0: missing_keys.each do |key| Chris@0: {key => en_strings[key]}.to_yaml.each_line do |line| Chris@0: next if line =~ /^---/ || line.empty? Chris@0: puts " #{line}" Chris@0: lang << " #{line}" Chris@0: end Chris@0: end Chris@441: Chris@0: lang.close Chris@0: end Chris@0: end Chris@441: Chris@245: desc 'Checks interpolation arguments in locals against en.yml' Chris@245: task :check_interpolation do Chris@245: dir = ENV['DIR'] || './config/locales' Chris@245: en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] Chris@245: files = Dir.glob(File.join(dir,'*.{yaml,yml}')) Chris@245: files.each do |file| Chris@245: file_strings = YAML.load(File.read(file)) Chris@245: file_strings = file_strings[file_strings.keys.first] Chris@441: Chris@245: file_strings.each do |key, string| Chris@245: next unless string.is_a?(String) Chris@245: string.scan /%\{\w+\}/ do |match| Chris@245: unless en_strings[key].nil? || en_strings[key].include?(match) Chris@245: puts "#{file}: #{key} uses #{match} not found in en.yml" Chris@245: end Chris@245: end Chris@245: end Chris@245: end Chris@245: end chris@37: chris@37: desc <<-END_DESC chris@37: 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: Chris@909: This task does not work on Ruby 1.8.6. Chris@909: You need to use Ruby 1.8.7 or later. Chris@909: chris@37: Options: chris@37: key=key_1,key_2 Comma-separated list of keys to delete chris@37: skip=en,de Comma-separated list of locale files to ignore (filename without extension) chris@37: END_DESC chris@37: chris@37: task :remove_key do chris@37: dir = ENV['DIR'] || './config/locales' chris@37: files = Dir.glob(File.join(dir,'*.yml')) chris@37: skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil chris@37: deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil chris@37: # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :) chris@37: delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/ Chris@441: chris@37: files.each do |path| chris@37: # Skip certain locales chris@37: (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips chris@37: puts "Deleting selected keys from #{path}" chris@37: orig_content = File.open(path, 'r') {|file| file.read} chris@37: File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}} chris@37: end chris@37: end Chris@441: chris@37: desc <<-END_DESC chris@37: 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: chris@37: Options: chris@37: key="some_key=foo" chris@37: key1="another_key=bar" chris@37: key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised chris@37: skip=en,de Comma-separated list of locale files to ignore (filename without extension) chris@37: END_DESC chris@37: chris@37: task :add_key do chris@37: dir = ENV['DIR'] || './config/locales' chris@37: files = Dir.glob(File.join(dir,'*.yml')) chris@37: skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil chris@37: keys_regex = /\Akey(\d+|_.+)?\z/ chris@37: adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)} chris@37: key_list = adds.collect {|v| v[0]}.join(", ") chris@37: chris@37: files.each do |path| chris@37: # Skip certain locales chris@37: (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips chris@37: # TODO: Check for dupliate/existing keys chris@37: puts "Adding #{key_list} to #{path}" chris@37: File.open(path, 'a') do |file| chris@37: adds.each do |kv| chris@37: Hash[*kv].to_yaml.each_line do |line| chris@37: file.puts " #{line}" unless (line =~ /^---/ || line.empty?) chris@37: end chris@37: end chris@37: end chris@37: end chris@37: end Chris@909: Chris@909: desc 'Check parsing yaml by psych library on Ruby 1.9.' Chris@909: Chris@909: # On Fedora 12 and 13, if libyaml-devel is available, Chris@909: # in case of installing by rvm, Chris@909: # Ruby 1.9 default yaml library is psych. Chris@909: Chris@909: task :check_parsing_by_psych do Chris@909: begin Chris@909: require 'psych' Chris@909: parser = Psych::Parser.new Chris@909: dir = ENV['DIR'] || './config/locales' Chris@909: files = Dir.glob(File.join(dir,'*.yml')) Chris@909: files.each do |filename| Chris@909: next if File.directory? filename Chris@909: puts "parsing #{filename}..." Chris@909: begin Chris@909: parser.parse File.open(filename) Chris@909: rescue Exception => e1 Chris@909: puts(e1.message) Chris@909: puts("") Chris@909: end Chris@909: end Chris@909: rescue Exception => e Chris@909: puts(e.message) Chris@909: end Chris@909: end Chris@0: end