Vize

use-plural-rules

Choose and render a message by CLDR plural category (Intl.PluralRules).

Package @vizejs/composable/use-plural-rules
Own the source vize lib pull composable:use-plural-rules
Runtime exports usePluralRules
Gzip budget 2560 B

Usage

import { usePluralRules } from "@vizejs/composable/use-plural-rules";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
usePluralRules i18n experimental deterministic-fallback caller-managed none web, server, worker, native, desktop, terminal Intl, window useLocale

API

usePluralRules

Choose and render a message by CLDR plural category (Intl.PluralRules). Messages are typed per category with a mandatory other, plus optional exact overrides ("=0": "No items"). Cardinal (1 item) and ordinal (1st, 2nd) rules are supported via type. The count is formatted with the same locale for interpolation. Everything is derived state: no listeners or timers, deterministic during server rendering when the locale is explicit. Invalid options surface as the platform RangeError.

function usePluralRules( count: MaybeRefOrGetter<number>, messages: MaybeRefOrGetter<PluralMessages>, options: MaybeRefOrGetter<UsePluralRulesOptions> = {}, ): PluralRulesControls
const { message } = usePluralRules(count, { "=0": "No files", one: "# file", other: "# files" }, { locale: "en" });

Types

UsePluralRulesOptions

Options for usePluralRules.

Member Type Description
locale? string | Intl.Locale Locale used for plural selection and count formatting. Pass it explicitly for hydration-stable server rendering.

PluralRulesControls

Reactive plural selection returned by usePluralRules.

Member Type Description
locale ComputedRef<string> Canonical locale in use.
rules ComputedRef<Intl.PluralRules> Plural rules for the current locale and options.
category ComputedRef<PluralCategory> Category of the current count.
message ComputedRef<string> Message for the current count with {count} / # interpolated.
select (count: number) => PluralCategory Plural category of any count.
format (count: number) => string Message for any count.