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@0: Chris@0: en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] Chris@0: 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@0: Chris@0: missing_keys = en_strings.keys - file_strings.keys Chris@0: next if missing_keys.empty? Chris@0: Chris@0: puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})" Chris@0: lang = File.open(file, 'a') Chris@0: 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@0: Chris@0: lang.close Chris@0: end Chris@0: 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@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@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: 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@37: 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@0: end