@formatjs/intl-pluralrules
- Version 6.2.2
- Published
- 373 kB
- 4 dependencies
- MIT license
Install
npm i @formatjs/intl-pluralrulesyarn add @formatjs/intl-pluralrulespnpm add @formatjs/intl-pluralrulesOverview
Polyfill for Intl.PluralRules
Index
Classes
Interfaces
Namespaces
Classes
class PluralRules
class PluralRules {}constructor
constructor(locales?: string | string[], options?: Intl.PluralRulesOptions);property availableLocales
static availableLocales: Set<string>;property localeData
static localeData: Record<string, PluralRulesData>;property polyfilled
static polyfilled: boolean;property relevantExtensionKeys
static relevantExtensionKeys: never[];method getDefaultLocale
static getDefaultLocale: () => string;method resolvedOptions
resolvedOptions: () => Intl.ResolvedPluralRulesOptions;method select
select: (val: number | bigint) => LDMLPluralRule;method selectRange
selectRange: (start: number | bigint, end: number | bigint) => LDMLPluralRule;Intl.PluralRules.prototype.selectRange ( start, end )
Returns a string indicating which plural rule applies to a range of numbers. This is useful for formatting ranges like "1-2 items" vs "2-3 items" where different languages have different plural rules for ranges.
Specification: https://tc39.es/ecma402/#sec-intl.pluralrules.prototype.selectrange
Parameter start
The start value of the range (number or bigint)
Parameter end
The end value of the range (number or bigint)
Returns
The plural category for the range (zero, one, two, few, many, or other)
Throws
{TypeError} If start or end is undefined
Throws
{RangeError} If start or end is not a finite number (Infinity, NaN)
Chrome's native implementation (as of early 2025) has a bug where it throws "Cannot convert a BigInt value to a number" when using BigInt arguments. This is a browser bug - the spec requires BigInt support. This polyfill handles BigInt correctly.
Example 1
const pr = new Intl.PluralRules('en'); pr.selectRange(1, 2); // "other" (English: "1-2 items") pr.selectRange(1, 1); // "one" (same value: "1 item")
Example 2
const prFr = new Intl.PluralRules('fr'); prFr.selectRange(0, 1); // "one" (French: "0-1 vue") prFr.selectRange(1, 2); // "other" (French: "1-2 vues")
Example 3
// BigInt support (spec-compliant, but Chrome has a bug as of early 2025) pr.selectRange(BigInt(1), BigInt(2)); // "other"
method supportedLocalesOf
static supportedLocalesOf: ( locales?: string | string[], options?: Pick<Intl.PluralRulesOptions, 'localeMatcher'>) => string[];method toString
toString: () => string;Interfaces
interface PluralRulesInternal
interface PluralRulesInternal extends NumberFormatDigitInternalSlots {}property compactDisplay
compactDisplay?: 'short' | 'long';property dataLocaleData
dataLocaleData?: any;property initializedPluralRules
initializedPluralRules: boolean;property locale
locale: string;property notation
notation: 'standard' | 'compact';property type
type: 'cardinal' | 'ordinal';Namespaces
namespace global
namespace global {}Type augmentation for Intl.PluralRules
ECMA-402 Spec: selectRange method (Intl.PluralRules.prototype.selectRange) https://tc39.es/ecma402/#sec-intl.pluralrules.prototype.selectrange
Extension: notation and compactDisplay options (not in ECMA-402 spec) Mirrors Intl.NumberFormat notation option for proper plural selection with compact numbers
namespace global.Intl
namespace global.Intl {}interface PluralRules
interface PluralRules {}method selectRange
selectRange: (start: number | bigint, end: number | bigint) => LDMLPluralRule;interface PluralRulesOptions
interface PluralRulesOptions {}property compactDisplay
compactDisplay?: 'short' | 'long';property notation
notation?: 'standard' | 'compact';Package Files (1)
Dependencies (4)
Dev Dependencies (2)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@formatjs/intl-pluralrules.
- Markdown[](https://www.jsdocs.io/package/@formatjs/intl-pluralrules)
- HTML<a href="https://www.jsdocs.io/package/@formatjs/intl-pluralrules"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 4028 ms. - Missing or incorrect documentation? Open an issue for this package.
