@@ -28,4 +28,62 namespace :locales do | |||
|
28 | 28 | lang.close |
|
29 | 29 | end |
|
30 | 30 | end |
|
31 | ||
|
32 | desc <<-END_DESC | |
|
33 | Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows). | |
|
34 | ||
|
35 | Options: | |
|
36 | key=key_1,key_2 Comma-separated list of keys to delete | |
|
37 | skip=en,de Comma-separated list of locale files to ignore (filename without extension) | |
|
38 | END_DESC | |
|
39 | ||
|
40 | task :remove_key do | |
|
41 | dir = ENV['DIR'] || './config/locales' | |
|
42 | files = Dir.glob(File.join(dir,'*.yml')) | |
|
43 | skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil | |
|
44 | deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil | |
|
45 | # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :) | |
|
46 | delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/ | |
|
47 | ||
|
48 | files.each do |path| | |
|
49 | # Skip certain locales | |
|
50 | (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips | |
|
51 | puts "Deleting selected keys from #{path}" | |
|
52 | orig_content = File.open(path, 'r') {|file| file.read} | |
|
53 | File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}} | |
|
54 | end | |
|
55 | end | |
|
56 | ||
|
57 | desc <<-END_DESC | |
|
58 | 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). | |
|
59 | ||
|
60 | Options: | |
|
61 | key="some_key=foo" | |
|
62 | key1="another_key=bar" | |
|
63 | key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised | |
|
64 | skip=en,de Comma-separated list of locale files to ignore (filename without extension) | |
|
65 | END_DESC | |
|
66 | ||
|
67 | task :add_key do | |
|
68 | dir = ENV['DIR'] || './config/locales' | |
|
69 | files = Dir.glob(File.join(dir,'*.yml')) | |
|
70 | skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil | |
|
71 | keys_regex = /\Akey(\d+|_.+)?\z/ | |
|
72 | adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)} | |
|
73 | key_list = adds.collect {|v| v[0]}.join(", ") | |
|
74 | ||
|
75 | files.each do |path| | |
|
76 | # Skip certain locales | |
|
77 | (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips | |
|
78 | # TODO: Check for dupliate/existing keys | |
|
79 | puts "Adding #{key_list} to #{path}" | |
|
80 | File.open(path, 'a') do |file| | |
|
81 | adds.each do |kv| | |
|
82 | Hash[*kv].to_yaml.each_line do |line| | |
|
83 | file.puts " #{line}" unless (line =~ /^---/ || line.empty?) | |
|
84 | end | |
|
85 | end | |
|
86 | end | |
|
87 | end | |
|
88 | end | |
|
31 | 89 | end |
General Comments 0
You need to be logged in to leave comments.
Login now