Chris@909: desc 'Updates and checks locales against en.yml' Chris@909: task :locales do Chris@909: %w(locales:update locales:check_interpolation).collect do |task| Chris@909: Rake::Task[task].invoke Chris@909: end Chris@909: end Chris@909: Chris@909: namespace :locales do Chris@909: desc 'Updates language files based on en.yml content (only works for new top level keys).' Chris@909: task :update do Chris@909: dir = ENV['DIR'] || './config/locales' Chris@909: Chris@909: en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] Chris@909: Chris@909: files = Dir.glob(File.join(dir,'*.{yaml,yml}')) Chris@909: files.each do |file| Chris@909: puts "Updating file #{file}" Chris@909: file_strings = YAML.load(File.read(file)) Chris@909: file_strings = file_strings[file_strings.keys.first] Chris@909: Chris@909: missing_keys = en_strings.keys - file_strings.keys Chris@909: next if missing_keys.empty? Chris@909: Chris@909: puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})" Chris@909: lang = File.open(file, 'a') Chris@909: Chris@909: missing_keys.each do |key| Chris@909: {key => en_strings[key]}.to_yaml.each_line do |line| Chris@909: next if line =~ /^---/ || line.empty? Chris@909: puts " #{line}" Chris@909: lang << " #{line}" Chris@909: end Chris@909: end Chris@909: Chris@909: lang.close Chris@909: end Chris@909: end Chris@909: Chris@909: desc 'Checks interpolation arguments in locals against en.yml' Chris@909: task :check_interpolation do Chris@909: dir = ENV['DIR'] || './config/locales' Chris@909: en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] Chris@909: files = Dir.glob(File.join(dir,'*.{yaml,yml}')) Chris@909: files.each do |file| Chris@909: file_strings = YAML.load(File.read(file)) Chris@909: file_strings = file_strings[file_strings.keys.first] Chris@909: Chris@909: file_strings.each do |key, string| Chris@909: next unless string.is_a?(String) Chris@909: string.scan /%\{\w+\}/ do |match| Chris@909: unless en_strings[key].nil? || en_strings[key].include?(match) Chris@909: puts "#{file}: #{key} uses #{match} not found in en.yml" Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: desc <<-END_DESC Chris@909: Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows). Chris@909: 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@909: Options: Chris@909: key=key_1,key_2 Comma-separated list of keys to delete Chris@909: skip=en,de Comma-separated list of locale files to ignore (filename without extension) Chris@909: END_DESC Chris@909: Chris@909: task :remove_key do Chris@909: dir = ENV['DIR'] || './config/locales' Chris@909: files = Dir.glob(File.join(dir,'*.yml')) Chris@909: skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil Chris@909: deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil Chris@909: # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :) Chris@909: delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/ Chris@909: Chris@909: files.each do |path| Chris@909: # Skip certain locales Chris@909: (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips Chris@909: puts "Deleting selected keys from #{path}" Chris@909: orig_content = File.open(path, 'r') {|file| file.read} Chris@909: File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}} Chris@909: end Chris@909: end Chris@909: Chris@909: desc <<-END_DESC Chris@909: 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@909: Chris@909: Options: Chris@909: key="some_key=foo" Chris@909: key1="another_key=bar" Chris@909: key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised Chris@909: skip=en,de Comma-separated list of locale files to ignore (filename without extension) Chris@909: END_DESC Chris@909: Chris@909: task :add_key do Chris@909: dir = ENV['DIR'] || './config/locales' Chris@909: files = Dir.glob(File.join(dir,'*.yml')) Chris@909: skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil Chris@909: keys_regex = /\Akey(\d+|_.+)?\z/ Chris@909: adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)} Chris@909: key_list = adds.collect {|v| v[0]}.join(", ") Chris@909: Chris@909: files.each do |path| Chris@909: # Skip certain locales Chris@909: (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips Chris@909: # TODO: Check for dupliate/existing keys Chris@909: puts "Adding #{key_list} to #{path}" Chris@909: File.open(path, 'a') do |file| Chris@909: adds.each do |kv| Chris@909: Hash[*kv].to_yaml.each_line do |line| Chris@909: file.puts " #{line}" unless (line =~ /^---/ || line.empty?) Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: 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@909: end