-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathlocale.js
More file actions
144 lines (112 loc) · 3.35 KB
/
Copy pathlocale.js
File metadata and controls
144 lines (112 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
import { useUser } from './index'
import {
SUPPORTED_LOCALES,
DEFAULT_LOCALE,
LOCALE_COOKIE,
isSupportedLocale,
parseCookieHeader,
negotiateLocale,
negotiateLocaleFromRequest
} from '../server/data/locales'
import { translateRestError, translateGraphqlError } from '../server/data/error-translation'
export {
SUPPORTED_LOCALES,
DEFAULT_LOCALE,
LOCALE_COOKIE,
isSupportedLocale,
negotiateLocale,
negotiateLocaleFromRequest,
translateRestError,
translateGraphqlError
}
export const LOCALE_CONFIG = {
en: {
label: 'English',
htmlLang: 'en',
openGraphLocale: 'en_GB',
dateFormat: 'dd/MM/yyyy',
dateFnsLocale: 'en-GB'
},
de: {
label: 'Deutsch',
htmlLang: 'de',
openGraphLocale: 'de_DE',
dateFormat: 'dd.MM.yyyy',
dateFnsLocale: 'de'
}
}
const readClientCookie = (name) => {
if (typeof document === 'undefined') return null
return parseCookieHeader(document.cookie || '', name)
}
const negotiateFromNavigator = () => {
if (typeof navigator === 'undefined') return null
const langs = Array.isArray(navigator.languages) ? navigator.languages : [navigator.language]
for (const lang of langs.filter(Boolean)) {
const primary = lang.toLowerCase().split('-')[0]
if (isSupportedLocale(primary)) return primary
}
return null
}
export const getCookiePath = () => {
const basePath = process.env.BASE_PATH || ''
return basePath || '/'
}
export const writeLocaleCookie = (locale) => {
if (typeof document === 'undefined' || !isSupportedLocale(locale)) return
const path = getCookiePath()
const maxAge = 60 * 60 * 24 * 365
document.cookie = `${LOCALE_COOKIE}=${encodeURIComponent(locale)}; Path=${path}; Max-Age=${maxAge}; SameSite=Lax`
}
const LocaleContext = createContext({
locale: DEFAULT_LOCALE,
ready: false,
setLocale: () => {}
})
export const LocaleProvider = ({ children }) => {
const { user } = useUser()
const [locale, setLocaleState] = useState(DEFAULT_LOCALE)
const [ready, setReady] = useState(false)
const [explicit, setExplicit] = useState(false)
const previousUserId = useRef(null)
useEffect(() => {
if (previousUserId.current !== null && previousUserId.current !== (user?.id ?? null)) {
setExplicit(false)
}
previousUserId.current = user?.id ?? null
}, [user?.id])
useEffect(() => {
if (typeof window === 'undefined') return
if (isSupportedLocale(user?.locale) && !explicit) {
setLocaleState(user.locale)
setReady(true)
return
}
if (explicit) {
setReady(true)
return
}
const cookieLocale = readClientCookie(LOCALE_COOKIE)
if (isSupportedLocale(cookieLocale)) {
setLocaleState(cookieLocale)
} else {
const navLocale = negotiateFromNavigator()
if (navLocale) setLocaleState(navLocale)
}
setReady(true)
}, [user?.locale, explicit])
const setLocale = useCallback((next) => {
if (!isSupportedLocale(next)) return
writeLocaleCookie(next)
setExplicit(true)
setLocaleState(next)
}, [])
const value = useMemo(() => ({ locale, ready, setLocale }), [locale, ready, setLocale])
return (
<LocaleContext.Provider value={value}>
{children}
</LocaleContext.Provider>
)
}
export const useResolvedLocale = () => useContext(LocaleContext)