@@ -1,31 +1,31 | |||||
1 | namespace :locales do |
|
1 | namespace :locales do | |
2 | desc 'Updates language files based on en.yml content (only works for new top level keys).' |
|
2 | desc 'Updates language files based on en.yml content (only works for new top level keys).' | |
3 | task :update do |
|
3 | task :update do | |
4 | dir = ENV['DIR'] || './config/locales' |
|
4 | dir = ENV['DIR'] || './config/locales' | |
5 |
|
5 | |||
6 | en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] |
|
6 | en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en'] | |
7 |
|
7 | |||
8 |
files = Dir.glob(File.join(dir,' |
|
8 | files = Dir.glob(File.join(dir,'*.{yaml,yml}')) | |
9 | files.each do |file| |
|
9 | files.each do |file| | |
10 | puts "Updating file #{file}" |
|
10 | puts "Updating file #{file}" | |
11 | file_strings = YAML.load(File.read(file)) |
|
11 | file_strings = YAML.load(File.read(file)) | |
12 | file_strings = file_strings[file_strings.keys.first] |
|
12 | file_strings = file_strings[file_strings.keys.first] | |
13 |
|
13 | |||
14 | missing_keys = en_strings.keys - file_strings.keys |
|
14 | missing_keys = en_strings.keys - file_strings.keys | |
15 | next if missing_keys.empty? |
|
15 | next if missing_keys.empty? | |
16 |
|
16 | |||
17 | puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})" |
|
17 | puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})" | |
18 | lang = File.open(file, 'a') |
|
18 | lang = File.open(file, 'a') | |
19 |
|
19 | |||
20 | missing_keys.each do |key| |
|
20 | missing_keys.each do |key| | |
21 | {key => en_strings[key]}.to_yaml.each_line do |line| |
|
21 | {key => en_strings[key]}.to_yaml.each_line do |line| | |
22 | next if line =~ /^---/ || line.empty? |
|
22 | next if line =~ /^---/ || line.empty? | |
23 | puts " #{line}" |
|
23 | puts " #{line}" | |
24 | lang << " #{line}" |
|
24 | lang << " #{line}" | |
25 | end |
|
25 | end | |
26 | end |
|
26 | end | |
27 |
|
27 | |||
28 | lang.close |
|
28 | lang.close | |
29 | end |
|
29 | end | |
30 | end |
|
30 | end | |
31 | end |
|
31 | end |
General Comments 0
You need to be logged in to leave comments.
Login now