Repository navigation
Fix: Use absolute value for plural rules category selection - #545
Merged
Merged
Conversation
This PR addresses a long-standing inconsistency in the pluralization logic used by `PluralRules`. Previously, the implementation did not explicitly evaluate the absolute value of the input number, contrary to the CLDR specification. This led to incorrect plural category resolution for negative numbers, which typically defaulted to the `OTHER` category. #### Changes - The decimal argument passed to all `PluralRules` delegates is now transformed to its absolute value before evaluation. - This change ensures that plural category resolution aligns with CLDR expectations, particularly because locales with plural forms depend on numeric value regardless of sign. #### Impact - Fixes incorrect plural category resolution for negative numbers in locales such as `DualOneOther`, where the number of plural word variants influences the outcome. - May affect pluralization behavior in edge cases across multiple locales. Unit tests have been updated to reflect the corrected logic. - The change affects `PluralLocalizationFormatter` and `TimeFormatter`. - **Thoroughly test the new version in your localization workflows**, especially if you rely on custom pluralization delegates or locale-specific plural form resolution. This change may surface previously masked edge cases. #### Versioning - The PR will be published with a new **minor version**, as this change corrects a bug but may alter behavior in downstream formatting logic. #### Background See discussion in #503 and #497 for context.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #545 +/- ##
===================================
- Coverage 98% 98% -0%
===================================
Files 101 101
Lines 3622 3621 -1
===================================
- Hits 3549 3548 -1
Misses 73 73 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
axunonb
force-pushed
the
pr/legacy-plural-rules
branch
from
December 5, 2026 22:09
1681629 to
40594d4
Compare
CLDR plural rules are defined on the absolute value of a number (operand n = |N|, UTS #35 "Plural Operands"). The formatters passed signed values to the rules, so negative numbers could select the wrong category. For example, -1 in en produced "There are -1 items" instead of "There is -1 item". Changes - PluralLocalizationFormatter and TimeTextInfo pass Math.Abs(value) to the plural rule. The formatted output still shows the signed value. - PluralRules: removed the negative-value checks in DualOneOther (4 words), DualFromZeroToTwo (2 words), GetWordsCount4Value and Tachelhit. Built-in callers never pass negative values to them. - PluralRuleDelegate documents that its value argument is the absolute value. - Updated the -1 expectations in PluralLocalizationFormatterTests (en, fr, tr). Impact - Changes output for negative numbers in every locale whose plural rules depend on the value. Tests cover en, fr and tr. - Custom PluralRuleDelegates used through the formatters receive the absolute value. - Custom PluralRuleDelegates called directly with negative numbers receive the signed value, and the rules listed above can return a different category than before. Versioning - Visible output change for negative numbers. Proposed as a minor release (e.g. 3.7.0). Background: #503, #497
axunonb
force-pushed
the
pr/legacy-plural-rules
branch
from
December 5, 2026 22:33
40594d4 to
ac2d7d9
Compare
Macedonian was registered under "mb", which is not an ISO 639-1 code, so plural formatting with mk or mk-MK cultures threw "IsoLangToDelegate not found for mk". The rule also compared the whole value with 11, so 111, 211, ... resolved to one. CLDR uses i % 10 = 1 and i % 100 != 11 for one.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Plural rules are defined by CLDR (UTS, tr35-numbers) on the absolute value of a number (operand n = |N|). The formatters passed signed values to the rules, so negative numbers fell through to the "other" category. For example, -1 in en produced "There are -1 items" instead of "There is -1 item".
Changes
PluralLocalizationFormatterandTimeTextInfopassMath.Abs(value)to the plural rule. The formatted output still shows the signed value.PluralRules: removed the negative-value checks in DualOneOther (4 words), DualFromZeroToTwo (2 words), GetWordsCount4Value and Tachelhit. Built-in callers never pass negative values to them.PluralRuleDelegatedocuments that its value argument is the absolute value.PluralLocalizationFormatterTests(en, fr, tr).Impact
PluralRuleDelegatesused through the formatters receive the absolute value.PluralRuleDelegatescalled directly with negative numbers receive the signed value, and the rules listed above can return a different category than before.CustomPluralRuleProviderorIsoLangToDelegate) used through the formatters cannot restore the old output. If you call aPluralRuleDelegatedirectly with negative numbers, you still get the previous result.Versioning
Background: #503, #497