To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / tasks / locales.rake @ 1298:4f746d8966dd
History | View | Annotate | Download (6 KB)
| 1 | 245:051f544170fe | Chris | desc 'Updates and checks locales against en.yml'
|
|---|---|---|---|
| 2 | task :locales do |
||
| 3 | %w(locales:update locales:check_interpolation).collect do |task| |
||
| 4 | Rake::Task[task].invoke |
||
| 5 | end
|
||
| 6 | end
|
||
| 7 | |||
| 8 | 0:513646585e45 | Chris | namespace :locales do |
| 9 | desc 'Updates language files based on en.yml content (only works for new top level keys).'
|
||
| 10 | task :update do |
||
| 11 | dir = ENV['DIR'] || './config/locales' |
||
| 12 | 441:cbce1fd3b1b7 | Chris | |
| 13 | 0:513646585e45 | Chris | en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] |
| 14 | 441:cbce1fd3b1b7 | Chris | |
| 15 | 0:513646585e45 | Chris | files = Dir.glob(File.join(dir,'*.{yaml,yml}')) |
| 16 | 1115:433d4f72a19b | Chris | files.sort.each do |file|
|
| 17 | 0:513646585e45 | Chris | puts "Updating file #{file}"
|
| 18 | file_strings = YAML.load(File.read(file)) |
||
| 19 | file_strings = file_strings[file_strings.keys.first] |
||
| 20 | 441:cbce1fd3b1b7 | Chris | |
| 21 | 0:513646585e45 | Chris | missing_keys = en_strings.keys - file_strings.keys |
| 22 | next if missing_keys.empty? |
||
| 23 | 441:cbce1fd3b1b7 | Chris | |
| 24 | 0:513646585e45 | Chris | puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})"
|
| 25 | lang = File.open(file, 'a') |
||
| 26 | 441:cbce1fd3b1b7 | Chris | |
| 27 | 0:513646585e45 | Chris | missing_keys.each do |key|
|
| 28 | {key => en_strings[key]}.to_yaml.each_line do |line|
|
||
| 29 | next if line =~ /^---/ || line.empty? |
||
| 30 | puts " #{line}"
|
||
| 31 | lang << " #{line}"
|
||
| 32 | end
|
||
| 33 | end
|
||
| 34 | 441:cbce1fd3b1b7 | Chris | |
| 35 | 0:513646585e45 | Chris | lang.close |
| 36 | end
|
||
| 37 | end
|
||
| 38 | 441:cbce1fd3b1b7 | Chris | |
| 39 | 245:051f544170fe | Chris | desc 'Checks interpolation arguments in locals against en.yml'
|
| 40 | task :check_interpolation do |
||
| 41 | dir = ENV['DIR'] || './config/locales' |
||
| 42 | en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] |
||
| 43 | files = Dir.glob(File.join(dir,'*.{yaml,yml}')) |
||
| 44 | 1115:433d4f72a19b | Chris | files.sort.each do |file|
|
| 45 | puts "parsing #{file}..."
|
||
| 46 | 1295:622f24f53b42 | Chris | file_strings = YAML.load_file(file)
|
| 47 | unless file_strings.is_a?(Hash) |
||
| 48 | puts "#{file}: content is not a Hash (#{file_strings.class.name})"
|
||
| 49 | next
|
||
| 50 | end
|
||
| 51 | unless file_strings.keys.size == 1 |
||
| 52 | puts "#{file}: content has multiple keys (#{file_strings.keys.size})"
|
||
| 53 | next
|
||
| 54 | end
|
||
| 55 | 245:051f544170fe | Chris | file_strings = file_strings[file_strings.keys.first] |
| 56 | 441:cbce1fd3b1b7 | Chris | |
| 57 | 245:051f544170fe | Chris | file_strings.each do |key, string|
|
| 58 | next unless string.is_a?(String) |
||
| 59 | string.scan /%\{\w+\}/ do |match| |
||
| 60 | unless en_strings[key].nil? || en_strings[key].include?(match)
|
||
| 61 | puts "#{file}: #{key} uses #{match} not found in en.yml"
|
||
| 62 | end
|
||
| 63 | end
|
||
| 64 | end
|
||
| 65 | end
|
||
| 66 | end
|
||
| 67 | 37:94944d00e43c | chris | |
| 68 | desc <<-END_DESC |
||
| 69 | Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows).
|
||
| 70 |
|
||
| 71 | 909:cbb26bc654de | Chris | This task does not work on Ruby 1.8.6.
|
| 72 | You need to use Ruby 1.8.7 or later.
|
||
| 73 |
|
||
| 74 | 37:94944d00e43c | chris | Options:
|
| 75 | key=key_1,key_2 Comma-separated list of keys to delete
|
||
| 76 | skip=en,de Comma-separated list of locale files to ignore (filename without extension)
|
||
| 77 | END_DESC
|
||
| 78 | |||
| 79 | task :remove_key do |
||
| 80 | dir = ENV['DIR'] || './config/locales' |
||
| 81 | files = Dir.glob(File.join(dir,'*.yml')) |
||
| 82 | skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil |
||
| 83 | deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil |
||
| 84 | # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
|
||
| 85 | delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
|
||
| 86 | 441:cbce1fd3b1b7 | Chris | |
| 87 | 37:94944d00e43c | chris | files.each do |path|
|
| 88 | # Skip certain locales
|
||
| 89 | (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips |
||
| 90 | puts "Deleting selected keys from #{path}"
|
||
| 91 | orig_content = File.open(path, 'r') {|file| file.read} |
||
| 92 | File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}} |
||
| 93 | end
|
||
| 94 | end
|
||
| 95 | 441:cbce1fd3b1b7 | Chris | |
| 96 | 37:94944d00e43c | chris | desc <<-END_DESC |
| 97 | 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).
|
||
| 98 |
|
||
| 99 | Options:
|
||
| 100 | key="some_key=foo"
|
||
| 101 | key1="another_key=bar"
|
||
| 102 | key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
|
||
| 103 | skip=en,de Comma-separated list of locale files to ignore (filename without extension)
|
||
| 104 | END_DESC
|
||
| 105 | |||
| 106 | task :add_key do |
||
| 107 | dir = ENV['DIR'] || './config/locales' |
||
| 108 | files = Dir.glob(File.join(dir,'*.yml')) |
||
| 109 | skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil |
||
| 110 | keys_regex = /\Akey(\d+|_.+)?\z/
|
||
| 111 | adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)} |
||
| 112 | key_list = adds.collect {|v| v[0]}.join(", ")
|
||
| 113 | |||
| 114 | files.each do |path|
|
||
| 115 | # Skip certain locales
|
||
| 116 | (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips |
||
| 117 | # TODO: Check for dupliate/existing keys
|
||
| 118 | puts "Adding #{key_list} to #{path}"
|
||
| 119 | File.open(path, 'a') do |file| |
||
| 120 | adds.each do |kv|
|
||
| 121 | Hash[*kv].to_yaml.each_line do |line| |
||
| 122 | file.puts " #{line}" unless (line =~ /^---/ || line.empty?) |
||
| 123 | end
|
||
| 124 | end
|
||
| 125 | end
|
||
| 126 | end
|
||
| 127 | end
|
||
| 128 | 909:cbb26bc654de | Chris | |
| 129 | 1115:433d4f72a19b | Chris | desc 'Duplicates a key. Exemple rake locales:dup key=foo new_key=bar'
|
| 130 | task :dup do |
||
| 131 | dir = ENV['DIR'] || './config/locales' |
||
| 132 | files = Dir.glob(File.join(dir,'*.yml')) |
||
| 133 | skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil |
||
| 134 | key = ENV['key'] |
||
| 135 | new_key = ENV['new_key'] |
||
| 136 | abort "Missing key argument" if key.blank? |
||
| 137 | abort "Missing new_key argument" if new_key.blank? |
||
| 138 | |||
| 139 | files.each do |path|
|
||
| 140 | # Skip certain locales
|
||
| 141 | (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips |
||
| 142 | puts "Adding #{new_key} to #{path}"
|
||
| 143 | |||
| 144 | strings = File.read(path)
|
||
| 145 | unless strings =~ /^( #{key}: .+)$/ |
||
| 146 | puts "Key not found in #{path}"
|
||
| 147 | next
|
||
| 148 | end
|
||
| 149 | line = $1
|
||
| 150 | |||
| 151 | File.open(path, 'a') do |file| |
||
| 152 | file.puts(line.sub(key, new_key)) |
||
| 153 | end
|
||
| 154 | end
|
||
| 155 | end
|
||
| 156 | |||
| 157 | 909:cbb26bc654de | Chris | desc 'Check parsing yaml by psych library on Ruby 1.9.'
|
| 158 | |||
| 159 | # On Fedora 12 and 13, if libyaml-devel is available,
|
||
| 160 | # in case of installing by rvm,
|
||
| 161 | # Ruby 1.9 default yaml library is psych.
|
||
| 162 | |||
| 163 | task :check_parsing_by_psych do |
||
| 164 | begin
|
||
| 165 | require 'psych'
|
||
| 166 | parser = Psych::Parser.new |
||
| 167 | dir = ENV['DIR'] || './config/locales' |
||
| 168 | files = Dir.glob(File.join(dir,'*.yml')) |
||
| 169 | 1115:433d4f72a19b | Chris | files.sort.each do |filename|
|
| 170 | 909:cbb26bc654de | Chris | next if File.directory? filename |
| 171 | puts "parsing #{filename}..."
|
||
| 172 | begin
|
||
| 173 | parser.parse File.open(filename)
|
||
| 174 | rescue Exception => e1 |
||
| 175 | puts(e1.message) |
||
| 176 | puts("")
|
||
| 177 | end
|
||
| 178 | end
|
||
| 179 | rescue Exception => e |
||
| 180 | puts(e.message) |
||
| 181 | end
|
||
| 182 | end
|
||
| 183 | 0:513646585e45 | Chris | end |