diff options
author | Eric Davis <edavis@littlestreamsoftware.com> | 2010-10-15 22:41:47 +0000 |
---|---|---|
committer | Eric Davis <edavis@littlestreamsoftware.com> | 2010-10-15 22:41:47 +0000 |
commit | 1bfbc012f591e4ecd6ce7e15a989c19b6fba316b (patch) | |
tree | 62589306472b22fd4a748126409ae2e6a79cc482 | |
parent | 02711057943805a2c605a87f069a9fc333c6d1fb (diff) | |
download | redmine-1bfbc012f591e4ecd6ce7e15a989c19b6fba316b.tar.gz redmine-1bfbc012f591e4ecd6ce7e15a989c19b6fba316b.zip |
Add rake tasks to add and remove keys in the locales. #6548
Contributed by Felix Schäfer
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4254 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r-- | lib/tasks/locales.rake | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/tasks/locales.rake b/lib/tasks/locales.rake index 9bf8e26d1..512538475 100644 --- a/lib/tasks/locales.rake +++ b/lib/tasks/locales.rake @@ -28,4 +28,62 @@ namespace :locales do lang.close end end + + 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). + +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/ + + 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 + + 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 end |