From 76ecc39f8f2d95c42849f2acc6b5d21520dce452 Mon Sep 17 00:00:00 2001 From: Mark Edmondson Date: Fri, 13 Dec 2024 12:38:59 -0800 Subject: [PATCH] Ignore specific plural parts --- lib/i18n/tasks/missing_keys.rb | 10 ++++++++-- spec/plural_keys_spec.rb | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/i18n/tasks/missing_keys.rb b/lib/i18n/tasks/missing_keys.rb index 3a5de01d..3befcec5 100644 --- a/lib/i18n/tasks/missing_keys.rb +++ b/lib/i18n/tasks/missing_keys.rb @@ -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) diff --git a/spec/plural_keys_spec.rb b/spec/plural_keys_spec.rb index b25bc9d0..41cfba2f 100644 --- a/spec/plural_keys_spec.rb +++ b/spec/plural_keys_spec.rb @@ -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 @@ -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