Last active
August 29, 2015 14:28
-
-
Save quamen/0fcfb82d721beefbe015 to your computer and use it in GitHub Desktop.
Dump out a list of unique vs non-unique translations
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
# We have a custom cane extention that we use to ensure the numbers don't change without us knowing that runs as part of CI. | |
namespace :translation do | |
class Hash | |
# like invert but not lossy | |
# {"one"=>1,"two"=>2, "1"=>1, "2"=>2}.inverse => {1=>["one", "1"], 2=>["two", "2"]} | |
def safe_invert | |
each_with_object({}) do |(key,value),out| | |
out[value] ||= [] | |
out[value] << key | |
end | |
end | |
end | |
def initialize_i18n_translation_store | |
I18n.t("lol") | |
rescue I18n::MissingTranslationData | |
end | |
def unique_count_excluding_gems(translation_keys) | |
translation_keys.reject do |e| | |
e.to_s =~ /\.(faker|dotiw)\./ | |
end.uniq.count | |
end | |
desc "Dump out a list of unique vs non-unique translations" | |
task :stats => :environment do | |
initialize_i18n_translation_store | |
translations = I18n.backend.send(:translations) | |
values = iterate(translations) | |
count = 0 | |
key_count = 0 | |
values.safe_invert.each do |k,v| | |
if unique_count_excluding_gems(v) > 1 && !k.is_a?(Symbol) && !k.to_s.match(/^%{\S*}$/) | |
count += 1 | |
puts k | |
v.each do |v| | |
key_count += 1 | |
puts " #{v}" | |
end | |
end | |
end | |
puts "---" | |
puts "Number of duplicated strings: #{count}" | |
puts "Defined in #{key_count} keys" | |
end | |
def iterate(translation_hash, values = {}, key = "") | |
translation_hash.each do |k,v| | |
new_key = "#{key}.#{k}" | |
if v.is_a?(Hash) | |
values = iterate(v, values, new_key) | |
else | |
values[new_key] = v | |
end | |
end | |
values | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment