Skip to content

Commit

Permalink
Missing plural keys: Extract plural_nodes method
Browse files Browse the repository at this point in the history
Refs #309
Refs #310
  • Loading branch information
glebm committed Oct 26, 2018
1 parent 5dca119 commit 44409e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 1 addition & 5 deletions lib/i18n/tasks/missing_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,9 @@ def missing_used_forest(locales, _base = base_locale)
def missing_plural_forest(locales, _base = base_locale)
locales.each_with_object(empty_forest) do |locale, tree|
next unless I18n.exists?(:'i18n.plural.keys', locale)

required_keys = Set.new(I18n.t(:'i18n.plural.keys', locale: locale, resolve: false))

data[locale].leaves.map(&:parent).compact.uniq.each do |node|
plural_nodes data[locale] do |node|
children = node.children
next unless plural_forms?(children)
present_keys = Set.new(children.map { |c| c.key.to_sym })
next if present_keys.superset?(required_keys)
tree[node.full_key] = node.derive(
Expand All @@ -73,7 +70,6 @@ def missing_plural_forest(locales, _base = base_locale)
data: node.data.merge(missing_keys: (required_keys - present_keys).to_a)
)
end

tree.set_root_key!(locale, type: :missing_plural)
end
end
Expand Down
14 changes: 14 additions & 0 deletions lib/i18n/tasks/plural_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ def depluralize_key(key, locale = base_locale)
end
end

# @param [::I18n::Tasks::Data::Tree::Traversal] tree
# @yieldparam node [::I18n::Tasks::Data::Tree::Node] plural node
def plural_nodes(tree)
return to_enum(:plural_nodes, tree) unless block_given?
visited = Set.new
tree.leaves.each do |node|
parent = node.parent
next if !parent || visited.include?(parent)
yield parent if plural_forms?(parent.children)
visited.add(parent)
end
self
end

def plural_forms?(s)
s.present? && s.all? { |node| node.leaf? && plural_suffix?(node.key) }
end
Expand Down

0 comments on commit 44409e3

Please sign in to comment.