2011-02-23 21:03:33 +03:00
|
|
|
desc 'Updates and checks locales against en.yml'
|
|
|
|
task :locales do
|
|
|
|
%w(locales:update locales:check_interpolation).collect do |task|
|
|
|
|
Rake::Task[task].invoke
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-21 18:11:09 +03:00
|
|
|
namespace :locales do
|
|
|
|
desc 'Updates language files based on en.yml content (only works for new top level keys).'
|
|
|
|
task :update do
|
|
|
|
dir = ENV['DIR'] || './config/locales'
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2009-02-21 18:11:09 +03:00
|
|
|
en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2009-02-21 18:21:31 +03:00
|
|
|
files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
|
2012-07-06 17:06:48 +04:00
|
|
|
files.sort.each do |file|
|
2009-02-21 18:11:09 +03:00
|
|
|
puts "Updating file #{file}"
|
|
|
|
file_strings = YAML.load(File.read(file))
|
|
|
|
file_strings = file_strings[file_strings.keys.first]
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2009-02-21 18:11:09 +03:00
|
|
|
missing_keys = en_strings.keys - file_strings.keys
|
|
|
|
next if missing_keys.empty?
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2009-02-21 18:11:09 +03:00
|
|
|
puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})"
|
|
|
|
lang = File.open(file, 'a')
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2009-02-21 18:11:09 +03:00
|
|
|
missing_keys.each do |key|
|
|
|
|
{key => en_strings[key]}.to_yaml.each_line do |line|
|
|
|
|
next if line =~ /^---/ || line.empty?
|
|
|
|
puts " #{line}"
|
|
|
|
lang << " #{line}"
|
|
|
|
end
|
|
|
|
end
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2009-02-21 18:11:09 +03:00
|
|
|
lang.close
|
|
|
|
end
|
|
|
|
end
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2011-02-23 21:03:33 +03:00
|
|
|
desc 'Checks interpolation arguments in locals against en.yml'
|
|
|
|
task :check_interpolation do
|
|
|
|
dir = ENV['DIR'] || './config/locales'
|
|
|
|
en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
|
|
|
|
files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
|
2012-07-05 16:52:28 +04:00
|
|
|
files.sort.each do |file|
|
|
|
|
puts "parsing #{file}..."
|
2011-02-23 21:03:33 +03:00
|
|
|
file_strings = YAML.load(File.read(file))
|
|
|
|
file_strings = file_strings[file_strings.keys.first]
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2011-02-23 21:03:33 +03:00
|
|
|
file_strings.each do |key, string|
|
|
|
|
next unless string.is_a?(String)
|
|
|
|
string.scan /%\{\w+\}/ do |match|
|
|
|
|
unless en_strings[key].nil? || en_strings[key].include?(match)
|
|
|
|
puts "#{file}: #{key} uses #{match} not found in en.yml"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-10-16 02:41:47 +04:00
|
|
|
|
|
|
|
desc <<-END_DESC
|
|
|
|
Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows).
|
|
|
|
|
2011-08-26 08:58:22 +04:00
|
|
|
This task does not work on Ruby 1.8.6.
|
|
|
|
You need to use Ruby 1.8.7 or later.
|
|
|
|
|
2010-10-16 02:41:47 +04:00
|
|
|
Options:
|
|
|
|
key=key_1,key_2 Comma-separated list of keys to delete
|
|
|
|
skip=en,de Comma-separated list of locale files to ignore (filename without extension)
|
|
|
|
END_DESC
|
|
|
|
|
|
|
|
task :remove_key do
|
|
|
|
dir = ENV['DIR'] || './config/locales'
|
|
|
|
files = Dir.glob(File.join(dir,'*.yml'))
|
|
|
|
skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
|
|
|
|
deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
|
|
|
|
# Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
|
|
|
|
delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2010-10-16 02:41:47 +04:00
|
|
|
files.each do |path|
|
|
|
|
# Skip certain locales
|
|
|
|
(puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
|
|
|
|
puts "Deleting selected keys from #{path}"
|
|
|
|
orig_content = File.open(path, 'r') {|file| file.read}
|
|
|
|
File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
|
|
|
|
end
|
|
|
|
end
|
2011-05-10 15:41:30 +04:00
|
|
|
|
2010-10-16 02:41:47 +04:00
|
|
|
desc <<-END_DESC
|
|
|
|
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).
|
|
|
|
|
|
|
|
Options:
|
|
|
|
key="some_key=foo"
|
|
|
|
key1="another_key=bar"
|
|
|
|
key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
|
|
|
|
skip=en,de Comma-separated list of locale files to ignore (filename without extension)
|
|
|
|
END_DESC
|
|
|
|
|
|
|
|
task :add_key do
|
|
|
|
dir = ENV['DIR'] || './config/locales'
|
|
|
|
files = Dir.glob(File.join(dir,'*.yml'))
|
|
|
|
skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
|
|
|
|
keys_regex = /\Akey(\d+|_.+)?\z/
|
|
|
|
adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
|
|
|
|
key_list = adds.collect {|v| v[0]}.join(", ")
|
|
|
|
|
|
|
|
files.each do |path|
|
|
|
|
# Skip certain locales
|
|
|
|
(puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
|
|
|
|
# TODO: Check for dupliate/existing keys
|
|
|
|
puts "Adding #{key_list} to #{path}"
|
|
|
|
File.open(path, 'a') do |file|
|
|
|
|
adds.each do |kv|
|
|
|
|
Hash[*kv].to_yaml.each_line do |line|
|
|
|
|
file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-27 04:11:08 +04:00
|
|
|
|
2012-07-26 21:32:22 +04:00
|
|
|
desc 'Duplicates a key. Exemple rake locales:dup key=foo new_key=bar'
|
|
|
|
task :dup do
|
|
|
|
dir = ENV['DIR'] || './config/locales'
|
|
|
|
files = Dir.glob(File.join(dir,'*.yml'))
|
|
|
|
skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
|
|
|
|
key = ENV['key']
|
|
|
|
new_key = ENV['new_key']
|
|
|
|
abort "Missing key argument" if key.blank?
|
|
|
|
abort "Missing new_key argument" if new_key.blank?
|
|
|
|
|
|
|
|
files.each do |path|
|
|
|
|
# Skip certain locales
|
|
|
|
(puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
|
|
|
|
puts "Adding #{new_key} to #{path}"
|
|
|
|
|
|
|
|
strings = File.read(path)
|
|
|
|
unless strings =~ /^( #{key}: .+)$/
|
|
|
|
puts "Key not found in #{path}"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
line = $1
|
|
|
|
|
|
|
|
File.open(path, 'a') do |file|
|
|
|
|
file.puts(line.sub(key, new_key))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-27 04:11:08 +04:00
|
|
|
desc 'Check parsing yaml by psych library on Ruby 1.9.'
|
|
|
|
|
|
|
|
# On Fedora 12 and 13, if libyaml-devel is available,
|
|
|
|
# in case of installing by rvm,
|
|
|
|
# Ruby 1.9 default yaml library is psych.
|
|
|
|
|
2011-08-27 04:18:17 +04:00
|
|
|
task :check_parsing_by_psych do
|
2011-08-27 04:11:08 +04:00
|
|
|
begin
|
|
|
|
require 'psych'
|
|
|
|
parser = Psych::Parser.new
|
|
|
|
dir = ENV['DIR'] || './config/locales'
|
|
|
|
files = Dir.glob(File.join(dir,'*.yml'))
|
2012-07-05 16:52:28 +04:00
|
|
|
files.sort.each do |filename|
|
2011-08-27 04:11:08 +04:00
|
|
|
next if File.directory? filename
|
2011-09-07 12:17:58 +04:00
|
|
|
puts "parsing #{filename}..."
|
2011-08-27 04:11:08 +04:00
|
|
|
begin
|
|
|
|
parser.parse File.open(filename)
|
|
|
|
rescue Exception => e1
|
|
|
|
puts(e1.message)
|
|
|
|
puts("")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Exception => e
|
|
|
|
puts(e.message)
|
|
|
|
end
|
|
|
|
end
|
2009-02-21 18:11:09 +03:00
|
|
|
end
|