Something went wrong. Try again.
Bluesky app fork with some witchin' additions 💫 forked from jollywhoppers.com/witchsky.app
Something went wrong. Try again.
1.6 kB · 113 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114/** * Hooks for date-fns localized formatters. * * Our app supports some languages that are not included in date-fns by * default, in which case it will fall back to English. * * {@link https://github.com/date-fns/date-fns/blob/main/docs/i18n.md} */
import React from 'react'import {formatDistance, type Locale} from 'date-fns'import { ca, cy, da, de, el, enGB, eo, es, eu, fi, fr, fy, gd, gl, hi, hu, id, it, ja, km, ko, nl, pl, pt, ptBR, ro, ru, sv, th, tr, uk, vi, zhCN, zhHK, zhTW,} from 'date-fns/locale'
import {type AppLanguage} from '#/locale/languages'import {useLanguagePrefs} from '#/state/preferences'
/** * {@link AppLanguage} */const locales: Record<AppLanguage, Locale | undefined> = { en: undefined, an: undefined, ast: undefined, ca, cy, da, de, el, ['en-GB']: enGB, eo, es, eu, fi, fr, fy, ga: undefined, gd, gl, hi, hu, ia: undefined, id, it, ja, km, ko, ne: undefined, nl, pl, ['pt-PT']: pt, ['pt-BR']: ptBR, ro, ru, sv, th, tr, uk, vi, ['zh-Hans-CN']: zhCN, ['zh-Hant-HK']: zhHK, ['zh-Hant-TW']: zhTW,}
/** * Returns a localized `formatDistance` function. * {@link formatDistance} */export function useFormatDistance() { const {appLanguage} = useLanguagePrefs() return React.useCallback<typeof formatDistance>( (date, baseDate, options) => { const locale = locales[appLanguage as AppLanguage] return formatDistance(date, baseDate, {...options, locale: locale}) }, [appLanguage], )}