Skip to content

Commit

Permalink
Ignore specific plural parts
Browse files Browse the repository at this point in the history
  • Loading branch information
markedmondson committed Dec 14, 2024
1 parent debc937 commit 76ecc39
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/i18n/tasks/missing_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,23 @@ def missing_plural_tree(locale, base = base_locale, _skip_interpolation = false)
Set.new
end
# Compare the keys to those existing in base
next if ignore_key?(node.full_key(root: false), :missing, locale)
next if present_keys.superset?(required_keys)

# Mark for removal any existing keys that are not required
base_keys = Set.new(node.children.map { |c| c.key.to_sym })
missing_keys = (required_keys - present_keys).to_a
remove_keys = (present_keys + base_keys) - required_keys

# Remove any ignored plural keys, eg: 'something.key.few' or '*.many'
missing_keys.reject! do |plural_key|
ignore_key?("#{node.full_key(root: false)}.#{plural_key}", :missing, locale)
end
next if missing_keys.empty? && remove_keys.empty?

tree[node.full_key] = node.derive(
value: children.to_hash,
children: nil,
data: node.data.merge(missing_keys: (required_keys - present_keys).to_a, remove_keys: remove_keys)
data: node.data.merge(missing_keys: missing_keys, remove_keys: remove_keys)
)
end
tree.set_root_key!(locale, type: :missing_plural)
Expand Down
11 changes: 9 additions & 2 deletions spec/plural_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
'config/i18n-tasks.yml' => {
base_locale: 'en',
locales: %w[en ar],
ignore_missing: ['ignored_pattern.*']
ignore_missing: ['ignored_pattern.*', '*.plural_key.two']
}.to_yaml,
'config/locales/en.yml' => { en: base_keys }.to_yaml,
'config/locales/ar.yml' => { ar: base_keys }.to_yaml
Expand Down Expand Up @@ -75,8 +75,15 @@ def depluralize(key)
expect(leaves.size).to eq 2
expect(leaves[0].full_key).to eq 'ar.plural_key'
expect(leaves[0].data[:missing_keys]).to eq %i[zero two few many]
end

it 'ignores specified pluralizations' do
wrong = task.missing_plural_forest(%w[en ar])
leaves = wrong.leaves.to_a

expect(leaves.size).to eq 2
expect(leaves[1].full_key).to eq 'ar.nested.plural_key'
expect(leaves[1].data[:missing_keys]).to eq %i[two few many]
expect(leaves[1].data[:missing_keys]).to eq %i[few many]
end

it 'ignores keys with a single interpolation string' do
Expand Down

0 comments on commit 76ecc39

Please sign in to comment.