(
{
locale,
onError,
}: {
locale: string
onError: OnErrorFn
},
getPluralRules: Formatters['getPluralRules'],
value: Parameters<IntlFormatters['formatPlural']>[0],
options: Parameters<IntlFormatters['formatPlural']>[1] = {}
)
| 10 | const PLURAL_FORMAT_OPTIONS: Array<keyof Intl.PluralRulesOptions> = ['type'] |
| 11 | |
| 12 | export 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. |
| 28 | Try 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 | } |
no test coverage detected