18 lines
738 B
TypeScript
18 lines
738 B
TypeScript
import { useT } from '@/i18n/useT'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import type { Locale } from '@/i18n/types'
|
|
|
|
/** `<key>_one` or `<key>_other` by the locale's plural rule (fr: 0 and 1 are singular). */
|
|
export function pluralKey(locale: Locale, key: string, count: number): string {
|
|
const category = new Intl.PluralRules(locale).select(count)
|
|
return `${key}_${category === 'one' ? 'one' : 'other'}`
|
|
}
|
|
|
|
/** A count phrase: picks the plural form of `key` and fills `{count}`. */
|
|
export function usePlural(): (key: string, count: number) => string {
|
|
const t = useT()
|
|
const locale = useLocaleStore((s) => s.locale)
|
|
return (key, count) =>
|
|
t(pluralKey(locale, key, count)).replace('{count}', String(count))
|
|
}
|