Created
April 1, 2016 04:29
-
-
Save JokerCatz/842bcd44ab3cc8aabbd2ef200447e832 to your computer and use it in GitHub Desktop.
Rails i18n unused key scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
require 'awesome_print' | |
require 'open3' | |
###### keys diff start | |
def flat_hash(hash , flag = [] , global = {}) | |
return global.update({flag=>hash}) unless hash.is_a?(Hash) | |
hash.each do |key,rnd| | |
flat_hash(rnd,flag+[key],global) | |
end | |
global | |
end | |
keys = {tw:'zh-tw',cn:'zh-CN',en:'en'} | |
all_keys = keys.keys.map{|key|[key,{}]}.to_h | |
keys.each_pair do |key , file_name| | |
keys[key] = flat_hash(YAML.load_file("#{__dir__}/../config/locales/#{file_name}.yml")[file_name]).keys.map{|i|i.join('::')} | |
end | |
keys.each_pair do |main_lang , main_keys| keys.each_pair do |sub_lang , sub_keys| | |
next if main_lang == sub_lang | |
(main_keys - sub_keys).each do |key| | |
all_keys[main_lang][key] ||= [] | |
all_keys[main_lang][key] << sub_lang | |
end | |
end end | |
all_keys.each_pair do |main_lang , sub_set| sub_set.each do |key , sub_langs| | |
puts "(#{main_lang} - #{sub_langs.join(',')}) => #{key}" | |
end end | |
###### keys diff end | |
###### keys unused start | |
all_keys = {} | |
keys.each_value do |value_set| value_set.each do |value| | |
all_keys[value.split('::').pop] = true | |
end end | |
all_keys = all_keys.keys | |
puts "start_search : #{all_keys.length} keys" | |
len = all_keys.length | |
flag = 0 | |
flag_char = {0 => '-' , 1 => '\\' , 2 => '|' , 3 => '/'} | |
all_keys.each_with_index do |key , index| | |
index += 1 | |
begin | |
opts, errors, status = Open3.capture3(%Q%find #{__dir__}/../ -type f \\( -name "*.rb" -o -name "*.erb" \\) | xargs grep -m 1 "#{key}"%) | |
if !errors.empty? | |
puts "\r#{index}/#{len} : search_error1 : #{key}" | |
elsif opts.empty? | |
puts "\r#{index}/#{len} : maybe unused : #{key}" | |
else | |
flag = (flag + 1) % 4 | |
print "\r#{flag_char[flag]}" | |
end | |
rescue | |
puts "\r#{index}/#{len} : search_error2 : #{key}" | |
end | |
end | |
###### keys unused end | |
puts "[scan finished , please fix me ^.~]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment