@@ -1,146 +1,147 | |||
|
1 | 1 | desc 'Updates and checks locales against en.yml' |
|
2 | 2 | task :locales do |
|
3 | 3 | %w(locales:update locales:check_interpolation).collect do |task| |
|
4 | 4 | Rake::Task[task].invoke |
|
5 | 5 | end |
|
6 | 6 | end |
|
7 | 7 | |
|
8 | 8 | namespace :locales do |
|
9 | 9 | desc 'Updates language files based on en.yml content (only works for new top level keys).' |
|
10 | 10 | task :update do |
|
11 | 11 | dir = ENV['DIR'] || './config/locales' |
|
12 | 12 | |
|
13 | 13 | en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] |
|
14 | 14 | |
|
15 | 15 | files = Dir.glob(File.join(dir,'*.{yaml,yml}')) |
|
16 | 16 | files.each do |file| |
|
17 | 17 | puts "Updating file #{file}" |
|
18 | 18 | file_strings = YAML.load(File.read(file)) |
|
19 | 19 | file_strings = file_strings[file_strings.keys.first] |
|
20 | 20 | |
|
21 | 21 | missing_keys = en_strings.keys - file_strings.keys |
|
22 | 22 | next if missing_keys.empty? |
|
23 | 23 | |
|
24 | 24 | puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})" |
|
25 | 25 | lang = File.open(file, 'a') |
|
26 | 26 | |
|
27 | 27 | missing_keys.each do |key| |
|
28 | 28 | {key => en_strings[key]}.to_yaml.each_line do |line| |
|
29 | 29 | next if line =~ /^---/ || line.empty? |
|
30 | 30 | puts " #{line}" |
|
31 | 31 | lang << " #{line}" |
|
32 | 32 | end |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | lang.close |
|
36 | 36 | end |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | desc 'Checks interpolation arguments in locals against en.yml' |
|
40 | 40 | task :check_interpolation do |
|
41 | 41 | dir = ENV['DIR'] || './config/locales' |
|
42 | 42 | en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] |
|
43 | 43 | files = Dir.glob(File.join(dir,'*.{yaml,yml}')) |
|
44 | files.each do |file| | |
|
44 | files.sort.each do |file| | |
|
45 | puts "parsing #{file}..." | |
|
45 | 46 | file_strings = YAML.load(File.read(file)) |
|
46 | 47 | file_strings = file_strings[file_strings.keys.first] |
|
47 | 48 | |
|
48 | 49 | file_strings.each do |key, string| |
|
49 | 50 | next unless string.is_a?(String) |
|
50 | 51 | string.scan /%\{\w+\}/ do |match| |
|
51 | 52 | unless en_strings[key].nil? || en_strings[key].include?(match) |
|
52 | 53 | puts "#{file}: #{key} uses #{match} not found in en.yml" |
|
53 | 54 | end |
|
54 | 55 | end |
|
55 | 56 | end |
|
56 | 57 | end |
|
57 | 58 | end |
|
58 | 59 | |
|
59 | 60 | desc <<-END_DESC |
|
60 | 61 | Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows). |
|
61 | 62 | |
|
62 | 63 | This task does not work on Ruby 1.8.6. |
|
63 | 64 | You need to use Ruby 1.8.7 or later. |
|
64 | 65 | |
|
65 | 66 | Options: |
|
66 | 67 | key=key_1,key_2 Comma-separated list of keys to delete |
|
67 | 68 | skip=en,de Comma-separated list of locale files to ignore (filename without extension) |
|
68 | 69 | END_DESC |
|
69 | 70 | |
|
70 | 71 | task :remove_key do |
|
71 | 72 | dir = ENV['DIR'] || './config/locales' |
|
72 | 73 | files = Dir.glob(File.join(dir,'*.yml')) |
|
73 | 74 | skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil |
|
74 | 75 | deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil |
|
75 | 76 | # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :) |
|
76 | 77 | delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/ |
|
77 | 78 | |
|
78 | 79 | files.each do |path| |
|
79 | 80 | # Skip certain locales |
|
80 | 81 | (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips |
|
81 | 82 | puts "Deleting selected keys from #{path}" |
|
82 | 83 | orig_content = File.open(path, 'r') {|file| file.read} |
|
83 | 84 | File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}} |
|
84 | 85 | end |
|
85 | 86 | end |
|
86 | 87 | |
|
87 | 88 | desc <<-END_DESC |
|
88 | 89 | 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). |
|
89 | 90 | |
|
90 | 91 | Options: |
|
91 | 92 | key="some_key=foo" |
|
92 | 93 | key1="another_key=bar" |
|
93 | 94 | key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised |
|
94 | 95 | skip=en,de Comma-separated list of locale files to ignore (filename without extension) |
|
95 | 96 | END_DESC |
|
96 | 97 | |
|
97 | 98 | task :add_key do |
|
98 | 99 | dir = ENV['DIR'] || './config/locales' |
|
99 | 100 | files = Dir.glob(File.join(dir,'*.yml')) |
|
100 | 101 | skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil |
|
101 | 102 | keys_regex = /\Akey(\d+|_.+)?\z/ |
|
102 | 103 | adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)} |
|
103 | 104 | key_list = adds.collect {|v| v[0]}.join(", ") |
|
104 | 105 | |
|
105 | 106 | files.each do |path| |
|
106 | 107 | # Skip certain locales |
|
107 | 108 | (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips |
|
108 | 109 | # TODO: Check for dupliate/existing keys |
|
109 | 110 | puts "Adding #{key_list} to #{path}" |
|
110 | 111 | File.open(path, 'a') do |file| |
|
111 | 112 | adds.each do |kv| |
|
112 | 113 | Hash[*kv].to_yaml.each_line do |line| |
|
113 | 114 | file.puts " #{line}" unless (line =~ /^---/ || line.empty?) |
|
114 | 115 | end |
|
115 | 116 | end |
|
116 | 117 | end |
|
117 | 118 | end |
|
118 | 119 | end |
|
119 | 120 | |
|
120 | 121 | desc 'Check parsing yaml by psych library on Ruby 1.9.' |
|
121 | 122 | |
|
122 | 123 | # On Fedora 12 and 13, if libyaml-devel is available, |
|
123 | 124 | # in case of installing by rvm, |
|
124 | 125 | # Ruby 1.9 default yaml library is psych. |
|
125 | 126 | |
|
126 | 127 | task :check_parsing_by_psych do |
|
127 | 128 | begin |
|
128 | 129 | require 'psych' |
|
129 | 130 | parser = Psych::Parser.new |
|
130 | 131 | dir = ENV['DIR'] || './config/locales' |
|
131 | 132 | files = Dir.glob(File.join(dir,'*.yml')) |
|
132 | files.each do |filename| | |
|
133 | files.sort.each do |filename| | |
|
133 | 134 | next if File.directory? filename |
|
134 | 135 | puts "parsing #{filename}..." |
|
135 | 136 | begin |
|
136 | 137 | parser.parse File.open(filename) |
|
137 | 138 | rescue Exception => e1 |
|
138 | 139 | puts(e1.message) |
|
139 | 140 | puts("") |
|
140 | 141 | end |
|
141 | 142 | end |
|
142 | 143 | rescue Exception => e |
|
143 | 144 | puts(e.message) |
|
144 | 145 | end |
|
145 | 146 | end |
|
146 | 147 | end |
General Comments 0
You need to be logged in to leave comments.
Login now