Skip to content

Fix: Use absolute value for plural rules category selection - #545

Merged
axunonb merged 4 commits into
mainfrom
pr/legacy-plural-rules
Dec 5, 2026
Merged

axunonb merged 4 commits into
mainfrom
pr/legacy-plural-rules

Conversation

@axunonb

@axunonb axunonb commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

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

  • 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.
  • There is no opt-in setting. The formatters pass the absolute value to the plural rule, so a custom delegate (via CustomPluralRuleProvider or IsoLangToDelegate) used through the formatters cannot restore the old output. If you call a PluralRuleDelegate directly with negative numbers, you still get the previous result.

Versioning

  • Visible output change for negative numbers. Proposed as a minor release (e.g. 3.7.0).

Background: #503, #497

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

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98%. Comparing base (699fef3) to head (4c05e29).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@axunonb
axunonb force-pushed the pr/legacy-plural-rules branch from 1681629 to 40594d4 Compare December 5, 2026 22:09
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
axunonb force-pushed the pr/legacy-plural-rules branch from 40594d4 to ac2d7d9 Compare December 5, 2026 22:33
vtxe02023274 and others added 2 commits December 6, 2026 00:31
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.
@axunonb
axunonb merged commit 7901ec4 into main Dec 5, 2026
6 checks passed
@axunonb
axunonb deleted the pr/legacy-plural-rules branch December 5, 2026 23:40
@sonarqubecloud

sonarqubecloud Bot commented Dec 5, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants