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 / .svn / text-base / locales.rake.svn-base @ 442:753f1380d6bc
History | View | Annotate | Download (4.11 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 | files.each do |file| |
||
| 17 | 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 | files.each do |file| |
||
| 45 | file_strings = YAML.load(File.read(file)) |
||
| 46 | file_strings = file_strings[file_strings.keys.first] |
||
| 47 | 441:cbce1fd3b1b7 | Chris | |
| 48 | 245:051f544170fe | Chris | file_strings.each do |key, string| |
| 49 | next unless string.is_a?(String) |
||
| 50 | string.scan /%\{\w+\}/ do |match|
|
||
| 51 | unless en_strings[key].nil? || en_strings[key].include?(match) |
||
| 52 | puts "#{file}: #{key} uses #{match} not found in en.yml"
|
||
| 53 | end |
||
| 54 | end |
||
| 55 | end |
||
| 56 | end |
||
| 57 | end |
||
| 58 | 37:94944d00e43c | chris | |
| 59 | desc <<-END_DESC |
||
| 60 | 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 | Options: |
||
| 63 | key=key_1,key_2 Comma-separated list of keys to delete |
||
| 64 | skip=en,de Comma-separated list of locale files to ignore (filename without extension) |
||
| 65 | END_DESC |
||
| 66 | |||
| 67 | task :remove_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 | deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
|
||
| 72 | # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :) |
||
| 73 | delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
|
||
| 74 | 441:cbce1fd3b1b7 | Chris | |
| 75 | 37:94944d00e43c | chris | files.each do |path| |
| 76 | # Skip certain locales |
||
| 77 | (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
|
||
| 78 | puts "Deleting selected keys from #{path}"
|
||
| 79 | orig_content = File.open(path, 'r') {|file| file.read}
|
||
| 80 | File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
|
||
| 81 | end |
||
| 82 | end |
||
| 83 | 441:cbce1fd3b1b7 | Chris | |
| 84 | 37:94944d00e43c | chris | desc <<-END_DESC |
| 85 | 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). |
||
| 86 | |||
| 87 | Options: |
||
| 88 | key="some_key=foo" |
||
| 89 | key1="another_key=bar" |
||
| 90 | key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised |
||
| 91 | skip=en,de Comma-separated list of locale files to ignore (filename without extension) |
||
| 92 | END_DESC |
||
| 93 | |||
| 94 | task :add_key do |
||
| 95 | dir = ENV['DIR'] || './config/locales' |
||
| 96 | files = Dir.glob(File.join(dir,'*.yml')) |
||
| 97 | skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
|
||
| 98 | keys_regex = /\Akey(\d+|_.+)?\z/ |
||
| 99 | adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
|
||
| 100 | key_list = adds.collect {|v| v[0]}.join(", ")
|
||
| 101 | |||
| 102 | files.each do |path| |
||
| 103 | # Skip certain locales |
||
| 104 | (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
|
||
| 105 | # TODO: Check for dupliate/existing keys |
||
| 106 | puts "Adding #{key_list} to #{path}"
|
||
| 107 | File.open(path, 'a') do |file| |
||
| 108 | adds.each do |kv| |
||
| 109 | Hash[*kv].to_yaml.each_line do |line| |
||
| 110 | file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
|
||
| 111 | end |
||
| 112 | end |
||
| 113 | end |
||
| 114 | end |
||
| 115 | end |
||
| 116 | 0:513646585e45 | Chris | end |