Skip to content

Commit

Permalink
Merge pull request #5 from bookingexperts/fix-pluralize-suffix-specia…
Browse files Browse the repository at this point in the history
…l-cases

Add support for explicit `0` and `1` rules in #depluralize_key.
  • Loading branch information
gstokkink authored Jan 13, 2025
2 parents d30e6bf + b8a314c commit 0cc2470
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/i18n/tasks/plural_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ def plural_forms?(s)
end

def plural_suffix?(key)
PLURAL_KEY_SUFFIXES.include?(key)
PLURAL_KEY_SUFFIXES.include?(key) || %w[0 1].include?(key)
end
end
20 changes: 19 additions & 1 deletion spec/plural_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
plural_key: {
other: '%{count}'
}
},

explicit_0_1_rules: {
'0': 'explicit zero',
'1': 'explicit one',
one: 'one',
other: '%{count}'
}
}
end
Expand Down Expand Up @@ -62,6 +69,15 @@
expect(depluralize('not_really_plural.one')).to eq('not_really_plural.one')
end

it 'ignores `0` and `1` explicit rules' do
expect(depluralize('explicit_0_1_rules.0')).to eq('explicit_0_1_rules.0')
expect(depluralize('explicit_0_1_rules.1')).to eq('explicit_0_1_rules.1')
end

it 'depluralizes keys that have explicit `0` and `1` rules siblings' do
expect(depluralize('explicit_0_1_rules.other')).to eq('explicit_0_1_rules')
end

def depluralize(key)
task.depluralize_key(key, 'en')
end
Expand All @@ -72,11 +88,13 @@ def depluralize(key)
wrong = task.missing_plural_forest(%w[en ar])
leaves = wrong.leaves.to_a

expect(leaves.size).to eq 2
expect(leaves.size).to eq 3
expect(leaves[0].full_key).to eq 'ar.plural_key'
expect(leaves[0].data[:missing_keys]).to eq %i[zero two few many]
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[2].full_key).to eq 'ar.explicit_0_1_rules'
expect(leaves[2].data[:missing_keys]).to eq %i[zero two few many]
end
end
end

0 comments on commit 0cc2470

Please sign in to comment.