workaround for the url locale not sticking

This commit is contained in:
Moody Salem 2021-05-29 00:29:06 -05:00
parent 8633dc7a50
commit 7d0fb9cc10
No known key found for this signature in database
GPG Key ID: 8CB5CD10385138DB

@ -1,6 +1,6 @@
import { DEFAULT_LOCALE, SupportedLocale, SUPPORTED_LOCALES } from 'constants/locales'
import { useMemo } from 'react'
import { useUserLocale } from 'state/user/hooks'
import { useEffect, useMemo } from 'react'
import { useUserLocaleManager } from 'state/user/hooks'
import useParsedQueryString from './useParsedQueryString'
/**
@ -34,11 +34,16 @@ function navigatorLocale(): SupportedLocale | undefined {
*/
export function useActiveLocale(): SupportedLocale {
const parsed = useParsedQueryString()
const userLocale = useUserLocale()
const [userLocale, setUserLocale] = useUserLocaleManager()
useEffect(() => {
const urlLocale = typeof parsed.lng === 'string' ? parseLocale(parsed.lng) : undefined
if (urlLocale) {
setUserLocale(urlLocale)
}
}, [parsed.lng, setUserLocale, userLocale])
return useMemo(() => {
const urlLocale = () => (typeof parsed.lng === 'string' && parseLocale(parsed.lng)) || undefined
return userLocale ?? urlLocale() ?? navigatorLocale() ?? DEFAULT_LOCALE
}, [userLocale, parsed])
return userLocale ?? navigatorLocale() ?? DEFAULT_LOCALE
}, [userLocale])
}