MCPcopy
hub / github.com/formatjs/formatjs / formatPlural

Function formatPlural

packages/intl/plural.ts:12–48  ·  view source on GitHub ↗
(
  {
    locale,
    onError,
  }: {
    locale: string
    onError: OnErrorFn
  },
  getPluralRules: Formatters['getPluralRules'],
  value: Parameters<IntlFormatters['formatPlural']>[0],
  options: Parameters<IntlFormatters['formatPlural']>[1] = {}
)

Source from the content-addressed store, hash-verified

10const PLURAL_FORMAT_OPTIONS: Array<keyof Intl.PluralRulesOptions> = ['type']
11
12export function formatPlural(
13 {
14 locale,
15 onError,
16 }: {
17 locale: string
18 onError: OnErrorFn
19 },
20 getPluralRules: Formatters['getPluralRules'],
21 value: Parameters<IntlFormatters['formatPlural']>[0],
22 options: Parameters<IntlFormatters['formatPlural']>[1] = {}
23): Intl.LDMLPluralRule {
24 if (!Intl.PluralRules) {
25 onError(
26 new FormatError(
27 `Intl.PluralRules is not available in this environment.
28Try polyfilling it using "@formatjs/intl-pluralrules"
29`,
30 ErrorCode.MISSING_INTL_API
31 )
32 )
33 }
34 const filteredOptions = filterProps(
35 options,
36 PLURAL_FORMAT_OPTIONS
37 ) as Intl.PluralRulesOptions
38
39 try {
40 return getPluralRules(locale, filteredOptions).select(
41 value
42 ) as Intl.LDMLPluralRule
43 } catch (e) {
44 onError(new IntlFormatError('Error formatting plural.', locale, e))
45 }
46
47 return 'other'
48}

Callers 2

FormattedPluralFunction · 0.85

Calls 3

onErrorFunction · 0.85
filterPropsFunction · 0.85
selectMethod · 0.80

Tested by

no test coverage detected