lib/i18n/tasks/missing_keys.rb in i18n-tasks-0.8.5 vs lib/i18n/tasks/missing_keys.rb in i18n-tasks-0.8.6

- old
+ new

@@ -1,6 +1,7 @@ # coding: utf-8 +require 'set' module I18n::Tasks module MissingKeys MISSING_TYPES = { used: {glyph: '✗', summary: 'used in code but missing from base locale'}, @@ -46,26 +47,16 @@ } end tree end - def missing_used_forest(locales, base = base_locale) - if locales.include?(base) - missing_used_tree(base) - else - empty_forest - end + def missing_used_forest(locales, _base = base_locale) + locales.inject(empty_forest) { |forest, locale| + forest.merge! missing_used_tree(locale) + } end - def missing_tree(locale, compared_to) - if locale == compared_to - missing_used_tree locale - else - missing_diff_tree locale, compared_to - end - end - # keys present in compared_to, but not in locale def missing_diff_tree(locale, compared_to = base_locale) data[compared_to].select_keys { |key, _node| locale_key_missing? locale, depluralize_key(key, compared_to) }.set_root_key!(locale, type: :missing_diff).keys { |_key, node| @@ -93,8 +84,35 @@ }.set_root_key!(locale, type: :eq_base) end def locale_key_missing?(locale, key) !key_value?(key, locale) && !ignore_key?(key, :missing) + end + + # @param [::I18n::Tasks::Data::Tree::Siblings] forest + def collapse_missing_used_locales!(forest) + locales_and_nodes_by_key = {} + to_remove = [] + forest.each do |root| + locale = root.key + root.leaves { |node| + if node.data[:type] == :missing_used + (locales_and_nodes_by_key[node.full_key(root: false)] ||= []) << [locale, node] + to_remove << node + end + } + end + forest.remove_nodes_collapsing_emptied_ancestors! to_remove + keys_and_nodes_by_locale = {} + locales_and_nodes_by_key.each { |key, locales_and_nodes| + locales = locales_and_nodes.map(&:first).sort.join('+') + (keys_and_nodes_by_locale[locales] ||= []) << [key, locales_and_nodes[0][1]] + } + keys_and_nodes_by_locale.map { |locales, keys_nodes| + keys_nodes.each { |(key, node)| + forest["#{locales}.#{key}"] = node + } + } + forest end end end