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