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, type Locale} from 'date-fns'
12import {
13 ca,
14 cy,
15 da,
16 de,
17 el,
18 enGB,
19 eo,
20 es,
21 eu,
22 fi,
23 fr,
24 fy,
25 gd,
26 gl,
27 hi,
28 hu,
29 id,
30 it,
31 ja,
32 km,
33 ko,
34 nl,
35 pl,
36 pt,
37 ptBR,
38 ro,
39 ru,
40 sv,
41 th,
42 tr,
43 uk,
44 vi,
45 zhCN,
46 zhHK,
47 zhTW,
48} from 'date-fns/locale'
49
50import {type AppLanguage} from '#/locale/languages'
51import {useLanguagePrefs} from '#/state/preferences'
52
53/**
54 * {@link AppLanguage}
55 */
56const locales: Record<AppLanguage, Locale | undefined> = {
57 en: undefined,
58 an: undefined,
59 ast: undefined,
60 ca,
61 cy,
62 da,
63 de,
64 el,
65 ['en-GB']: enGB,
66 eo,
67 es,
68 eu,
69 fi,
70 fr,
71 fy,
72 ga: undefined,
73 gd,
74 gl,
75 hi,
76 hu,
77 ia: undefined,
78 id,
79 it,
80 ja,
81 km,
82 ko,
83 ne: undefined,
84 nl,
85 pl,
86 ['pt-PT']: pt,
87 ['pt-BR']: ptBR,
88 ro,
89 ru,
90 sv,
91 th,
92 tr,
93 uk,
94 vi,
95 ['zh-Hans-CN']: zhCN,
96 ['zh-Hant-HK']: zhHK,
97 ['zh-Hant-TW']: zhTW,
98}
99
100/**
101 * Returns a localized `formatDistance` function.
102 * {@link formatDistance}
103 */
104export function useFormatDistance() {
105 const {appLanguage} = useLanguagePrefs()
106 return React.useCallback<typeof formatDistance>(
107 (date, baseDate, options) => {
108 const locale = locales[appLanguage as AppLanguage]
109 return formatDistance(date, baseDate, {...options, locale: locale})
110 },
111 [appLanguage],
112 )
113}