@@ -1,53 +1,53 | |||||
1 | namespace :gloc do |
|
1 | namespace :gloc do | |
2 | desc 'Sorts the keys in the lang ymls' |
|
2 | desc 'Sorts the keys in the lang ymls' | |
3 | task :sort do |
|
3 | task :sort do | |
4 | dir = ENV['DIR'] || '{.,vendor/plugins/*}/lang' |
|
4 | dir = ENV['DIR'] || '{.,vendor/plugins/*}/lang' | |
5 | puts "Processing directory #{dir}" |
|
5 | puts "Processing directory #{dir}" | |
6 | files = Dir.glob(File.join(dir,'*.{yaml,yml}')) |
|
6 | files = Dir.glob(File.join(dir,'*.{yaml,yml}')) | |
7 | puts 'No files found.' if files.empty? |
|
7 | puts 'No files found.' if files.empty? | |
8 | files.each {|file| |
|
8 | files.each {|file| | |
9 | puts "Sorting file: #{file}" |
|
9 | puts "Sorting file: #{file}" | |
10 | header = [] |
|
10 | header = [] | |
11 | content = IO.readlines(file) |
|
11 | content = IO.readlines(file) | |
12 | content.each {|line| line.gsub!(/[\s\r\n\t]+$/,'')} |
|
12 | content.each {|line| line.gsub!(/[\s\r\n\t]+$/,'')} | |
13 | content.delete_if {|line| line==''} |
|
13 | content.delete_if {|line| line==''} | |
14 | tmp= [] |
|
14 | tmp= [] | |
15 | content.each {|x| tmp << x unless tmp.include?(x)} |
|
15 | content.each {|x| tmp << x unless tmp.include?(x)} | |
16 | content= tmp |
|
16 | content= tmp | |
17 | header << content.shift if !content.empty? && content[0] =~ /^file_charset:/ |
|
17 | header << content.shift if !content.empty? && content[0] =~ /^file_charset:/ | |
18 | content.sort! |
|
18 | content.sort! | |
19 | filebak = "#{file}.bak" |
|
19 | filebak = "#{file}.bak" | |
20 | File.rename file, filebak |
|
20 | File.rename file, filebak | |
21 | File.open(file, 'w') {|fout| fout << header.join("\n") << content.join("\n") << "\n"} |
|
21 | File.open(file, 'w') {|fout| fout << header.join("\n") << content.join("\n") << "\n"} | |
22 | File.delete filebak |
|
22 | File.delete filebak | |
23 | # Report duplicates |
|
23 | # Report duplicates | |
24 | count= {} |
|
24 | count= {} | |
25 | content.map {|x| x.gsub(/:.+$/, '') }.each {|x| count[x] ||= 0; count[x] += 1} |
|
25 | content.map {|x| x.gsub(/:.+$/, '') }.each {|x| count[x] ||= 0; count[x] += 1} | |
26 | count.delete_if {|k,v|v==1} |
|
26 | count.delete_if {|k,v|v==1} | |
27 | puts count.keys.sort.map{|x|" WARNING: Duplicate key '#{x}' (#{count[x]} occurances)"}.join("\n") unless count.empty? |
|
27 | puts count.keys.sort.map{|x|" WARNING: Duplicate key '#{x}' (#{count[x]} occurances)"}.join("\n") unless count.empty? | |
28 | } |
|
28 | } | |
29 | end |
|
29 | end | |
30 |
|
30 | |||
31 |
desc 'Updates language files based on e |
|
31 | desc 'Updates language files based on en.yml content' | |
32 | task :update do |
|
32 | task :update do | |
33 | dir = ENV['DIR'] || './lang' |
|
33 | dir = ENV['DIR'] || './lang' | |
34 |
|
34 | |||
35 | en_strings = {} |
|
35 | en_strings = {} | |
36 | en_file = File.open(File.join(dir,'en.yml'), 'r') |
|
36 | en_file = File.open(File.join(dir,'en.yml'), 'r') | |
37 | en_file.each_line {|line| en_strings[$1] = $2 if line =~ %r{^([\w_]+):\s(.+)$} } |
|
37 | en_file.each_line {|line| en_strings[$1] = $2 if line =~ %r{^([\w_]+):\s(.+)$} } | |
38 | en_file.close |
|
38 | en_file.close | |
39 |
|
39 | |||
40 | files = Dir.glob(File.join(dir,'*.{yaml,yml}')) |
|
40 | files = Dir.glob(File.join(dir,'*.{yaml,yml}')) | |
41 | files.each do |file| |
|
41 | files.each do |file| | |
42 | puts "Updating file #{file}" |
|
42 | puts "Updating file #{file}" | |
43 | keys = IO.readlines(file).collect {|line| $1 if line =~ %r{^([\w_]+):\s(.+)$} }.compact |
|
43 | keys = IO.readlines(file).collect {|line| $1 if line =~ %r{^([\w_]+):\s(.+)$} }.compact | |
44 | lang = File.open(file, 'a') |
|
44 | lang = File.open(file, 'a') | |
45 | en_strings.each do |key, str| |
|
45 | en_strings.each do |key, str| | |
46 | next if keys.include?(key) |
|
46 | next if keys.include?(key) | |
47 | puts "added: #{key}" |
|
47 | puts "added: #{key}" | |
48 | lang << "#{key}: #{str}\n" |
|
48 | lang << "#{key}: #{str}\n" | |
49 | end |
|
49 | end | |
50 | lang.close |
|
50 | lang.close | |
51 | end |
|
51 | end | |
52 | end |
|
52 | end | |
53 | end No newline at end of file |
|
53 | end |
General Comments 0
You need to be logged in to leave comments.
Login now