mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1/**
2 * Hooks for date-fns localized formatters.
3 *
4 * Our app supports some languages that are not included in date-fns by
5 * default, in which case it will fall back to English.
6 *
7 * {@link https://github.com/date-fns/date-fns/blob/main/docs/i18n.md}
8 */
9
10import React from 'react'
11import {formatDistance, Locale} from 'date-fns'
12import {
13 ca,
14 de,
15 es,
16 fi,
17 fr,
18 hi,
19 id,
20 it,
21 ja,
22 ko,
23 ptBR,
24 tr,
25 uk,
26 zhCN,
27 zhTW,
28} from 'date-fns/locale'
29
30import {AppLanguage} from '#/locale/languages'
31import {useLanguagePrefs} from '#/state/preferences'
32
33/**
34 * {@link AppLanguage}
35 */
36const locales: Record<AppLanguage, Locale | undefined> = {
37 en: undefined,
38 ca,
39 de,
40 es,
41 fi,
42 fr,
43 ga: undefined,
44 hi,
45 id,
46 it,
47 ja,
48 ko,
49 ['pt-BR']: ptBR,
50 tr,
51 uk,
52 ['zh-CN']: zhCN,
53 ['zh-TW']: zhTW,
54}
55
56/**
57 * Returns a localized `formatDistance` function.
58 * {@link formatDistance}
59 */
60export function useFormatDistance() {
61 const {appLanguage} = useLanguagePrefs()
62 return React.useCallback<typeof formatDistance>(
63 (date, baseDate, options) => {
64 const locale = locales[appLanguage as AppLanguage]
65 return formatDistance(date, baseDate, {...options, locale: locale})
66 },
67 [appLanguage],
68 )
69}