-
Notifications
You must be signed in to change notification settings - Fork 586
/
Copy pathuse-language.ts
30 lines (25 loc) · 1.07 KB
/
use-language.ts
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
import i18next from 'i18next'
import {arrayIncludes} from 'ts-extras'
import {trpcReact} from '@/trpc/trpc'
import {SupportedLanguageCode, supportedLanguageCodes} from '@/utils/language'
export function useLanguage(): [code: SupportedLanguageCode, setCode: (code: SupportedLanguageCode) => void] {
const ctx = trpcReact.useContext()
const userSetMut = trpcReact.user.set.useMutation({
onSuccess() {
ctx.user.get.invalidate()
ctx.user.language.invalidate()
},
})
const setCode = (language: SupportedLanguageCode) => {
// Update the preferred language on the backend, which in turn notifies
// RemoteLanguageInjector that the preferred language has changed. When
// this happens, and the preferred language differs from the active
// language, the injector sets the new language and reloads the page.
if (arrayIncludes(supportedLanguageCodes, language)) {
userSetMut.mutate({language})
}
}
// Default to English if active code is not supported
const code = arrayIncludes(supportedLanguageCodes, i18next.language) ? i18next.language : 'en'
return [code, setCode]
}